Commonly used JSP built-in objects (1)

Commonly used JSP built-in objects

 

The request object is mainly used to process client requests

 

The role of the request object is to interact with the client, collect the client's Form, Cookies, hyperlinks, or collect server-side environment variables.
Common methods of request object
 
method name illustrate
String getParameter(String name) Get submit data based on form component name
String[ ] getParameterValues(String name) Get the request data when the form component corresponds to multiple values
void setCharacterEncoding(String charset) Specify the encoding for each request
RequestDispatcher getRequestDispatcher(String path) Returns a RequestDispatcher object whose forward( ) method is used to forward requests
method name illustrate
void addCookie(Cookie cookie) Add cookies to the client
void setCookie(String type) Set the corresponding contentType of Http
void setCharacterEncoding(String charset) Set the corresponding character encoding type used
void sendRedirect Relocate the request to a new url
 
Forward
Forwarding works on the server side, passing the same request between server resources
The address bar of the client browser does not display the redirected address
redirect
Redirection works on the client side, by sending a new request to achieve page turn
The redirected address can be displayed in the address bar
 
The difference between redirection and request forwarding in HTTP
In a word, forwarding is server behavior, and redirection is client behavior. Why do I say this, it depends on the workflow of the two actions:
Forwarding process: client browser sends http request----"web server accepts this request-"calls an internal method to complete request processing and forwarding action inside the container----"sends the target resource to the client; here , the forwarding path must be the url under the same web container, it cannot be redirected to other web paths, and the request in its own container is passed in the middle. The path displayed in the path bar of the client's browser is still the path of the first visit, that is to say, the client does not feel that the server has forwarded it. The forwarding behavior is that the browser only makes one access request.
Redirection process: The client browser sends an http request----"The web server sends a 302 status code response and the corresponding new location to the client browser after accepting it-"The client browser finds that it is a 302 response, and automatically sends a new one. http request, the request url is the new location address ----" the server finds the resource according to this request and sends it to the client. Here location can be redirected to any URL. Since the browser reissues the request, there is no concept of request transmission. The path bar of the client's browser displays the redirected path, and the client can observe the change of the address. The redirect behavior is that the browser makes at least two access requests.

 

Session: Common methods of objects:
 
method name illustrate
String getId() get sessionid
void setMaxInactiveInterval(int interval) Set the inactive time of the session
int getMaxInactiveInterval() Get the effective inactivity time of the session (in seconds)
void invalidate() Set the session object invalid
void setAttribute(String key, Object value) Save the object value in the form of key/value
Object getAttribute(String key) Get object value by key
void removeAttribute(String key) Delete the object corresponding to the specified name (key) from the session
 
 
 
The relationship between Session and window
  • Each session object corresponds to a browser window. Re-opening a browser window can recreate a session object (different versions of browsers may vary)
  • A new window opened by a hyperlink, the session of the new window is the same as that of the parent window
 

 

 
What is the difference between COOKIE and SESSION?
The cookie is stored on the client side, and the session is stored on the server side.
The purpose of cookies can track sessions, save user preferences or save usernames and passwords
session is used to track the session
Both jsp and servlet are singleton multithreaded.
 
 
Simple login case
main page:
 
<div id="container">
<h2>Please enter registration information</h2>
<form action="control.jsp" method="post">
用户名:<input name="uname"/><br/>
密码:<input name="upwd" type="password"/><br/>
<br/><input type="submit" value="Login"/>
</form>
</div>
 
 
Judging the login proof page:
 
String path = request.getContextPath();
Object obj = session.getAttribute("uname");
if (obj == null) { //Prove not logged in
if ((String) obj == null) {
//Prove not logged in
response.sendRedirect(path + "/computer2/login.jsp");
}
}
 
login page:
 
String path = request.getContextPath();
//1.request
String name = request.getParameter("uname");
String pwd = request.getParameter("upwd");
if (name.equals("admin") && pwd.equals("admin")) {
 
//Authentication succeeded
//Record the currently logged in user and save the user information to the session
session.setAttribute("uname", name);
//Jump to the home page
request.getRequestDispatcher("index.jsp").forward(request,response);
} else {
response.sendRedirect(path + "/computer2/login.jsp");
}
 
登陆成功页面和注销页面:
 
<a href="logout.jsp">注销</a><br/>
<%=session.getAttribute("uname")+"你好" %>
 
消除session的服务器的记录:
 
String path = request.getContextPath();
session.removeAttribute("uname");
response.sendRedirect(path+"/computer2/login.jsp");
 
 
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324974579&siteId=291194637