Servlet的几种跳转
来源:互联网 发布:男女对唱的网络歌曲 编辑:程序博客网 时间:2024/05/20 03:48
Servlet:
当然,在servlet中,一般跳转都发生在doGet, doPost等方法里面。
一、原理
1) redirect 方式
response.sendRedirect("/a.jsp");
页面的路径是相对路径。sendRedirect可以将页面跳转到任何页面,不一定局限于本web应用中,如:
response.sendRedirect("http://www.ycul.com");
跳转后浏览器地址栏变化。
这种方式要传值出去的话,只能在url中带parameter或者放在session中,无法使用request.setAttribute来传递。
{
response.setContentType("text/html; charset=UTF-8");
response.sendRedirect("/index.jsp");
}
2) forward方式
RequestDispatcher dispatcher = request.getRequestDispatcher("/a.jsp");
dispatcher .forward(request, response);
页面的路径是相对路径。forward方式只能跳转到本web应用中的页面上。
跳转后浏览器地址栏不会变化。
使用这种方式跳转,传值可以使用三种方法:url中带parameter,session,request.setAttribute
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
response.setContentType("text/html; charset=UTF-8");
ServletContext sc = getServletContext();
RequestDispatcher rd = null;
rd = sc.getRequestDispatcher("/index.jsp");
rd.forward(request, response);
}
JSP:
1) response.sendRedirect();
和servlet的response.sendRedirect()方式一样。
此语句前不允许有out.flush(),如果有,会有异常:
java.lang.IllegalStateException: Can't sendRedirect() after data has committed to the client.
at com.caucho.server.connection.AbstractHttpResponse.sendRedirect(AbstractHttpResponse.java:558)
...
跳转后浏览器地址栏变化
如果要跳到不同主机下,跳转后,此语句后面的语句会继续执行,如同新开了线程,但是对response的操作已经无意义了;
如果要跳到相同主机下,此语句后面的语句执行完成后才会跳转;
2) response.setHeader("Location","");
此语句前不允许有out.flush(),如果有,页面不会跳转。
跳转后浏览器地址栏变化
此语句后面的语句执行完成后才会跳转
3) <jsp:forward page="" />
此语句前不允许有out.flush(),如果有,会有异常:
java.lang.IllegalStateException: forward() not allowed after buffer has committed.
at com.caucho.server.webapp.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:134)
at com.caucho.server.webapp.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:101)
at com.caucho.jsp.PageContextImpl.forward(PageContextImpl.java:836)
...
跳转后浏览器地址栏不变,但是只能跳到当前主机下
此语句后面的语句执行完成后才会跳转
1. 转向(Forward)
转向(forward)是通过RequestDispatcher对象的forward(HttpServletRequest request, HttpServletResponse response)来实现的。示例如下:
RequestDispatcher dispatcher = request.getRequestDispatcher(
"/servlet/LifeCycleServlet"
);
dispatcher.forward(request, response);
getRequestDispatcher()方法的参数必须以“/”开始,“/”表示本Web应用程序的根目录。如上例中,
表示要跳转的地址为http://localhost:8080/servlet/servlet/LifeCycleServlet。
forward是最常用的方式,在Structs等MVC框架中,都是用Servlet来处理用户请求,把结果通过request.setAttribute()放到request中,
然后forward到JSP中显示。
当执行forward方法时,不能有任何输出到达客户端,否则会抛出异常,也就是说,在forward之前,不要使用out.println()语句向客户端输出。
public
void
doGet(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
String destination = request.getParameter(
"destination"
);
if
(
"file"
.equals(destination)){
RequestDispatcher d = request.getRequestDispatcher(
"/WEB-INF/web.xml"
);
d.forward(request, response);
}
else
if
(
"jsp"
.equals(destination)){
request.setAttribute(
"date"
,
new
Date());
//attributes are reset between requests.
RequestDispatcher dispatcher = request.getRequestDispatcher(
"/forward.jsp"
);
dispatcher.forward(request, response);
}
else
if
(
"servlet"
.equals(destination)){
RequestDispatcher disp = request.getRequestDispatcher(
"/servlet/LifeCycleServlet"
);
disp.forward(request, response);
}
else
{
response.setCharacterEncoding(
"UTF-8"
);
response.getWriter().println(
"缺少参数。用法:"
+request.getRequestURI()+
"?destination=jsp或者file或者servlet"
);
}
}
2. 重定向(Redirect)
重定向是通过服务器端返回状态码来实现的。301,302都表示重定向,区别是301表示永久性重定向,302表示临时性重定向。通过sendRedirect(String location)就可以实现重定向,下面是例子。本例子主要实现了Servlet来实现文件下载并统计下载次数。要下载的文件以及下载次数都保存在一个Map中。主要思路是:首先加载页面表单,当用户点击下载链接时,客户端发起请求,运行doGet里的if判断,实现重定向。
重定向和跳转的区别:跳转是在服务器端实现的,客户端浏览器并不知道该浏览动作,而使用Redict跳转时,跳转是在客户端实现的,也就是说客户端浏览器实际上请求了2次服务器。
public
class
RedictServlet
extends
HttpServlet {
Map<String,Integer> map =
new
HashMap<String,Integer>();
//new一个Map
public
void
init()
throws
ServletException {
//放在init中,加载servlet时运行此方法,把文件内容放到map中去
map.put(
"/download/setup.exe"
,
0
);
map.put(
"/download/application.zip"
,
0
);
map.put(
"/download/01.mp3"
,
0
);
}
public
void
doGet(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
String filename = request.getParameter(
"filename"
);
if
(filename!=
null
){
int
hit = map.get(filename);
//取下载次数
map.put(filename, ++hit);
//下载次数加1后保存
response.sendRedirect(request.getContextPath()+filename);
//重定向到文件
}
else
{
response.setCharacterEncoding(
"UTF-8"
);
PrintWriter out = response.getWriter();
response.setContentType(
"text/html"
);
out.println(
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"
);
out.println(
"<HTML>"
);
out.println(
" <HEAD><TITLE>文件下载</TITLE></HEAD>"
);
out.println(
" <link rel='stylesheet' type='text/css' href='../css/style.css'>"
);
out.println(
" <BODY><br/>"
);
out.println(
"<fieldset align=center style=width:90%><legend>文件下载</legend>"
);
//绘制页面表单
out.println(
"<table width=100%>"
);
out.println(
" <tr>"
);
out.println(
" <td><b>文件名"
+
"</b></td>"
);
out.println(
" <td><b>下载次数</b></td>"
);
out.println(
" <td><b>下载</b></td>"
);
out.println(
" </tr>"
);
for
(Entry<String,Integer> entry: map.entrySet()){
//遍历map的方法
out.println(
"<tr>"
);
out.println(
" <td>"
+entry.getKey()+
"</td>"
);
out.println(
" <td>"
+entry.getValue()+
"</td>"
);
out.println(
" <td><a href = '"
+request.getRequestURI()+
"?filename="
+entry.getKey()+
"'target = '_blank' onclick ='location = location;'>下载</a></td>"
);
//target='_blank'目标地址在无标题的新页面中打开。onclick ='location = location;'页面刷新
out.println(
"</tr>"
);
}
out.println(
"</table>"
);
out.println(
" </legend>"
);
out.println(
" </BODY>"
);
out.println(
"</HTML>"
);
out.flush();
out.close();
}
}
public
void
destroy() {
super
.destroy();
// Just puts "destroy" string in log
// Put your code here
map =
null
;
}
}
- Servlet的几种跳转
- Servlet的几种跳转
- Servlet的几种跳转
- Servlet的几种跳转
- servlet几种跳转
- Servlet几种页面跳转的区别
- Servlet几种页面跳转的区别
- Servlet和jsp的几种跳转
- servlet跳转页面的几种方法
- servlet跳转页面的几种方法
- servlet跳转页面的几种方法
- Servlet几种页面跳转的区别
- Servlet几种页面跳转的区别
- servlet跳转页面的几种方法
- servlet跳转页面的几种方法
- servlet跳转页面的几种方法
- servlet跳转页面的几种方法
- servlet跳转页面的几种方法
- 陶醉在音乐的国度,我如痴如醉
- 12-12-09记录
- Hdu 1423 Greatest Common Increasing Subsequence
- 杨辉三角
- iOS开发之使用lipo命令制作模拟器与真机通用静态库
- Servlet的几种跳转
- Lua 学习开篇
- yii url 重新
- Lua简介:开发《魔兽争霸》和《愤怒的小鸟》的语言
- VBA操作文件四大方法之之四-API函数
- VBA操作文件四大方法之一--Excel对象方法
- Lua基础 初识Lua
- C# 操作Excel
- java实现简单的数字运算以及dos命令运行Java程序