JSP Page Pass Value

Source: Internet
Author: User

It is often necessary to pass parameters between JSP pages, and sometimes it is necessary to pass parameters between multiple JSP pages. Here's how to implement this.

(1) Add directly after URL request

such as:< a href= "thexuan.jsp?action=transparams&detail=directe" > Direct transfer Parameters </a>

In particular, when using response.sendredirect to make a page turn, you can also use the following code:

Response.sendredirect ("Thexuan.jsp?action=transparams&detail=directe"), parameters can be obtained with Request.getparameter (name)

(2) Jsp:param

It allows you to implement the main page for the Include page pass parameters, as follows:

    1. < jsp:include page="Relative URL">
    2. < jsp:param name="param name" value="paramvalue" />
    3. < /jsp:include>

It is also possible to pass parameters when using the Jsp:forward action to do page jumps, as follows:

    1. < jsp:forward page="Relative URL">
    2. < jsp:param name="paramname" value="paramvalue" />

</jsp:forward> This method is the same as the normal form parameter, can also be obtained by Request.getparameter (name) parameters

(3) Set session and request

The parameter is passed to the session and request by displaying the parameters to achieve the purpose of passing the parameters.

    1. Session.setattribute (Name,value);
    2. Request.setattribute (Name,value)

Take parameters:

    1. Value= (value className) session.getattribute (name);
    2. Value= (value className) request.getattribute (name);

As you must have noticed, the type conversion is done when parameters are taken, because the properties of the objects placed in the session and request are treated as Java.lang.Object type, if not converted, the classcastexception exception will be reported when the value will be paid directly.

Passing parameters between multiple JSP pages

1. How do I pass parameters between multiple JSP pages? You need to use the JSP's built-in scope object session. Two ways to use it setattribute (), getattribute ()

2. The following example implements the ability to pass the parameters of the first JSP page to a third page

3. The code is as follows: 1.jsp

  1. < HTML>
  2. < form method=get action=2.jsp>
  3. What ' s your name< input type=text name=username >
  4. < input type=submit value=submit>
  5. < /form>
  6. < /html>

4.2.jsp

  1. < HTML>
  2. < form method=post action="3.jsp?pass=11">
  3. < %
  4. String name=request.getparameter ("username");
  5. Session.setattribute ("username", name);
  6. %>
  7. Your name is:< %=request.getparameter ("username")%>
  8. < br>What ' s your hobby< input type= text name=hobby>
  9. < input type=submit value=submit>
  10. < /form>
  11. < /html>

5.3.jsp

  1. < HTML>
  2. Your name is:< %=session.getattribute ("username")%>
  3. < br>
  4. Your hobby is:< %=request.getparameter ("hobby")%>
  5. < br>
  6. Your password is:< %=request.getparameter ("pass")%>
  7. < br>
  8. < /form>
  9. < /html>

JSP Page Pass Value

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.