请求重定向与请求转发
来源:互联网 发布:html5程序员 表白网页 编辑:程序博客网 时间:2024/06/05 09:01
用sendRedirect方法实现请求重定向
.sendRedirect方法用于生成302响应码和Location响应头,从而通知客户端去重新访问Location响应头中指定的URL,其完整的定义语法如下:
public void sendRedirect(java.lang.String location)
throws java.io.IOException
.使用下面两条语句也能完成response.sendRedirect(url)语句所完成的功能:
response.setStatus(response.SC_MOVED_TEMPORARILY );
response.setHeader ("Location", url);
.sendRedirect 方法不仅可以重定向到当前应用程序中的其他资源,它还可以重定向到同一个站点上的其他应用程序中的资源,甚至是使用绝对URL重定向到其他站点的资源。
.如果传递给sendRedirect 方法的相对URL以“/”开头,则是相对于整个WEB站点的根目录,而不是相对于当前WEB应用程序的根目录。
请求重定向与请求转发的比较
.RequestDispatcher.forward方法只能将请求转发给同一个WEB应用中的组件;而HttpServletResponse.sendRedirect 方法还可以重定向到同一个站点上的其他应用程序中的资源,甚至是使用绝对URL重定向到其他站点的资源。
.如果传递给HttpServletResponse.sendRedirect 方法的相对URL以“/”开头,它是相对于整个WEB站点的根目录;如果创建RequestDispatcher对象时指定的相对URL以“/”开头,它是相对于当前WEB应用程序的根目录。
.调用HttpServletResponse.sendRedirect方法重定向的访问过程结束后,浏览器地址栏中显示的URL会发生改变,由初始的URL地址变成重定向的目标URL;调用RequestDispatcher.forward 方法的请求转发过程结束后,浏览器地址栏保持初始的URL地址不变。
.HttpServletResponse.sendRedirect方法对浏览器的请求直接作出响应,响应的结果就是告诉浏览器去重新发出对另外一个URL的访问请求;
.RequestDispatcher.forward方法在服务器端内部将请求转发给另外一个资源,浏览器只知道发出了请求并得到了响应结果,并不知道在服务器程序内部发生了转发行为。
.RequestDispatcher.forward方法的调用者与被调用者之间共享相同的request对象和response对象,它们属于同一个访问请求和响应过程;而HttpServletResponse.sendRedirect方法调用者与被调用者使用各自的request对象和response对象,它们属于两个独立的访问请求和响应过程。
.无论是RequestDispatcher.forward方法,还是HttpServletResponse.sendRedirect方法,在调用它们之前,都不能有内容已经被实际输出到了客户端。如果缓冲区中已经有了一些内容,这些内容将被从缓冲区中清除。
缺省Servlet缓存问题的模拟程序(1)
public class ReportServlet extends HttpServlet
{
private int count = 0;
public void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
//禁止浏览器缓存
response.setDateHeader("Expires",0);
response.setHeader("Cache-Control","no-cache");
response.setHeader("Pragma","no-cache");
String department = request.getParameter("dept");
synchronized(this)
{
System.out.println(++count + ":" + department);
String path = getServletContext().getRealPath("/WEB-INF/report.html");
ReportTool rt = new ReportTool();
rt.setCount(count);
rt.setDepartment(department);
rt.createReportHTML(path);
RequestDispatcher rd = getServletContext().getRequestDispatcher(
"/WEB-INF/report.html");
rd.forward(request,response);
}
}
}
缺省Servlet缓存问题的模拟程序(2)
public class ReportTool
{
private int count = 0;
private String department = "";
public void setCount(int count)
{
this.count = count;
}
public void setDepartment(String department)
{
this.department = department;
}
public void createReportHTML(String path) throws IOException
{
FileOutputStream fos = new FileOutputStream(path);
OutputStreamWriter ops = new OutputStreamWriter(fos,"UTF-8");
ops.write(String.valueOf(count) + ":" + department + "的统计信息");
ops.close();
}
}
例子:
SubmitTest.java
package com.hbsi.csdn;
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 SubmitTest extends HttpServlet {
/**
* Constructor of the object.
*/
public SubmitTest() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-Cache");
response.setHeader("Pragma", "no-Cache");
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
String username=request.getParameter("username");
String pass=request.getParameter("password");
String sex=request.getParameter("sex");
String lover[]=request.getParameterValues("lover");
String temp="";
for(int i=0;i<lover.length;i++){
temp=temp+lover[i];
}
out.println("用户名:"+new String(username.getBytes("ISO8859-1"),"utf-8")+"<br>");
out.println("密码:"+pass+"<br>");
out.println("性别:"+sex+"<br>");
out.println("爱好是:"+temp+"<br>");
out.println("出生年月:"+request.getParameter("year")+"年"+request.getParameter("month")+"月");
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
Submit.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Submit.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<meta http-equiv="Expires" content="0">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Pragma" content="no-cache">
</head>
<body>
<form id="form1" name="form1" method="post" action="servlet/ST">
<p>用户名:
<label for="username"></label>
<input type="text" name="username" id="username" />
</p>
<p>密码:
<label for="password"></label>
<input type="password" name="password" id="password" />
</p>
<p>性别:
<input type="radio" name="sex" id="radio" value="男" />
男
<label for="sex"></label>
<input type="radio" name="sex" id="radio2" value="女" />
女
<label for="sex"></label>
</p>
<p>爱好:
<input name="lover" type="checkbox" id="lover" value="听歌" />
<label for="lover"></label>
听歌
<input name="lover" type="checkbox" id="lover" value="eat" />
<label for="lover"></label>
吃好吃的
<input name="lover" type="checkbox" id="lover" value="look" />
<label for="lover"></label>
看书</p>
<p>出生日期:
<label for="year"></label>
<select name="year" id="year">
<option value="1990">1990年</option>
<option value="1991">1991年</option>
<option value="1992">1992年</option>
</select>
年
<label for="month"></label>
<select name="month" size="5" id="month">
<option value="1">1月</option>
<option value="2">2月</option>
<option value="3">3月</option>
</select>
月</p>
<p>
<input type="submit" name="button" id="button" value="提交" />
<input type="reset" name="button2" id="button2" value="重置" />
</p>
</form>
</body>
</html>
- 重定向与请求转发
- 请求转发与重定向
- 请求转发与重定向
- 请求转发与重定向!!*
- 重定向与请求转发
- 重定向与请求转发
- 重定向与请求转发
- 重定向与请求转发
- 请求转发与重定向
- 请求转发与重定向
- 请求转发,与重定向
- 请求转发与重定向
- 重定向与请求转发
- 请求转发与重定向
- 请求转发与重定向
- 请求转发与重定向
- 重定向与请求转发
- 请求转发与重定向
- iOS平台XML解析类库对比概述 (一)
- XML Schema
- VB FSO.GetSpecialFolder 参数说明
- 你们有没有在一起:伤感日志
- HTML <input> 标签的 type 属性
- 请求重定向与请求转发
- Messagebox.Show()常用参数
- 用C++ ulxmlrpcpp 做服务端,PHP xmlrpc 做客户端,实现前后xml通信
- 触屏网页设计初探1
- LINUX的日子
- 算法参考
- 解决ASP中的Response.Flush不起作用的办法
- run placement fails for object ".far", size 0x100b3c (page 0). Available
- SQL语言总结