Servlet(二)——客户端跳转和服务器跳转

目录

1.客户端跳转

2.服务器跳转


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可以取到值!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值