About the problem of multi-pass value between jsp and servlet

Source: Internet
Author: User
Tags url forwarding

I. JSP-> servlet
The JSP page has three methods to pass values to the servlet: form, URL, and others.

<! -- JSP page -->

<% ......

Session. setAttribute ("testSession", "Hello session ");
Reqeust. setAttribute ("testRequest", "Hello request ");
%>

<A href = "JspServlet? Action = toServlet "> click me </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 in

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:

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)

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.