通过web_servlet04 学习转发和重定向
1、请求的转发
1)实现
request.getRequestDispatcher("/regist_error.html").forward(request, response);
2)特点
在服务器内部完成,用户感知不到
浏览器地址栏不变
整个过程浏览器只发出了一个请求
目标资源可以在WEB-INF目录下
2、请求的重定向
1)实现
response.sendRedirect("./regist_success.html");
response.sendRedirect("/MyNews/regist_success.html");
response.sendRedirect(request.getContextPath() + "/MyNews/regist_success.html");
2)特点
服务器以302状态码通知浏览器访问新地址
浏览器地址栏改变
整个过程浏览器发出两次请求
目标资源不可以在WEB-INF目录下
3、案例实操
1)LoginServlet
package com.alex.web.servlet;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
// 声明周期方法
// 生命周期:表示对象从创建、初始化、使用、销毁过程
// 创建对象
public LoginServlet() {
System.out.println("LoginServlet.....");
}
// 销毁对象时使用
@Override
public void destroy() {
System.out.println("destroy ......");
}
// 初始化对象
// 初始化方法只执行一次
@Override
public void init(ServletConfig servletconfig) throws ServletException {
System.out.println("init ... ...");
}
// 处理请求使用的
@Override
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
// 设置请求字符编码,解决客户端提交数据是中文乱码问题
// 设置必须在request.getParameter(" ") 方法前调用,否则不起作用
// 只针对post请求方式有效,对get请求方式无效
// 如果解决Get请求乱码问题,需要在Tomcat/conf/server.xml中设置字符编码
// <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000"
// redirectPort="8443" URIEncoding="UTF-8"/>
request.setCharacterEncoding("UTF-8");
// 如何获取客户端浏览器的数据?
// 一个参数名称带一个值,例如:文本框、密码框
String username = request.getParameter("username");
String passwd = request.getParameter("passwd");
System.out.println("username:" + username);
System.out.println("passwd:" + passwd);
// 服务器端告诉客户端浏览器,返回的是text/html类型的数据
// 设置响应内容类型及编码,需要在response.getWriter().println("")方法调用前设置,否则不起作用
response.setContentType("text/html;charset=UTF-8");
if ("admin".equals(username) && "123".equals(passwd)) {
// 转发请求
// 转发请求资源的路径,以斜杠开头,表示从当前应用程序的根路径来查找资源
// XML配置文件中以斜杠开头,也表示从当前应用程序的根路径来查找资源
request.getRequestDispatcher("/success.jsp").forward(request, response);
} else {
// 转换为子接口,子接口内容更丰富一些
HttpServletRequest httpServletRequest = (HttpServletRequest)request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
//httpServletRequest.getContextPath() 方法获取当前应用程序的上下文路径
// URL : http://localhost:8080/web_servlet04/login.jsp
// URI : /web_servlet04/login.jsp
// getContextPath() : /web_servlet04
httpServletResponse.sendRedirect(httpServletRequest.getContextPath() + "/failure.jsp");
}
}
}
2)web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>web_servlet04</display-name>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.alex.web.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
3)login.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>
Get
<form action="LoginServlet" method="get">
用户名:<input type="text" name="username" />
<br/>
密码:<input type="text" name="passwd" />
<br/>
<!-- 访问AutoByEclipseServlet中的doGet方法 -->
<input type="submit" value="发送GET请求" />
</form>
<br />
<br />
POST
<form action="LoginServlet" method="post">
用户名<input type="text" name="username" />
<br/>
密码:<input type="text" name="passwd" />
<br/>
<!-- 访问AutoByEclipseServlet中的doPost方法 -->
<input type="submit" value="发送POST请求" />
</form>
</body>
</html>
4)failure.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>
failure
</body>
</html>
5)success.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>
success
</body>
</html>
网友评论