1. Parameter passing when jumping from one jsp page to another jsp page
(1) Use the request object to get the information submitted by the client
The login.jsp page code is as follows:
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<form action="getinfo.jsp">
<input type="text" name="name">
<input type="submit" value="Enter" name="submit">
</form>
</body>
</html>
The getinfo.jsp page code is as follows:
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<p>Get the information submitted by the text box:
<%
String textContent = request.getParameter("name");
%>
Tr
<%=textContent%>
</p>Get the name of the button:
<%
String buttonName = request.getParameter("submit");
%>
Tr
<%=buttonName%>
</body>
</html>
(2) use the session object to get the information submitted from the client
The session1.jsp page code is as follows:
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<form action="session2.jsp" method="post">
please enter your name:
<input type="text" name="thename">
<input type="submit" value="submit">
</form>
</body>
</html>
Session2.jsp extracts the name value in the session1.jsp form through the request object and saves it to the session object. The session2.jsp code is as follows:
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<%
String name = request.getParameter("thename");
session.setAttribute("thename",name);
%>
Your name is: <%=name%>
<p>
<form action="session3.jsp?pwd=123456" method="post">
What do you like to eat?
<input type="text" name="food"/>
<p>
<input type="submit" value="submit">
</form>
</body>
</html>
The main task of session3.jsp is to display the result. It extracts the value of thename from the session object and displays it to prove that the session object can store and maintain information in multiple pages. The session3.jsp code is as follows:
<pre name="code" class="java"><%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<%
String food = request.getParameter("food");
String name = (String)session.getAttribute("thename");
String pwd = request.getParameter("pwd");
%>
Your name is: <%=name%>
<p>
You like to eat: <%=food%>
<p>
Your password is: <%=pwd%>
</body>
</html></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
(3) Passing data using hidden fields
The hiddenindex.jsp code that uses the hidden space to record the user's submission date is as follows:
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<form action="hidden.jsp" method="post" name="form1">
<!-- hide fields, system time, and convert to milliseconds -->
<input type="hidden" name="time" value="<%=new java.util.Date()%>">
<p>
Please enter your name: <input type="text" name="name">
</p>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
The hidden.jsp code that displays hidden space information is as follows:
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<%
String name = request.getParameter("name");
String date = request.getParameter("time");
%>
<p>Your name is:
<%=name %>
</p>
<p>The system time at the time of submission is:
<%=date %>
</p>
</body>
</html>
One disadvantage of using a hidden field is that the user can read the value of the hidden field by looking at the web page source.
(4) Passing data through hyperlinks
The hyperlink page.jsp code that links to other tabs is as follows:
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
Int PageCount = 15;
%>
<html>
<body>
<table>
<tr>
<td>
<a href=showpage.jsp?ToPage=<%=1%>> to page 1</a>
</td>
<td>
<a href=showpage.jsp?ToPage=<%=PageCount%>> to the last page</a>
</td>
</tr>
</table>
</body>
</html>
Receive the display pagination showpage.jsp code as follows:
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<%
String toPage = request.getParameter("ToPage");
Out.println("第" + toPage + "Page");
%>
</body>
</html>
2.jsp page passing parameters to the servlet
(1) Receive parameters using the request object
Create a new Servlet class named ForwardServlet, the code is as follows
Package com.shi.servlet;
Import java.io.IOException;
Import javax.servlet.ServletException;
Import javax.servlet.http.HttpServlet;
Import javax.servlet.http.HttpServletRequest;
Import javax.servlet.http.HttpServletResponse;
/**
* This class is used to obtain form request information and forward the page according to the request information.
*/
Public class ForwardServlet extends HttpServlet
{
Private static final long serialVersionUID = 1L;
/**
* Constructor
*/
Public ForwardServlet()
{
Super();
}
/**
* Initialization
*/
Public void init() throws ServletException
{}
/**
* doGet() method
*/
Public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doPost(request, response);
}
/**
* doPost() method
*/
Public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
If((name!=null && !name.equals("")) && (pwd!=null && !pwd.equals("")))
{
If(name.equals("mr")&&pwd.equals("123"))
{
request.getRequestDispatcher("success.jsp").forward(request, response);
}
Else
{
request.getRequestDispatcher("error.jsp").forward(request, response);
}
}
}
/**
* Destruction
*/
Public void destroy()
{
Super.destroy();
}
}
Configure the ForwardServlet class in web.xml with the following code:
<?xml version="1.0" encoding="GB2312"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- Configure servlet -->
<servlet>
<servlet-name>ForwardServlet</servlet-name>
<servlet-class>com.shi.servlet.ForwardServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ForwardServlet</servlet-name>
<url-pattern>/forward</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Create index.jsp, where the action attribute value in <form> is the url configured by the Servlet class in web.xml. The submission method is post. The code is as follows:
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<form action="forward" method="post">
<table align="center">
<tr>
<td>Username:</td>
<td>
<input type="text" name="name"/>
</td>
</tr>
<tr>
<td>Password:</td>
<td>
<input type="password" name="pwd"/>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Login"/>
</td>
</tr>
</table>
</form>
</body>
</html>
Login success page success.jsp, the code is as follows:
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
Welcome <%=request.getParameter("name") %>, login is successful!
</body>
</html>
(2) use the session object, url, form to pass parameters
In jsp2servlet.jsp, use the request object, session object, url, form to set the parameters to be passed, the code is as follows:
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<%
session.setAttribute("testSession","Hello session");
request.setAttribute("testRequest","Hello request");
%>
<a href="http://localhost:8080/086/jsp2servlet?hrefAction=toServlet">Click here</a>
<form action="jsp2servlet?action=toServlet" method="post" name="form1">
<input type="text" name="username"/>
<input type="submit" value="submit"/>
</form>
</body>
</html>
In the Jsp2Servlet to receive the parameters passed in the jsp page and display it, the code is as follows:
Package com.shi.servlet;
Import java.io.IOException;
Import java.io.PrintWriter;
Import javax.servlet.ServletException;
Import javax.servlet.http.HttpServlet;
Import javax.servlet.http.HttpServletRequest;
Import javax.servlet.http.HttpServletResponse;
Public class Jsp2Servlet extends HttpServlet
{
/**
* Automatic generated
*/
Private static final long serialVersionUID = 1L;
/**
* Constructor
*/
Public Jsp2Servlet()
{
Super();
}
/**
* Initialization
*/
Public void init()
{}
/**
* doGet()
*/
Public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doPost(request, response);
}
/**
* doPost()
*/
Public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String strSession = (String)request.getSession().getAttribute("testSession");
String strRequest = (String)request.getAttribute("testRequest");
String strHref = request.getParameter("hrefAction");
String strForm = request.getParameter("action");
String username = request.getParameter("username");
PrintWriter out = response.getWriter();
Out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
Out.println("<html>");
Out.println("<body>");
Out.println("Save to session: " + strSession + "<br>");
Out.println("Save to the contents of the request:" + strRequest + "<br>");
Out.println("The value of the parameter passed in the hyperlink is: " + strHref + "<br>");
Out.println("The action value passed in the form is: " + strForm + "<br>");
Out.println("The value of username in the input tag in the form: " + username + "<br>");
Out.println("</body>");
Out.println("</html>");
}
/**
* Destruction
*/
Public void destroy()
{
Super.destroy();
}
}
The servlet can't get the content in request.setAttribute() because when a commit is made in jsp2servlet.jsp, a new request is generated, so the value set in request.setAttribute() in the jsp page cannot be fetched.
3. Pass parameters from the servlet to the jsp page
From the servlet to the jsp page to pass parameters, you can use the URL, request object, session object, create a new Servlet2Jsp.java, the code is as follows:
Package com.shi.servlet;
Import java.io.IOException;
Import javax.servlet.ServletException;
Import javax.servlet.http.HttpServlet;
Import javax.servlet.http.HttpServletRequest;
Import javax.servlet.http.HttpServletResponse;
Public class Servlet2Jsp extends HttpServlet
{
/**
* Automatic generated
*/
Private static final long serialVersionUID = 1L;
/**
* Constructor
*/
Public Servlet2Jsp()
{
Super();
}
/**
* Initialization
*/
Public void init()
{}
/**
* doGet()
*/
Public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doPost(request, response);
}
/**
* doPost()
*/
Public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String url = "result.jsp?first=pass the first argument &second=pass the second argument";
String strRequest = "request passed value";
String strSession = "session passed value";
request.setAttribute("strRequest", strRequest);
request.getSession().setAttribute("strSession", strSession);
/**
* Client jump: low efficiency
* session scope property, the parameters in the url will be passed, the request scope property will not be passed
*/
//response.sendRedirect(url);
/**
* Server-side jump: common, high efficiency
* request range attribute, session range attribute, parameter in url will be passed
*/
request.getRequestDispatcher(url).forward(request, response);
}
/**
* Destruction
*/
Public void destroy()
{
Super.destroy();
}
}
After configuring the servlet in web.xml, create a new parameter receiving page result.jsp, the code is as follows:
[java] view plain copy
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<%
String strURL = request.getParameter("first");
String strRequest = (String)request.getAttribute("strRequest");
String strSession = (String)request.getSession().getAttribute("strSession");
%>
<p>
The attribute value obtained in the URL is: <%=strURL%>
</p>
<p>
The attribute value obtained in the request is: <%=strRequest%>
</p>
<p>
The attribute value obtained in the session is: <%=strSession%>
</p>
</body>
</html>
4. Parameter passing from one servlet to another servlet
(1) Passing data through hyperlinks and forms
Given the value of the parameter to be passed after the hyperlink, but the length is limited, the data is passed through the hyperlink and the form, using the request.getParamter() method, the code is as follows:
Package com.shi.servlet;
Import java.io.IOException;
Import java.io.PrintWriter;
Import javax.servlet.ServletException;
Import javax.servlet.http.HttpServlet;
Import javax.servlet.http.HttpServletRequest;
Import javax.servlet.http.HttpServletResponse;
Public class ServletA extends HttpServlet
{
/**
* Automatic generated
*/
Private static final long serialVersionUID = 1L;
/**
* Constructor
*/
Public ServletA()
{
Super();
}
/**
* Initialization
*/
Public void init()
{}
/**
* doGet()
*/
Public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doPost(request, response);
}
/**
* doPost()
*/
Public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name = "hello, servlet";
Out.println("<html>");
Out.println("<body>");
/**
* Hyperlink
*/
Out.println("<a href='ServletB?name="+name+"'>this is a test</a>");
/**
* form
*/
Out.println("<form action='ServletB' method='post'>");
Out.println("username:");
Out.println("<input type='text' name='username'>");
Out.println("<input type='submit' vlue='submit'>");
Out.println("</form>");
Out.println("</body>");
Out.println("</html>");
Out.flush();
}
/**
* Destruction
*/
Public void destroy()
{
Super.destroy();
}
}
The code that passes the parameters passed in the servlet is as follows:
Package com.shi.servlet;
Import java.io.IOException;
Import java.io.PrintWriter;
Import javax.servlet.ServletException;
Import javax.servlet.http.HttpServlet;
Import javax.servlet.http.HttpServletRequest;
Import javax.servlet.http.HttpServletResponse;
Public class ServletB extends HttpServlet
{
/**
* Automatic generated
*/
Private static final long serialVersionUID = 1L;
/**
* Constructor
*/
Public ServletB()
{
Super();
}
/**
* Initialization
*/
Public void init()
{}
/**
* doGet()
*/
Public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doPost(request, response);
}
/**
* doPost()
*/
Public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String name = request.getParameter("name");
String username = request.getParameter("username");
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
Out.println("<html>");
Out.println("<body>");
Out.println("The parameter value passed by the hyperlink is: " + name);
Out.println("The username submitted in the form is: " + username );
Out.println("</body>");
Out.println("</html>");
Out.flush();
}
/**
* Destruction
*/
Public void destroy()
{
Super.destroy();
}
}
(2) Pass parameters using setAttribute() and getAttribute() methods
The ServletA code to set the parameters is as follows:
Package com.shi.servlet;
Import java.io.IOException;
Import java.io.PrintWriter;
Import javax.servlet.ServletException;
Import javax.servlet.http.HttpServlet;
Import javax.servlet.http.HttpServletRequest;
Import javax.servlet.http.HttpServletResponse;
Public class ServletA extends HttpServlet
{
/**
* Automatic generated
*/
Private static final long serialVersionUID = 1L;
/**
* Constructor
*/
Public ServletA()
{
Super();
}
/**
* Initialization
*/
Public void init()
{}
/**
* doGet()
*/
Public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doPost(request, response);
}
/**
* doPost()
*/
Public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
/**
* setAttribute() and getAttribute()
*/
String data = "This is a test, we need more test for this";
request.setAttribute("strAtt", data);
request.getRequestDispatcher("/ServletB").forward(request, response);
}
/**
* Destruction
*/
Public void destroy()
{
Super.destroy();
}
}
The ServletB code to get the parameters is as follows:
Package com.shi.servlet;
Import java.io.IOException;
Import java.io.PrintWriter;
Import javax.servlet.ServletException;
Import javax.servlet.http.HttpServlet;
Import javax.servlet.http.HttpServletRequest;
Import javax.servlet.http.HttpServletResponse;
Public class ServletB extends HttpServlet
{
/**
* Automatic generated
*/
Private static final long serialVersionUID = 1L;
/**
* Constructor
*/
Public ServletB()
{
Super();
}
/**
* Initialization
*/
Public void init()
{}
/**
* doGet()
*/
Public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doPost(request, response);
}
/**
* doPost()
*/
Public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String strAtt = (String)request.getAttribute("strAtt");
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
Out.println("<html>");
Out.println("<body>");
Out.println("getAttribute() gets the data value: " + strAtt);
Out.println("</body>");
Out.println("</html>");
Out.flush();
}
/**
* Destruction
*/
Public void destroy()
{
Super.destroy();
}
}
5.JSP page jump method
(1) RequestDispatcher.forward()
On the server side, when using forward(), the Servlet engine passes the HTTP request from the current Servlet or JSP to another Servlet, JSP or plain HTML file, ie your form is submitted to a.jsp, used in a.jsp When forward() is redirected to b.jsp, all the information submitted by form is available in b.jsp, and the parameters are automatically passed. However, forward() cannot be redirected to a jsp file with a frame. It can be redirected to an html file with a frame. At the same time, forward() can no longer be passed with parameters, such as servlet?name=tom. This is not possible, you can pass the response within the program. .setAttribute("name",name) to pass to the next page.
The URL of the browser address bar does not change after the redirect.
Usually used in Servlets, not in jsp.
(2) response.sendRedirect()
Working on the user's browser, sengRedirect() can pass parameters, such as servlet?name=tom to the next page, and it can be redirected to a different host, sendRedirect() can be redirected to jsp with frame file.
After redirecting, the URL of the redirect page will appear on the browser address bar.
The above two modes of code are shown in Figure 7.
Since response is an implicit object in the jsp page, relocation can be directly implemented in the jsp page using response.sendRedirect().
note:
1. When using response.sendRedirect(), there is no HTML output in front.
This is not absolute. There is no HTML output. It means that no HTML can be sent to the browser. In fact, the current Server has a cache mechanism, usually in 8K (JSP SERVER), which means that unless you close the cache, or you use out.flush () to force a refresh, then there is a small amount of HTML output before using sendRedirect It is also allowed.
After 2.response.sendRedirect(), it should be followed by a return
We already know that response.sendRedirect() is redirected through the browser, so the actual action will only be available after the page has been processed. Since you have already turned, what is the meaning of the output later? And it is possible that the steering output will fail due to the subsequent output.
Comparison:
1.Dispatcher.forward() is the steering of the control in the container. The redirected address will not appear in the address bar of the client browser.
2.response.sendRedirect() is a complete jump, the browser will get the address of the jump, and resend the request link. In this way, you can see the link address after the jump from the address bar of the browser.
The former is more efficient, and when the former can meet the needs, try to use the RequestDispatcher.forward() method.
Note: In some cases, such as the need to jump to a resource on another server, you must use the HttpServletResponse.sendRedirect() method.
(3)<jsp:forward page=""/>
The underlying part of it is implemented by RequestDispatcher, so it has the imprint of the RequestDispatcher.forward() method.
If there is a lot of output before, the previous output has made the buffer full and will be automatically output to the client, then the statement will not work, so you should pay special attention.
Also note that it does not change the browser address, and refreshing will result in duplicate submissions.
The test code is as follows:
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<jsp:forward page="UpdateServlet">
<jsp:param value="list" name="action" />
</jsp:forward>
</body>
</html>
(4) modify the Location attribute of the HTTP header to redirect
The page is redirected by setting the address bar directly.
The index page 1.jsp code is as follows:
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<%
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
String newLocn = "2.jsp";
response.setHeader("Location",newLocn);
%>
</body>
</html>
The turn page 2.jsp code is as follows:
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
Jump to this page from 1.jsp
</body>
</html>
(5) JSP realizes automatic redirection to another page after staying on a certain page for a few seconds
In the jsp file, the following code:
<meta http-equiv="refresh" content="5; url=target.jsp"
The implication is that the page being viewed after 5 seconds will automatically become the target.jsp page. 5 in the code is the delay time for the refresh, in seconds. Target.jsp is the target page you want to turn to. If it is this page, it will automatically refresh this page.
As can be seen from the above, you can use setHeader to achieve a certain page to stay for a few seconds, then automatically redirect to another page.
The index page 5_1.jsp code is as follows:
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<%
String content = "5; url=5_2.jsp";
response.setHeader("REFRESH",content);
%>
5_1.jsp page, after 5 seconds, turn to the 5_2.jsp page.
</body>
</html>
Turn the page 5_2.jsp code as follows
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
Page to go from 5_1.jsp
</body>
</html>