Data transmission through JSP (web basic learning Note 3) and web Learning notes
Difference between get and post:
JSP built-in objects:
1) out Built-in objects: out Built-in objects are the most frequently used objects during JSP development. However, they are also the simplest to use. The out object is used to output data to the client browser. The out object is usually used by the print () or println () methods. These two methods are used to print the string information on the page.
2) request built-in object: The request object. The request built-in object in JSP corresponds to the HttpServletRequest Class Object in Servlet and is one of the most common JSP built-in objects, this built-in object encapsulates information about browser requests. For example, the user name, password, and other information we enter on the page are encapsulated in the request. The request object is mainly used to process client requests.
3) response built-in object: The response object corresponds to the request object, which is used to respond to the client request and output information to the client. The response built-in object in JSP corresponds to the HttpServletResponse Class Object in Servlet. This object encapsulates the response generated by JSP and is then sent to the client to respond to the customer's request.
4) application built-in object: The application corresponds to the objects of the ServletContext class in the Servlet. It has only one instance in each Web project and is often used to share data between different users. Because different users access the same object when accessing the application, a user can put the data to be shared in the application object, then other users can obtain the desired shared data in this object.
5) session built-in object: session is the session between the client browser and the web server. This object is the same as the built-in object in the request. It is one of the most commonly used built-in objects during JSP development, its most common function is Access Control. This session object is an instance of the HttpSession class in Servlet.
6) built-in pageContext object:The pageContext object is javax. servlet. the PageContext class object in the jsp package provides access to all objects and namespaces on the JSP page. With this object, you can access the application object, session object, and exception object. That is to say, this object can access eight built-in JSp objects except itself. YesAccess the Java objects bound to objects such as page, request, session, and application. It is equivalent to the integrator of all object functions in the JSP program.
7) built-in page Object: The page Object is java. lang. an example of the Object class refers to an example of the Servlet implementation class after a JSP page is translated into a Servlet during the translation stage, that is, it is the JSP page itself. Therefore, the scope of this object is limited to the current JSP page.
8) config built-in object: The config object corresponds to the ServletConfig interface in the Servlet, which is used to represent the configuration of Servelt. When the Servlet container initializes a Servlet object, the Servlet container will pass some initialization information to the Servlet through this object.
9) built-in exception object: The exception object is an instance of the java. lang. Throwable class. It refers to the running exception, that is, the result of the called error page, which can only be used on the error page (the page with isErrorPage = true configured in the page command.
Request:
// JSP receives the client Registration Information and outputs <% String username = request. getParameter ("username"); String password = request. getParameter ("password"); String con_password = request. getParameter ("con_password"); String email = request. getParameter ("email"); %> User name: <% = username %> <br/> password: <% = password %> <br/> confirm the password: <% = con_password %> <br/> email: <% = email %>
Request object method:
| Method Name |
Method description |
| GetCookies () |
Obtain the Cookie sent by the client |
| GetSession () |
Returns the Session associated with the client. If no Session is assigned to the client, a Session is created and |
| GetSession (Boolean create) |
Similar to the above method, the difference is that if no Session is assigned to the client, the system determines whether to create a new Session based on the parameter and returns |
| GetParameter (String name) |
Obtain the value of the parameter named name in the request. If this parameter is not found in the request, null is returned. |
| GetParameterValues (String name) |
Returns the parameter value named name in the request. This value is often submitted by the checkbox control. Therefore, a String array is returned. |
| GetMethod () |
Return the HTTP Method Used for this request (such as GET, POST, and PUT) |
| GetHeader (String name) |
Returns the value of a request header field, which is a string. |
| GetIntHeader (String name) |
Returns the value of a request header field, which is converted to an integer. |
| GetPathInfo () |
Returns the additional path information of the request URL after the Servlet Path of the request URL. |
Supported Chinese encoding formats:
GB2312, GBK, UTF-8
Request garbled:
Post: sets the encoding method of the request and response to solve the problem:
Request:
Request. setCharcterEncoding ("UTF-8 ");
Response:
Response. setCharcterEncoding ("UTF-8 ");
Or in the jsp page declaration section:
<% @ Page language = "java" contentType = "text/html; charset = UTF-8"
// Request Encoding request. setCharacterEncoding ("UTF-8"); // response setting encoding response. setCharacterEncoding ("UTF-8"); // the response in the Declaration section can be set without writing the above <% @ page language = "java" contentType = "text/html; charset = UTF-8 "pageEncoding =" UTF-8 "%>
Get:
// Receiving username String un = request. getParameter ("username"); // get method solves garbled String username = new String (un. getBytes ("iso-8859-1"), "UTF-8 ");
Conf/server. xml file in Tomcat
<Connector connectionTimeout="20000" port="80" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>
Page jump:
Redirection without parameters and redirection with parameters:
If (username. equals ("admin") {// registration not allowed // jump directly // response. sendRedirect ("userCreate. jsp "); // jump with returned information, set the request information. setAttribute ("mess", "registration failed, please change the user name"); request. getRequestDispatcher ("userCreate. jsp "). forward (request, response);} else {// registration successful // jump directly // response. sendRedirect ("index. jsp "); // jump with returned information, set the request information. setAttribute ("mess", "registered successfully"); request. getRequestDispatcher ("index. jsp "). forward (request, response );}
Response. sendRedirect and request. getRequestDispatcher ("index. jsp"). forward (request, response) are different: