Article Directory
1. Servlet binding method
–HttpServletRequest
–HttpServletResponse
–HttpSession
–ServletContext
2. The role of Session and Cookie
2.1. Session
Session saves data on the server:
HttpSession session = req.getSession();
session.setAttribute("Binding Name", "Data");
The validity period (lifetime) of the session on most servers is 20-30 minutes by default, and it can also be set by yourself.
Important features of HttpSession: sharing among multiple servlets
2.2 Cookie
Cookie: save data in the browser client, unimportant data, a small amount of data that is not designed for security
The server adds cookies to the browser:
Cookie cookie = new Cookie("name","fengkang");
Use the response object:
HttpServletResponse responds to the browser
resp.addCookie(cookie);
generally do not set the save time, the data will be destroyed when the browser is closed
3. Use of JSTL Tag Library
JSTL tag library: Provides many tags with functions, such as if, forEach
Guide package:
<!-- https://mvnrepository.com/artifact/jstl/jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
Use: Introduce instructions in the jsp page that needs to use the JSTL tag library
4. Page jump: redirect, forward
Redirection: After the client requests, the server gives the 302 status code and address to the client, and the client then requests the page (request twice)
resp.sendRedirect("Address, such as /userSelectServlet");
Forwarding: The client requests two parameters req and resp to the server in one request, and the server binds data to the page
5. The essence of jsp is a servlet
The essence of jsp is servlet (after compilation). When jsp is accessed for the first time, the web container will be translated into servle code.