Parameters passing between JSP pages is a feature that is frequently used. Sometimes, parameters passing between multiple JSP pages are required. The following describes the implementation methods.
(1) Add
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
1. How to pass parameters between multiple JSP pages? Use the JSP built-in scope object session. Using its two methods setattribute (), getattribute ()
2. The following example implements the function of passing parameters of the first JSP page to the 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>
This article is from the javaeye blog: Passing parameters between JSP pages