目录
1.客户端跳转
1,在 Servlet 中获取 session,application
2,客户端跳转 response.sendRedirect("目标地址");
3,服务器跳转:RequestDispatcher rd=request.getRequestDispatcher("目标地址"); rd.forward(request, response);
新建一个RedirectServlet.java类:
其中session用HttpSession定义并用request.getSession()获取;
application用ServletContext定义并用this.getServletContext()获取;
这两个接口要记住!
package com.java.web;
import java.io.IOException;
import javax.servlet.ServletContext;
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 RedirectServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//用户请求doGet时交给doPost去处理
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//给request设置值
request.setAttribute("requestKey", "request值");
//获取session
HttpSession session=request.getSession();
//给session设置值
session.setAttribute("sessionKey", "session值");
//获取application值
ServletContext application=this.getServletContext();
//给application设置值
application.setAttribute("applicationKey", "application值");
//客户端跳转
response.sendRedirect("target.jsp");
}
}
目标target.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<h1>目标地址</h1>
request值:<%= request.getAttribute("requestKey") %><br/>
session值:<%= session.getAttribute("sessionKey") %><br/>
application值:<%= application.getAttribute("applicationKey") %><br/>
</body>
</html>
配置文件web.xml:
运行结果:
可以看到request其实是取不到值的!
2.服务器跳转
1.获取RequestDispatcher接口的forward()方法;
2.RequestDispatcher是通过调用HttpServletRequest对象的getRequestDispatcher()方法得到的。
新建一个ForwardServlet类:
package com.java.web;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
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 ForwardServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//用户请求doGet时交给doPost去处理
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//给request设置值
request.setAttribute("requestKey", "request值");
//获取session
HttpSession session=request.getSession();
//给session设置值
session.setAttribute("sessionKey", "session值");
//获取application值
ServletContext application=this.getServletContext();
//给application设置值
application.setAttribute("applicationKey", "application值");
//服务器跳转
RequestDispatcher rd=request.getRequestDispatcher("target.jsp");
rd.forward(request, response);
}
}
目标target.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<h1>目标地址</h1>
request值:<%= request.getAttribute("requestKey") %><br/>
session值:<%= session.getAttribute("sessionKey") %><br/>
application值:<%= application.getAttribute("applicationKey") %><br/>
</body>
</html>
配置web.xml:
运行结果:
这里request可以取到值!