1.request.getRequestDispatcher()是请求转发,前后页面共享一个request ;
response.sendRedirect()是重新定向,前后页面不是一个request。
2.RequestDispatcher.forward()是在服务器端运行;
HttpServletResponse.sendRedirect()是通过向客户浏览器发送命令来完成.
所以RequestDispatcher.forward()对于浏览器来说是“透明的”;
而HttpServletResponse.sendRedirect()则不是。
3.ServletContext.getRequestDispatcher(String url)中的url只能使用绝对路径; 而ServletRequest.getRequestDispatcher(String url)中的url可以使用相对路径。因为ServletRequest具有相对路径的概念;而ServletContext对象无次概念。
RequestDispatcher对象从客户端获取请求request,并把它们传递给服务器上的servlet,html或jsp。它有两个方法:
1.void forward(ServletRequest request,ServletResponse response)
用来传递request的,可以一个Servlet接收request请求,另一个Servlet用这个request请 求来产生response。request传递的请求,response是客户端返回的信息。forward要在response到达客户端之前调用,也 就是 before response body output has been flushed。如果不是的话,它会报出异常。
2.void include(ServletRequest request,ServletResponse response)
用来记录保留request和response,以后不能再修改response里表示状态的信息。
如果需要把请求转移到另外一个Web App中的某个地址,可以按下面的做法:
1. 获得另外一个Web App的ServletConext对象(currentServletContext.getContext(uripath)).
2. 调用ServletContext.getRequestDispatcher(String url)方法。
eg:ServletContext.getRequestDispatcher(“smserror.jsp”).forward(request,response);
代码实例:
index.jsp:
复制代码 代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
My JSP 'index.jsp' starting page
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3" />
<meta http-equiv="description" content="This is my page" />
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<form action="servlet/session" method="post">
用户名:<input type="text" name="username" />
密码:<input type="password" name="password" />
<input type="submit" />
</form>
session.java:
复制代码 代码如下:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class session extends HttpServlet {
/**
* Constructor of the object.
*/
public session() {
super();
}
/**
* Destruction of the servlet.
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet.
*
* 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 {
doPost(request, response);
}
/**
* The doPost method of the servlet.
*
* 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 {
String username = "";
String password = "";
username = request.getParameter("username");
password = request.getParameter("password");
HttpSession session = request.getSession();
session.setAttribute("username", username);
session.setAttribute("password", password);
request.setAttribute("name", username);
request.setAttribute("pwd", password);
RequestDispatcher dis = request.getRequestDispatcher("/getsession.jsp");
dis.forward(request, response);
/*
response.sendRedirect("http://localhost:8080/sessiontest/getsession.jsp");
*/
//这个路径必须是这样写,而不能像上面的request.getRequestDispatcher那样使用相对路径
// 而且要是使用response.sendRedirect的话在下面的session.jsp中不能通过request.getAttribute来获取request对象
//因为前后使用的不是同一个request,但是session可以,因为session会一直存在直到用户关闭浏览器
}
/**
* Initialization of the servlet.
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
getsession.jsp:
复制代码 代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
My JSP 'getsession.jsp' starting page
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3" />
<meta http-equiv="description" content="This is my page" />
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<% out.print(""); String username = (String)session.getAttribute("username");
String password = (String)session.getAttribute("password");
String name = (String)request.getAttribute("name");
String pwd = (String)request.getAttribute("pwd");
out.println("username " + username + " password " +password);
//如果上面是使用response.sendRedirect的话就不能获取到name和pwd
out.println("name " + name + "pwd "+ pwd);
%>
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]