java实现请求重定向

servletdemo7代码:

package com.haidi8.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class servletdemo7 extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("A:我有一个请求要处理");
		System.out.println("B:你的请求我处理不了,已为你转其他应用处理");
		
		//response.sendRedirect("/Httpservletresponde/servletdemo8");//实现效果与下面两行代码相同
		response.setStatus(302);//告诉客户端要重新定向新的资源 
		response.setHeader("location", "/Httpservletresponde/servletdemo8");//告诉浏览器要访问的资源路经
		System.out.println("A:正在转到其他应用处理中");
		
		
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		
	}

}

servletdemo8.java代码:

package com.haidi8.servlet;

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 servletdemo8 extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("C:你的请求我能处理");

		
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		
	}

}

web.xml代码:



  
  
    servletdemo7
    com.haidi8.servlet.servletdemo7
  
  
    servletdemo8
    com.haidi8.servlet.servletdemo8
  
  
    servletdemo7
    /servletdemo7
  
  
    servletdemo8
    /servletdemo8
  	
  
    index.jsp
  

运行效果:

访问servletdemo7后跳转到servletdemo8

 

java实现请求重定向_第1张图片

 

你可能感兴趣的:(web学习小结,java,tomcat,java-ee,servlet)