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:
- < jsp:include page="Relative URL">
- < jsp:param name="param name" value="paramvalue" />
- < /jsp:include>
It is also possible to pass parameters when using the Jsp:forward action to do page jumps, as follows:
- < jsp:forward page="Relative URL">
- < 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.
- Session.setattribute (Name,value);
- Request.setattribute (Name,value)
Take parameters:
- Value= (value className) session.getattribute (name);
- 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
- < HTML>
- < form method=get action=2.jsp>
- What ' s your name< input type=text name=username >
- < input type=submit value=submit>
- < /form>
- < /html>
4.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>What ' s your hobby< input type= text name=hobby>
- < input type=submit value=submit>
- < /form>
- < /html>
5.3.jsp
- < HTML>
- Your name is:< %=session.getattribute ("username")%>
- < br>
- Your hobby is:< %=request.getparameter ("hobby")%>
- < br>
- Your password is:< %=request.getparameter ("pass")%>
- < br>
- < /form>
- < /html>
A method of transmitting values between JSP pages