Detailed description of JSP parameter passing Methods

Source: Internet
Author: User

The following describes how to pass parameters between pages in JSP.

1. Add directly after URL request
For example, <a href = "thexuan. jsp? Action = transparams & detail = directe "> directly passing parameters </a>
When using response. sendRedirect for page redirection, you can also use the following code:
Response. sendRedirect ("thexuan. jsp? Action = transparams & detail = directe "), which can be obtained by request. getParameter (name ).

2. jsp: param
It allows you to pass parameters to the include page on the home page, as follows:
<Jsp: include page = "Relative URL">
<Jsp: param name = "param name" value = "paramvalue"/>
</Jsp: include>
You can also pass parameters when using the jsp: forward action for page Jump, as follows:
<Jsp: forward page = "Relative URL">
<Jsp: param name = "paramname" value = "paramvalue"/>
</Jsp: forward> in this way, parameters can be obtained through request. getParameter (name), which is the same as common form parameters.

3. Set the session and request
The displayed parameters are placed in the session and request to pass the parameters.
Session. setAttribute (name, value );
Request. setAttribute (name, value)
Take the following parameters:
Value = (value className) session. getAttribute (name );
Value = (value className) request. getAttribute (name );
 
You must have noticed that the type conversion is performed when the parameter is obtained, because the attributes of the objects placed in the session and request are treated as java. lang. if the Object type is not converted, The classcastexception exception is reported when the value is directly paid.

Passing parameters between multiple JSP pages
How can we transmit parameters between multiple JSP pages? Use the JSP built-in scope object session. Using its two methods setAttribute (), getAttribute ()
The following example shows how to pass the parameters of the first JSP page to the third page.
The Code is as follows: 1.jsp
<Html>
<Form method = get action = 2.jsp>
Whats your name <input type = text name = username>
<Input type = submit value = submit>
</Form>
</Html>

2. jsp
<Html>
<Form method = post action = "3.jsp? Pass = 11 ">
<%
String name = request. getParameter ("username ");
Session. setAttribute ("username", name );
%>
Your name is: <% = request. getParameter ("username") %>
<Br> whats your holobby <input type = text name = holobby>
<Input type = submit value = submit>
</Form>
</Html>

3. jsp
<Html>
Your name is: <% = session. getAttribute ("username") %>
<Br>
Your holobby is: <% = request. getParameter ("holobby") %>
<Br>
Your password is: <% = request. getParameter ("pass") %>
<Br>
</Form>
</Html>

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.