生活随笔
收集整理的這篇文章主要介紹了
11. jsp与servlet之间页面跳转及参数传递实例
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.從一個jsp頁面跳轉(zhuǎn)到另一個jsp頁面時的參數(shù)傳遞
? ? (1)使用request對象獲取客戶端提交的信息
login.jsp頁面代碼如下:
[java] view plain
copy <%@?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>?? ? ?
getinfo.jsp頁面代碼如下:
[java] view plain
copy <%@?page?language="java"?import="java.util.*"?pageEncoding="GB2312"%>????<!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">??<html>??????<body>??????????<p>獲取文本框提交的信息:??????????<%??????????????String?textContent?=?request.getParameter("name");???????????%>???????????<br/>???????????<%=textContent%>?????????????????????</p>獲取按鈕的名字:???????????<%??????????????String?buttonName?=?request.getParameter("submit");????????????%>???????????<br/>???????????<%=buttonName%>????????????????</body>??</html>?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? (2)使用session對象獲取從客戶端提交的信息
? ? session1.jsp頁面代碼如下:
[java] view plain
copy <%@?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">??????????????請輸入您的姓名:??????????????<input?type="text"?name="thename">??????????????<input?type="submit"?value="submit">??????????</form>??????</body>??</html>?? ? ? session2.jsp通過request對象提取session1.jsp表單中的thename值,并將其保存到session對象中,session2.jsp代碼如下:
[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?name?=?request.getParameter("thename");??????????????session.setAttribute("thename",name);???????????%>???????????您的姓名是:<%=name%>???????????<p>??????????<form?action="session3.jsp?pwd=123456"?method="post">??????????????您喜歡吃什么???????????????<input?type="text"?name="food"/>??????????????<p>??????????????<input?type="submit"?value="submit">??????????</form>??????????????</body>??</html>?? ?
? session3.jsp的主要任務(wù)是顯示結(jié)果,它從session對象提取thename的值并顯示它,以證明在多個頁面中可以通過session對象存儲和維持信息,session3.jsp代碼如下:
[java] view plain
copy <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");???????????%>???????????您的姓名是:<%=name%>???????????<p>???????????您喜歡吃:<%=food%>???????????<p>???????????您的密碼是:<%=pwd%>??????</body>??</html></pre>??<pre></pre>??<pre></pre>??<pre></pre>??<pre></pre>??<pre></pre>??<pre></pre>??<pre></pre>??
? ? 程序運行結(jié)果如圖2所示:
? ? (3)利用隱藏域傳遞數(shù)據(jù)
? ? 使用隱藏空間記錄用戶提交日期的hiddenindex.jsp代碼如下:
[java] view plain
copy <%@?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">??????????????<!--?隱藏字段,系統(tǒng)時間,并轉(zhuǎn)換為毫秒數(shù)?-->??????????????<input?type="hidden"?name="time"?value="<%=new?java.util.Date()%>">??????????????<p>??????????????????請輸入您的姓名:<input?type="text"?name="name">??????????????</p>??????????????<input?type="submit"?name="submit"?value="submit">??????????</form>??????</body>??</html>?? ? ? ?顯示隱藏空間信息的hidden.jsp代碼如下:
[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?name?=?request.getParameter("name");??????????????String?date?=?request.getParameter("time");??????????%>??????????<p>您的姓名是:??????????????<%=name?%>??????????</p>??????????<p>提交時的系統(tǒng)時間為:??????????????<%=date?%>??????????</p>??????</body>??</html>?? ? ? 程序運行結(jié)果如下:
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
? ? 使用隱藏域的一個缺點是用戶可通過查看網(wǎng)頁源程序讀到隱藏域的值。
? ? (4)通過超鏈接傳遞數(shù)據(jù)
? ? 建立鏈接到其他分頁的超級鏈接page.jsp代碼如下:
[java] view plain
copy <%@?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%>>到第1頁</a>??????????????????</td>??????????????????<td>??????????????????????<a?href=showpage.jsp?ToPage=<%=PageCount%>>到最后一頁</a>??????????????????</td>??????????????</tr>??????????</table>????????????</body>??</html>?? ? ? 接收顯示分頁showpage.jsp代碼如下:
[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?toPage?=?request.getParameter("ToPage");??????????out.println("第"?+?toPage?+?"頁");??????????%>??????</body>??</html>?? ? ?
運行結(jié)果如下:
? ? 2.jsp頁面傳遞參數(shù)給servlet
? ? (1)使用request對象接收參數(shù)
? ? 新建名為ForwardServlet的Servlet類,代碼如下:
[java] view plain
copy 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?ForwardServlet?extends?HttpServlet??{??????private?static?final?long?serialVersionUID?=?1L;????????????????public?ForwardServlet()??????{??????????super();??????}????????????????????public?void?init()?throws?ServletException??????{}????????????????????public?void?doGet(HttpServletRequest?request,?HttpServletResponse?response)?throws?ServletException,?IOException??????{??????????doPost(request,?response);??????}????????????????????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);??????????????}??????????}??????}????????????????????public?void?destroy()??????{??????????super.destroy();??????}??}?? ? ? 在web.xml中配置ForwardServlet類,代碼如下:
[java] view plain
copy <?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:??????http:????????????<!--?配置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>?? ? ? 新建index.jsp,其中<form>中的action屬性值為Servlet類在web.xml中配置的url,提交方式為post,代碼如下:
[java] view plain
copy <%@?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>用戶名:</td>??????????????????????<td>??????????????????????????<input?type="text"?name="name"/>??????????????????????</td>??????????????????</tr>??????????????????<tr>??????????????????????<td>密碼:</td>??????????????????????<td>??????????????????????????<input?type="password"?name="pwd"/>??????????????????????</td>??????????????????</tr>??????????????????<tr>??????????????????????<td?colspan="2">??????????????????????????<input?type="submit"?value="登錄"/>??????????????????????</td>??????????????????</tr>??????????????</table>??????????</form>??????</body>??</html>?? ? ? 登錄成功頁面success.jsp,代碼如下:
[java] view plain
copy <%@?page?language="java"?import="java.util.*"?pageEncoding="GB2312"%>????<!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">??<html>??????<body>??????????歡迎<%=request.getParameter("name")?%>,登錄成功!??????</body>??</html>?? ? ? 程序的運行結(jié)果圖5所示:
? ? (2)使用session對象,url,form表單傳遞參數(shù)
? ? 在jsp2servlet.jsp中,分別使用request對象,session對象,url,form表單設(shè)置需要傳遞的參數(shù),代碼如下:
[java] view plain
copy <%@?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">單擊此處</a>??????????<form?action="jsp2servlet?action=toServlet"?method="post"?name="form1">??????????????<input?type="text"?name="username"/>??????????????<input?type="submit"?value="submit"/>??????????</form>??????</body>??</html>?? ? ? 在Jsp2Servlet中接收jsp頁面中傳遞的參數(shù)并將其顯示,代碼如下:
[java] view plain
copy 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??{??????????????private?static?final?long?serialVersionUID?=?1L;????????????????public?Jsp2Servlet()??????{??????????super();??????}????????????????????public?void?init()??????{}????????????????????public?void?doGet(HttpServletRequest?request,?HttpServletResponse?response)?throws?ServletException,?IOException??????{??????????doPost(request,?response);??????}????????????????????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("保存到session中的內(nèi)容:?"?+?strSession?+?"<br>");??????????out.println("保存到request中的內(nèi)容:"?+?strRequest?+?"<br>");??????????out.println("超鏈接中傳遞的參數(shù)值為:"?+?strHref?+?"<br>");??????????out.println("form表單中傳遞的action值為:"?+?strForm?+?"<br>");??????????out.println("form表單內(nèi)input標(biāo)簽中username的值:?"?+?username?+?"<br>");??????????out.println("</body>");??????????out.println("</html>");?????????????}????????????????????public?void?destroy()??????{??????????super.destroy();??????}??}?? ? ? 程序運行結(jié)果如圖6所示:
? ? servlet無法獲取request.setAttribute()中的內(nèi)容,因為在jsp2servlet.jsp中提交時,生成的一個新的request,因此無法取到j(luò)sp頁面中request.setAttribute()中設(shè)置的值。
? ? 3.從servlet到j(luò)sp頁面中傳遞參數(shù)
? ? 從servlet到j(luò)sp頁面?zhèn)鬟f參數(shù),可以使用URL,request對象,session對象,新建Servlet2Jsp.java,代碼如下:
[java] view plain
copy 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??{??????????????private?static?final?long?serialVersionUID?=?1L;????????????????public?Servlet2Jsp()??????{??????????super();??????}????????????????????public?void?init()??????{}????????????????????public?void?doGet(HttpServletRequest?request,?HttpServletResponse?response)?throws?ServletException,?IOException??????{??????????doPost(request,?response);??????}????????????????????public?void?doPost(HttpServletRequest?request,?HttpServletResponse?response)?throws?ServletException,?IOException??????{??????????String?url?=?"result.jsp?first=傳遞第一個參數(shù)&second=傳遞第二個參數(shù)";??????????String?strRequest?=?"request傳值";??????????String?strSession?=?"session傳值";??????????request.setAttribute("strRequest",?strRequest);??????????request.getSession().setAttribute("strSession",?strSession);??????????????????????????????????????????????????????????????????request.getRequestDispatcher(url).forward(request,?response);??????}????????????????????public?void?destroy()??????{??????????super.destroy();??????}??}??
? ? 在web.xml中配置該servlet后,新建參數(shù)接收頁result.jsp,代碼如下:
[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>??????????????URL中取得的屬性值為:<%=strURL%>??????????</p>????????????????<p>??????????????request中取得的屬性值為:<%=strRequest%>??????????</p>??????????<p>??????????????session中取得的屬性值為:<%=strSession%>??????????</p>??????</body>??</html>?? ? ? 程序的運行結(jié)果如圖7所示:
? ? 4.從一個servlet到另一個servlet時的參數(shù)傳遞
? ? (1)通過超鏈接和表單傳遞數(shù)據(jù)
? ? 通過超鏈接后面給定要傳遞參數(shù)的值,但長度有限制,通過超鏈接和表單傳遞數(shù)據(jù),使用request.getParamter()方法,代碼如下:
[java] view plain
copy 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??{??????????????private?static?final?long?serialVersionUID?=?1L;????????????????public?ServletA()??????{??????????super();??????}????????????????????public?void?init()??????{}????????????????????public?void?doGet(HttpServletRequest?request,?HttpServletResponse?response)?throws?ServletException,?IOException??????{??????????doPost(request,?response);??????}????????????????????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>");??????????????????????out.println("<a?href='ServletB?name="+name+"'>this?is?a?test</a>");??????????????????????out.println("<form?action='ServletB'?method='post'>");??????????????????out.println("用戶名:");??????????out.println("<input?type='text'?name='username'>");??????????out.println("<input?type='submit'?vlue='提交'>");??????????out.println("</form>");??????????out.println("</body>");??????????out.println("</html>");??????????out.flush();????????}????????????????????public?void?destroy()??????{??????????super.destroy();??????}??}?? ? ? 接收servlet中傳遞參數(shù)的代碼如下:
[java] view plain
copy 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??{??????????????private?static?final?long?serialVersionUID?=?1L;????????????????public?ServletB()??????{??????????super();??????}????????????????????public?void?init()??????{}????????????????????public?void?doGet(HttpServletRequest?request,?HttpServletResponse?response)?throws?ServletException,?IOException??????{??????????doPost(request,?response);??????}????????????????????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("超鏈接傳遞的參數(shù)值為:"?+?name);??????????out.println("form表單中提交的用戶名為:"?+?username);??????????out.println("</body>");??????????out.println("</html>");??????????out.flush();??????}????????????????????public?void?destroy()??????{??????????super.destroy();??????}??}?? ? ? 程序運行結(jié)果如圖8所示:
? ? (2)使用setAttribute()和getAttribute()方法傳遞參數(shù)
? ? 設(shè)置參數(shù)的ServletA代碼如下:
[java] view plain
copy 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??{??????????????private?static?final?long?serialVersionUID?=?1L;????????????????public?ServletA()??????{??????????super();??????}????????????????????public?void?init()??????{}????????????????????public?void?doGet(HttpServletRequest?request,?HttpServletResponse?response)?throws?ServletException,?IOException??????{??????????doPost(request,?response);??????}????????????????????public?void?doPost(HttpServletRequest?request,?HttpServletResponse?response)?throws?ServletException,?IOException??????{??????????????????????String?data?=?"This?is?a?test,?we?need?more?test?for?this";??????????request.setAttribute("strAtt",?data);??????????request.getRequestDispatcher("/ServletB").forward(request,?response);??????}????????????????????public?void?destroy()??????{??????????super.destroy();??????}??}?? ? ? 獲取參數(shù)的ServletB代碼如下:
[java] view plain
copy 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??{??????????????private?static?final?long?serialVersionUID?=?1L;????????????????public?ServletB()??????{??????????super();??????}????????????????????public?void?init()??????{}????????????????????public?void?doGet(HttpServletRequest?request,?HttpServletResponse?response)?throws?ServletException,?IOException??????{??????????doPost(request,?response);??????}????????????????????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()得到的數(shù)據(jù)值為:"?+?strAtt);???????????????out.println("</body>");??????????out.println("</html>");??????????out.flush();??????}????????????????????public?void?destroy()??????{??????????super.destroy();??????}??}?? ? ? 程序的運行結(jié)果如圖9所示:
??
? ? 5.JSP頁面跳轉(zhuǎn)方法
? ? (1)RequestDispatcher.forward()
? ? 在服務(wù)器端起作用,當(dāng)使用forward()時,Servlet engine傳遞HTTP請求從當(dāng)前的Servlet or JSP到另外一個Servlet,JSP或普通HTML文件,即你的form提交至a.jsp,在a.jsp用到了forward()重定向到b.jsp,此時form提交的所有信息在b.jsp都可以獲得,參數(shù)自動傳遞。但forward()無法重定向至有frame的jsp文件,可以重定向至有frame的html文件,同時forward()無法再后面帶參數(shù)傳遞,比如servlet?name=tom,這樣不行,可以程序內(nèi)通過response.setAttribute("name",name)來傳至下一個頁面。
? ? 重定向后瀏覽器地址欄URL不變。
? ? 通常在Servlet中使用,不在jsp中使用。
? ? (2)response.sendRedirect()
? ? 在用戶的瀏覽器端工作,sengRedirect()可以帶參數(shù)傳遞,比如servlet?name=tom傳至下個頁面,同時它可以重定向至不同的主機(jī)上,sendRedirect()可以重定向至有frame的jsp文件。
? ? 重定向后在瀏覽器地址欄上會出現(xiàn)重定向頁面的URL。
? ? 上述兩種方式代碼見圖7。
? ? 由于response是jsp頁面中的隱含對象,故在jsp頁面中可以用response.sendRedirect()直接實現(xiàn)重定位。
? ? 注意:
? ??①.使用response.sendRedirect()時,前面不能有HTML輸出
? ? 這并不是絕對的,不能有HTML輸出其實是指不能有HTML被送到了瀏覽器。事實上現(xiàn)在的Server都有cache機(jī)制,一般在8K(JSP SERVER),這意味著,除非你關(guān)閉了cache,或者你用out.flush()強(qiáng)制刷新,那么在使用sendRedirect之前,有少量的HTML輸出也是允許的。
? ??②.response.sendRedirect()之后,應(yīng)該緊跟一句return
? ? 我們已經(jīng)知道response.sendRedirect()是通過瀏覽器來做轉(zhuǎn)向的,所以只有在頁面處理完成后,才會有實際的動作。既然你已經(jīng)要做轉(zhuǎn)向了,那么后面的輸出還有什么意義呢?而且有可能會因為后面的輸出導(dǎo)致轉(zhuǎn)向失敗。
? ? 比較:
? ??①.Dispatcher.forward()是容器中控制權(quán)的轉(zhuǎn)向,在客戶端瀏覽器地址欄中不會出現(xiàn)轉(zhuǎn)向后的地址
? ??②.response.sendRedirect()則是完全的跳轉(zhuǎn),瀏覽器將會得到跳轉(zhuǎn)的地址,并重新發(fā)送請求鏈接。這樣,從瀏覽器的地址欄中可以看到跳轉(zhuǎn)后的鏈接地址。
? ?前者更加高效,在前者可以滿足需要時,盡量使用RequestDispatcher.forward()方法。
? ? 注:在有些情況下,比如,需要跳轉(zhuǎn)到一個其它服務(wù)器上的資源,則必須使用HttpServletResponse.sendRedirect()方法。
? ? (3)<jsp:forward page=""/>
? ? 它的底層部分是由RequestDispatcher來實現(xiàn)的,因此它帶有RequestDispatcher.forward()方法的印記。
? ? 如果在之前有很多輸出,前面的輸出已使緩沖區(qū)滿,將自動輸出至客戶端,那么該語句將不起作用,這一點應(yīng)該特別注意。
? ? 另外要注意:它不能改變?yōu)g覽器地址,刷新的話會導(dǎo)致重復(fù)提交。
? ? 測試代碼如下:
[java] view plain
copy <%@?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>?? ? ? 程序運行結(jié)果如圖10所示:
? ? (4)修改HTTP header的Location屬性來重定向
? ? 通過設(shè)置直接修改地址欄來實現(xiàn)頁面的重定向。
? ? 索引頁1.jsp代碼如下:
[java] view plain
copy <%@?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>?? ? ? 轉(zhuǎn)向頁2.jsp代碼如下:
[java] view plain
copy <%@?page?language="java"?import="java.util.*"?pageEncoding="GB2312"%>????<!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">??<html>??????<body>??????????從1.jsp跳轉(zhuǎn)至此頁??????</body>??</html>?? ? ? 程序運行結(jié)果如圖11所示:
? ?
? ? (5)JSP中實現(xiàn)在某頁面停留若干秒后,自動重定向到另一頁面
? ? 在jsp文件中,下面的代碼:
[java] view plain
copy <meta?http-equiv="refresh"?content="5;?url=target.jsp"?? ? ? 含義是:在5秒后正在瀏覽的頁面將會自動變?yōu)閠arget.jsp頁。代碼中5為刷新的延遲時間,以秒為單位。target.jsp為你想轉(zhuǎn)向的目標(biāo)頁,若為本頁則自動刷新本頁。
? ? 由上可知,可以通過setHeader來實現(xiàn)某頁面停留若干秒后,自動重定向到另一頁面。
? ? 索引頁5_1.jsp代碼如下:
[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?content?=?"5;?url=5_2.jsp";??????????response.setHeader("REFRESH",content);????????????????%>??????????5_1.jsp頁面,5秒后轉(zhuǎn)向5_2.jsp頁面。??????</body>??</html>?? ? ? 轉(zhuǎn)向頁5_2.jsp代碼如下:
[java] view plain
copy <%@?page?language="java"?import="java.util.*"?pageEncoding="GB2312"%>????<!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">??<html>??????<body>??????????從5_1.jsp轉(zhuǎn)到的頁面??????</body>??</html>?? ? ? 程序運行結(jié)果如下:
總結(jié)
以上是生活随笔為你收集整理的11. jsp与servlet之间页面跳转及参数传递实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。