java实现请求重定向

导读:本篇文章讲解 java实现请求重定向,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

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代码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>
  <servlet>
    <servlet-name>servletdemo7</servlet-name>
    <servlet-class>com.haidi8.servlet.servletdemo7</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>servletdemo8</servlet-name>
    <servlet-class>com.haidi8.servlet.servletdemo8</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>servletdemo7</servlet-name>
    <url-pattern>/servletdemo7</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>servletdemo8</servlet-name>
    <url-pattern>/servletdemo8</url-pattern>
  </servlet-mapping>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

运行效果:

访问servletdemo7后跳转到servletdemo8

java实现请求重定向

java实现请求重定向

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/94178.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!