The full name of the JSP is Java Server Pages, the Chinese name is the Java Servers page, it is the traditional HTML page inserted Java code Snippets and JSP tags formed by the file.
The previous servlet only explained why we inherited httpservlet in development, not servlets and Genericservlet. And just to ServletConfig, ServletContext, Servletresponse, ServletRequest four objects have a simple way to introduce, this article will be followed by a more detailed explanation of the previous article.
First of all we need to know that on the JSP page need to write Java code in <% java ...%> , JSP through the server to resolve the translation of its underlying is actually a Servlet implementation.
Know how to write Java code on a JSP page, and then how to write Java code and relate to the server. First we need to understand the nine hidden objects of a JSP page.
Request--HttpServletRequest Object
Response--HttpServletResponse Object
PageContext--The context of the page, the PageContext object, from which you can get to the other eight hidden objects, you can also get additional information to the current page, can represent the current page
Session-a conversation that represents a browser and a server, an object of HttpSession
Application--Represents the current Web application, is the ServletContext object, you can get the initialization parameter parameters of the current Web application
Config--Represents the current ServletConfig object
Out--is the JspWriter object, call out.println () can print characters to the page, <%=%> equals Out.print () function is output to the page (the underlying implementation is the same)
Page-a reference to the Servlet object that the current JSP corresponds to , an object-to - image
Exception--Used when iserrorpage=true is declared in the page directive
We go on to the ServletContext and other objects described in the previous article, SetAttribute (), getattribute (), then the implied object of the JSP has the methods that these objects have, and then we give the request on the page PageContext session application Four objects set properties, get their property values on another page and Servlet, index.jsp as follows:
1<%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>234<title>LoginPage</title>56<body>78<%9Request.setattribute ("Request", "Requestvalue");TenPagecontext.setattribute ("PageContext", "Pagecontextvalue"); OneSession.setattribute ("Session", "Sessionvalue"); AApplication.setattribute ("Application", "Applicationvalue"); -%> -<br><br> the<%= Request.getattribute ("request")%> -<br><br> -<%= Session.getattribute ("session")%> -<br><br> +<%= Pagecontext.getattribute ("PageContext")%> -<br><br> +<%= Application.getattribute ("Application")%> A<br><br> at<%--jump to Page2 and Servlet get Properties--%> -<a href= "page2.jsp" >ToPage2</a> -<a href= "Attributeservlet" >ToServlet</a> -</body> -
Page2.jsp as follows:
<%@ Page ContentType="Text/html;charset=utf-8"language="Java" %><HTML><Head> <title>Page2</title></Head><Body> <H2>PAGE2</H2> <BR><BR> <%=Request.getattribute ("Request")%> <BR><BR> <%=Session.getattribute ("Session")%> <BR><BR> <%=Pagecontext.getattribute ("PageContext")%> <BR><BR> <%=Application.getattribute ("Application")%> <BR><BR> <ahref= "index.jsp">Toindex</a></Body></HTML>
Attributeservlet as follows
1 Packageattribute.scope.test;2 3 Importjavax.servlet.ServletException;4 ImportJavax.servlet.http.HttpServlet;5 Importjavax.servlet.http.HttpServletRequest;6 ImportJavax.servlet.http.HttpServletResponse;7 Importjava.io.IOException;8 9 Public classAttributeservletextendsHttpServlet {Ten protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException { One A } - //because it is a hyperlink, it is a GET request, so you only need to overwrite the Doget method - protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException { theObject objrequest = Request.getattribute ("Request"); -Object objsession = Request.getsession (). getattribute ("session")); -Object objapplication = Getservletcontext (). getattribute ("Application"); - +System.out.println ("Application:" +objapplication); -System.out.println ("Request:" +objrequest); +System.out.println ("Session:" +objsession); A } at}
After running the program, only the values of session and application two objects in page2.jsp are not NULL, and only the session and application two objects in the Servlet are not empty, because the PageContext object The scope of the current page, so jump to another page or the Servlet will not get to, and the scope of the request is one of the requests, because the click is a hyperlink so each time is a different request, so get not, and the session is scoped to a session, That is, the browser opens until it is closed for a session, and application is scoped to the current web app, and can be accessed anywhere in the current web app, as long as the property is set in one place. We call the four objects above as domain objects, and their properties range from small to large to PageContext request session application
We have described the two methods associated with Attribute, followed by the forwarding and redirection of requests, and we need to know the difference between forwarding and redirection:
For request forwarding, the URL is the address of the initial sending request and is redirected to the address of the request response;
The request object is the same as requested in a different Servlet in the case of forwarding, and redirects are not the same
Request forwarding can only be used for the current WEB app, and request redirection may work on any resource including network resources
Forward implementation Step Servletrequest.getrequestdispatcher ("/user.jsp"). Forward (ServletRequest, servletresponse);
Call HttpServletRequest's Getrequestdispatcher () method to get the RequestDispatcher object, call Getrequestdispatcher () need to pass in the address to be forwarded;
Call HttpServletRequest's forward (request, response)
/Represents the current WEB app root directory (http://localhost:8989/project name)
REDIRECT Step response.sendredirect ("user.jsp");
The Sendredirect method of calling response (HttpServletResponse object) directly
We know that the requested redirect and the forwarding of the request, together with the requested domain object we learned earlier, can achieve the property values of the request across the page.
JSP Instruction Page directive
The session value of the page instruction is either True or FALSE, which indicates whether the current page can be used to suppress the field object;
Iserrorpage can be set to TRUE or false to specify whether the current page is an error page, and if true, then the page can call the JSP domain object Exception, you can use Exception.getmessage () to print the error message. If the iserrorpage of a page is true, then the page should not be accessed directly, only through request forwarding access, because if the direct access, the Exception domain object will be null, the call GetMessage () method will have null pointer exception;
ErrorPage specifies that the current page has an exception to a specified page, and that the iserrorpage of the page should be true;
ContentType Sets the page response type, which is actually called response.setcontenttype ("text/html; charset= "UTF-8" ") method, JSP page is usually set to text/html;
CHARSET Specifies the page character encoding returned; pageencoding Specifies the current page character encoding
iselignored Specifies whether the current JSP page can use an El expression (which will be explained after the El Expression), usually true;
Solve Chinese garbled characters
1. Set all programmable encoding to UTF-8, can solve the current page and return page is not garbled;
2. If you get the value in the form and return it, and the form value is Chinese using the method above will not reach the target, this situation can be used request.setcharacterencoding ("UTF-8") , and this method must be called before getting the form value (that is, Request.getparameter ("name"); Before), the POST method.
3. Call the above setcharacterencoding ("UTF-8") for the GET request; does not work because the default transfer process is encoded as iso-8859-1, when we can modify the Tomact configuration file server.xml and add usebodyencodingforuri= "true" to the Connector node, then for the GET Request request.setcharacterencoding ("UTF-8"); Method works as follows:
<port= "9090" protocol= "http/1.1" connectiontimeout = "20000" redirectport= "8443" usebodyencodingforuri= " True "/>
Plus the previous Servlet blog is my last week of learning content, I hope that the great God can give some advice, such as how to write a blog, in this first thank you!
JSP Learning Notes