Two kinds of data transfer between Servlet and JSP

Source: Internet
Author: User
Tags url forwarding

Servlet and JSP can be passed between two types of values: JSP-> Servlet, Servlet-> JSP.
Use the Object request and session (regardless of application) to complete the value transfer.

I. JSP-> servlet
The JSP page has three methods to pass values to the servlet: form, URLCopy codeThe Code is as follows: <! -- JSP page -->
...
<% ......
Session. setAttribute ("testSession", "Hello session ");
Reqeust. setAttribute ("testRequest", "Hello request ");
%>
<A href = "JspServlet? Action = toServlet "> click </a>
<Form action = "JspServlet? Action = toServlet "method =" post "name =" form ">
<Input name = "username" type = "test"/>
<Input type = "submit" value = "submit">
</Form>
...

1. For the content of the form on the JSP page, such as the <input> tag, you can use request. getParameter ("username"); In the servlet.
2. URL: for example, the <a> tag's href attribute and the <form> tag's action attribute value "JspServlet? Action = toServlet ", the request is also used in servlet. getParameter ("action") is obtained. Note that the url and servlet must be in the web. the path of the <url-pattern> label in xml corresponds. This part will be mentioned later.
3. java fragment code. servlet can only receive the content of session. setAttribute ("testSession", "Hello session"), but cannot receive the content of the request. Use request. getSession (). getAttribute ("testSession") in servlet to get the session content.

Ii. Servlet
1. For servlet, you should first mention its registration content in web. xml, as shown inCopy codeThe Code is as follows: <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 Context of the root directory of the project is/jsp2servlet, which is shown in the address bar as http: // localhost: 8080/jsp2servlet /;
The project root directory contains the admin directory. The corresponding Context is/admin/jsp2servlet. the address bar displays http: // localhost: 8080/jsp2servlet/admin,
Jsp files in both directories want to be transferred to the com. demo. JspServletDemo class for processing. At this time, the url must be registered twice in web. xml.
1) In JspServlet1 of the jsp page under http: // localhost: 8080/jsp2servlet/directory, the url should be written as "JspServlet"
2) Access JspServlet2 on the jsp page in the http: // localhost: 8080/jsp2servlet/admin/directory. The url should be written as "admin/JspServlet"
2. directly use the request object in the servlet to obtain the sent request content. Use request. getSession () to obtain the session object and obtain the session content.
Here, the request. getSession () parameter is of the boolean type. This method can be understood:
A session can be considered to correspond to one session for each IE process (two sessions can be created for each new IE process). getSession returns the session object of the current user. The parameter differences are as follows:
If the parameter is true (default), a new session object is created if the "current user's session object" is blank (when the first access is made;
If the parameter is false, if the "current user's session object" is null, null is returned (that is, the session object is not automatically created ).
This method can be used to determine whether the session has expired, as shown below:Copy codeThe Code is as follows: if (request. getSession (false) = null)
System. out. println ("Session has been invalidated! ");
Else
System. out. println ("Session is active! ");

3. Servlet-> JSP
Two methods are available for transferring from servlet to jsp: redirection and url forwarding.
1. Redirect: the redirection of the path, and the content and url change. The request parameter (the session parameter is acceptable) is not allowed, that is, the request object cannot be sent to the next page using the setAttribute method in the servlet. Use the response. sendRedirect (url) method in the servlet. Note that the url here does not contain a slash/, such as response. sendRedirect ("test. jsp").
2. url Forwarding (Forward): jump to a page. The page content changes and the url remains unchanged. It can contain request and session parameters. Use getServletConfig (). getServletContext (). getRequestDispatcher (url). forward (request, response) in servlet ). The url here requires a slash (/), such as getServletConfig (). getServletContext (). getRequestDispatcher ("/test. jsp"). forward (request, response)Copy codeThe Code is as follows: String fr = "good well ";
Request. setAttribute ("test", fr );
RequestDispatcher de = request. getRequestDispatcher ("/test. jsp ");
De. forward (request, response );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.