JavaWeb核心技术系列教程(13)——请求重定向


C语言自学完备手册(33篇)

Android多分辨率适配框架

JavaWeb核心技术系列教程

HTML5前端开发实战系列教程

MySQL数据库实操教程(35篇图文版)

推翻自己和过往——自定义View系列教程(10篇)

走出思维困境,踏上精进之路——Android开发进阶精华录

讲给Android程序员看的前端系列教程(40集免费视频教程+源码)


版权声明

  • 本文原创作者:谷哥的小弟
  • 作者博客地址:http://blog.csdn.net/lfdfhl

请求重定向概述

有时候,客户端发起请求;但是,服务端Servlet可能无法完成全部工作。这时, 我们需要使用请求重定向来完成后续的工作。所谓请求重定向,指的是Web服务器接收到客户端的请求后让客户端重新发送指向其它资源的请求。

为了实现请求重定向,在HttpServletResponse接口中定义了sendRedirect( ) 方法。该方法用于生成302响应码和Location响应头,从而通知客户端重新访问 Location响应头中指定的URL。

图示如下:
JavaWeb核心技术系列教程(13)——请求重定向_第1张图片

请求重定向示例

示例描述

用户登录时如果用户名为lucy,密码为123456则登录成功并跳转至welcome页面;否则跳转至登录页面。

login页面


<html>
	<head>
		<meta charset="UTF-8">
		<title>logintitle>
	head>
	<body>
	    <h2 style="color: red;">本文作者:谷哥的小弟h2>
		<h2 style="color: red;">博客地址:http://blog.csdn.net/lfdfhlh2>
		<form action="/Redirect01/loginServlet" method="post">
			用 名: <input type="text" name="username" /><br><br>
			密 码: <input type="password" name="password"/><br><br>
			<input type="submit" value="登录" />
		form>
	body>
html>

JavaWeb核心技术系列教程(13)——请求重定向_第2张图片

welcome页面




<html>
	<head>
		<meta charset="utf-8">
		<title>successtitle>
	head>
	<body>
	    <h2 style="color: red;">本文作者:谷哥的小弟h2>
		<h2 style="color: red;">博客地址:http://blog.csdn.net/lfdfhlh2>
		<h2 align="center">登陆成功,欢迎来访h2>
	body>
html>

JavaWeb核心技术系列教程(13)——请求重定向_第3张图片

LoginServlet

package cn.com;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 本文作者:谷哥的小弟 
 * 博客地址:http://blog.csdn.net/lfdfhl
 * 
 * HttpServletResponse示例: 
 * 请求重定向
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 4008184113098539992L;
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		String username = request.getParameter("username").trim();
		String password = request.getParameter("password").trim();
		if(username.equals("lucy")&&password.equals("123456")) {
			//request.getContextPath()为:/Redirect01
			System.out.println(request.getContextPath());
			//登录成功则重定向到welcome页面
			response.sendRedirect(request.getContextPath()+"/welcome.html");
		}else {
			//登录失败则重定向到login页面
			response.sendRedirect(request.getContextPath()+"/login.html");
		}
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
}

配置文件web.xml


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Redirect01display-name>
  <welcome-file-list>
    <welcome-file>index.htmlwelcome-file>
    <welcome-file>index.htmwelcome-file>
    <welcome-file>index.jspwelcome-file>
    <welcome-file>default.htmlwelcome-file>
    <welcome-file>default.htmwelcome-file>
    <welcome-file>default.jspwelcome-file>
  welcome-file-list>
  <servlet>
    <servlet-name>loginServletservlet-name>
    <servlet-class>cn.com.LoginServletservlet-class>
  servlet>
  <servlet-mapping>
    <servlet-name>loginServletservlet-name>
    <url-pattern>/loginServleturl-pattern>
  servlet-mapping>
web-app>

你可能感兴趣的:(JavaWeb核心技术系列教程)