Hello, today I share Servlet with you kids. Take out your little notebook and write it down

One, the Mapping

<servlet-name>HelloServlet</servlet-name>

<servlet-class>com.wenwu.servlet.HelloServlet</servlet-class>
Copy the code
  • A Servlet can also specify a mapping path

HelloServlet

/hello

  • A Servlrt can specify multiple mapping paths

HelloServlet

/hello

HelloServlet

/hello2

  • A Servlet can specify a generic mapping path
<servlet-name>HelloServlet</servlet-name>

<url-pattern>/hello/*</url-pattern>
Copy the code
<servlet-name>HelloServlet</servlet-name>

<url-pattern>/*</url-pattern>
Copy the code

Specify suffixes, prefixes, etc

<servlet-name>HelloServlet</servlet-name>

<url-pattern>*.do</url-pattern>
Copy the code

The ServletContext object

When the Web container is started, it creates a corresponding ServletContext object for each Web application, which represents the current Web application.

2.1 Sharing Data

HelloServlet, GetServlet

The data I save in this Sevrlet is available in another servlet

// Start with a class that holds parameters

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext servletContext = this.getServletContext(); response.setContentType("text/html"); response.setCharacterEncoding("utf-8"); String username = "username "; servletContext.setAttribute("username",username); // save a data in the ServletContext.Copy the code

​ To test the access result, you need to access the class with the following Settings to store the data before the class read by the method can read it

2.2 Obtaining Initialization Parameters

demo03

<param-name>url</param-name>

<param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
Copy the code

2.3 Request Forwarding

demo04 Result: After accessing Demo04, demo04 is entered first, and demo03 is forwarded again

2.4 Reading resource Files

Properties

  • Create properties under the Java directory
  • Create new Properties under the Resources directory

Discovery: all packaged into the same path: classes, commonly known as classpath

You need a file stream.

db. properties

username = “root”

password = “123456”

ServletDemo05.java

Third, HttpServletResponse

The Web server receives the HTTP request from the client. For this request, an HttpServletRequest object representing the request is created, representing an HttpServletResponse of the response.

  • To get the parameters requested by the client: look for HttpServletRequest
  • If you want to give the client response some information: look for HttpServletResponse

3.1 Simple Classification

The method responsible for sending data to the browser

// Write normal streams with this

ServletOutputStream getOutputStream() throws IOException;

// Use this to write Chinese

PrintWriter getWriter() throws IOException;

The method responsible for sending the response header to the browser

3.2 Common Applications

3.2.1 Output to the browser

getWriter()

getOutputStream()

3.2.2 Downloading files

  • To get the path to the downloaded file
  • What is the file name of the download?
  • Set up a way for the browser to support downloading what we need
  • Gets the input stream for the downloaded file
  • Creating a buffer
  • Get the OutputStream object
  • Writes the FileOutputStream to the buffer buffer
  • Output buffer data to the client using OutputStream!

3.2.3 Verification Code Function

3.2.4 Request Redirection

Common scenarios:

  • User logs in to void sendRediredt (string var1) throws IOException.

Redirection implementation code:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

Response.sendredirect ("/response_war/image"); response.sendredirect ("/response_war/image");Copy the code

}

Redirection implementation code:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

Response.sendredirect ("/response_war/image"); response.sendredirect ("/response_war/image");Copy the code

}

Differences between redirection and request forwarding:

  • The URL does not change when a forward request is made
  • The URL address bar will change when redirected

Simulate a simple login page using redirection:

Simulate a login page index.jsp

<%@ page contentType="text/html; Charset = utf-8 "%> <% Needs to find the path of the project - % > < % - ${pageContext. Request. ContextPath} represents the current project - % > < form Action = "${pageContext. Request. ContextPath} / login" method = "get" > username: < input type = "text" name = "username" / > < br / > password: <input type="password" name="password"/><br/> <input type="submit"/> </form>Copy the code

After login, jump to the page success.jsp

<h1> Login successful </h1>Copy the code

The servlet page RequestTest. Java

Fourth, it

The Web server receives the HTTP request from the client. For this request, an HttpServletRequest object representing the request is created, representing an HttpServletResponse of the response.

To get the parameters requested by the client: look for HttpServletRequest

If you want to give the client response some information: look for HttpServletResponse

With this HttpServletRequest method, we can get all the information about the client

4.1 Obtain the parameters passed by the front end and request forwarding

The login page index.jsp

<form action="/request_war/login" method="post"> account: <input type="text" name="username" /><br/> <input type="password" name="password"/> <input type="checkbox" name="hobbies" value=" hobbies" /> <input type="checkbox" name="hobbies" value=" alcohol "/> alcohol <input Name ="hobbies" value=" hobbies" /> <br/> <input type="submit"> </form>Copy the code

Login success page success.jsp

<h1> Login successful </h1>Copy the code

LoginServlet.java

Differences between redirection and request forwarding:

  • The URL does not change when a forward request is made
  • The URL address bar will change when redirected

Well, that’s all for today’s article, hoping to help those of you who are confused in front of the screen