1. Overview of built-in objects
jsp files automatically generate and declare objects when they are translated into other corresponding servlet files. We can use them directly in jsp pages, but not in global variables.
jsp's built-in objects are: application, config, exception, out, page, pageContext, request, response, session, among which the most important and commonly used are request, response, session, scope server and client to communicate.
2. request object
The request object sends requests from the client to the server, including information submitted by the user and some information from the client.
Function: Mainly used for processing various parameters and options in requests submitted by client browsers.
(1) Access request parameters, getParameter() method
Function: It can be used to obtain data submitted by users.
Format:
String userName = request.getParameter("name");
Example:
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form id="form1" name="form1" method="post" action="login_deal.jsp"> User name: <input name="username" type="text" id="username"/><br><br> dense code: <input name="pwd" type="password" id="pwd"/><br><br> <input type="submit" name="Submit" value="Submission"/> <input type="reset" name="Submit2" value="Reset"/> </form> </body> </html>
login_deal.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" errorPage=""%> <% String username=request.getParameter("username"); String pwd=request.getParameter("pwd"); out.println("User name:"+username); out.println("Password:"+pwd); %>
Operation result:
(2) Managing attributes in scope
Function: When forwarding requests, some data need to be brought to the forwarded page for processing.
Set the format for forwarding data:
<%request.setAttribute("key", Object)%>
When retrieving data from forwarded pages, it needs to be retrieved by key s, which represent data that needs to be stored in the request range.
Get the format of the forwarded data:
<%request.getAttribute(String name)%>
Example:
setAttribute.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%request.setAttribute("error", "Sorry, the username or password is wrong!");%> <jsp:forward page="error.jsp"></jsp:forward>
error.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%out.println("The error message is:"+request.getAttribute("error")); %>
Running results:
(3) Obtaining Cookie
Cookie is a useful way for Web applications to save relevant information and to solve different data requests. Cookie is a small piece of text information that is transmitted between web server and browser along with user requests and pages.
Format:
<%Cookie[] cookie=request.getCookies(); %>
Example:
showCookie.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% Cookie[] cookies=request.getCookies();//Getting Cookies Set from request //Initialize the Cookie set to be empty Cookie cookie_response=null; //Determine whether cookies are empty if(cookies!=null){ cookie_response=cookies[1]; } out.println("Time of this visit: "+new java.util.Date().toLocaleString()+"<br>"); if(cookie_response!=null){ //Output the time of the last page visit and set cookie_response to the new time out.println("Last visit time: "+cookie_response.getValue()); cookie_response.setValue(new java.util.Date().toLocaleString()); //response.addCookie(cookie_response); } //If the cookies set is Null, create the cookie and add it to the response if(cookies==null){ cookie_response=new Cookie("Access Time",""); cookie_response.setValue(new java.util.Date().toLocaleString()); response.addCookie(cookie_response); } %>
Operation results:
There is a problem here, the last visit time appeared chaotic code, temporarily unresolved.