Two types of transfer between servlets and JSPS

  • 2020-04-01 01:23:47
  • OfStack

There are two types of value transfers between servlets and JSPS: JSP -> The Servlet, the Servlet - > The JSP.
The value is passed through the object request and session (regardless of application).

A, JSP - > The servlet
There are three ways for JSP pages to pass values to servlets: form form, URL
 
<!-- JSP page --> 
... 
<%...... 
session.setAttribute("testSession","Hello session"); 
reqeust.setAttribute("testRequest","Hello request"); 
%> 
<a href="JspServlet?action=toServlet"> Click on the </a> 
<form action="JspServlet?action=toServlet" method="post" name="form"> 
<input name="username" type="test" /> 
<input type="submit" value="submit"> 
</form> 
... 

1. For the contents of the JSP page form form, such as < Input> The tag request.getparameter ("username") is available in the servlet; To obtain.
2, URL: such as here < A> Tag href attribute with < Form> The value of the action attribute of the tag "JspServlet? Action =toServlet", which is also obtained by request-getparameter ("action") in servlet; Notice that the url here has to be in the url with the servlet in web.xml. Url - pattern> The path of the label corresponds to. We'll talk about that later.
3. Java snippet code: the servlet can only receive the contents of session.setattribute ("testSession","Hello session"), but not the contents of the request. Get the session content in the servlet using request-getsession ().getattribute ("testSession").

Second, the Servlet
1. With regard to the servlet, the first thing to mention is its registration in web.xml, such as
 
<servlet-name>JspServlet1</servlet-name> 
<servlet-class>com.demo.JspServletDemo</servlet-class> 
</servlet> 
<servlet-mapping> 
<servlet-name>JspServlet1</servlet-name> 
<url-pattern>/JspServlet</url-pattern> 
</servlet-mapping> 
<servlet-name>JspServlet2</servlet-name> 
<servlet-class>com.demo.JspServletDemo</servlet-class> 
</servlet> 
<servlet-mapping> 
<servlet-name>JspServlet2</servlet-name> 
<url-pattern>/admin/JspServlet</url-pattern> 
</servlet-mapping> 

If the project name is jsp2servlet, the project root directory Context is/jsp2servlet, display is http://localhost:8080/jsp2servlet/; in the address bar
Admin directory in the project root directory, the corresponding Context is/admin/jsp2servlet, in the address bar display is http://localhost:8080/jsp2servlet/admin,
The JSPS in both directories want to go to the com.demo.jspservletdemo class for processing, and the url needs to be registered twice in web.xml.
1) on the http://localhost:8080/jsp2servlet/ JspServlet1 JSP page in the directory url should be written as "JspServlet"
2) in the http://localhost:8080/jsp2servlet/admin/ directory access JspServlet2 JSP page, the url should be written as "admin/JspServlet"
2. Directly use the request object in the servlet to get the request content sent; With request-getsession (), you get the session object, and thus the session content.
Here, the request.getsession () parameter is a Boolean type, which can be interpreted as:
Session can be considered as a session corresponding to each IE process (a newly opened IE process can correspond to two sessions). GetSession is a session object that returns the current user. The difference of parameters is:
If the "current user's session object" is null (when first accessed), a new session object is created and returned.
If the current user's session object is empty, null is returned (that is, the session object is not automatically created).
This method can be used to determine whether the session is expired, as follows:
 
if(request.getSession(false)==null) 
System.out.println("Session has been invalidated!"); 
else 
System.out.println("Session is active!"); 

Third, the Servlet - > The JSP
There are two ways to go from a servlet to a JSP, redirection and url forwarding
1, Redirect: is a Redirect to a path, the content and url are changed. It is not allowed to take the request parameter (the session parameter can), that is, it is not allowed to pass the request object to the next page in the servlet using the setAttribute method. Use the response.sendredirect (url) method in the servlet. Notice that the url here is not preceded by a slash /, such as response.sendredirect (" test.jsp ")
2. Url Forward: it is the jump of the page, the content of the page changes, and the url remains unchanged. You can take the request and session parameters. GetServletConfig (). GetServletContext (). GetRequestDispatcher (url). Forward (request, response). The url here needs to be preceded by a slash /, such as getServletConfig().getservletcontext ().getrequestdispatcher (" /test.jsp ").forward(request, response)
 
String fr="good well"; 
request.setAttribute("test", fr); 
RequestDispatcher de=request.getRequestDispatcher("/test.jsp"); 
de.forward(request, response); 

Related articles: