Pass the JSP page value, and pass the jsp page
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: includePage = "Relative URL">
-
- < Jsp: paramName = "param name" value = "paramvalue"/>
-
- </Jsp: include>
You can also pass parameters when using the jsp: forward action for page Jump, as follows:
- < Jsp: forwardPage = "Relative URL">
-
- < Jsp: paramName = "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>
- < FormMethod = get action = 2.jsp>
- What's your name< InputType = text name = username>
- < InputType = submit value = submit>
- </Form>
- </Html>
4. 2.jsp
- < Html>
-
- < FormMethod = 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 holobby< InputType = text name = holobby>
- < InputType = submit value = submit>
- </Form>
- </Html>
-
5. 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>