1、先上结论
- 重定向会改变地址栏的内容,转发不会
- 重定向是两次请求的过程,而转发只有一次.由此可见重定向是由浏览器完成的,转发是服务器内部完成的
- 转发由于是一次过程,所以request和response能在Servlet和jsp中共享,可以在里面添加Attribute,而转发是两次过程,两次之间的request和response是独立的
- 转发和重定向代码中的path路径的写法也不一样,转发中的/代表服务器的web站点目录,重定向/代表webapps这个目录
2、举例说明
(1)请求转发
??? ?
RequestDispatcherServlet.java?
package com.test.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class RequestDispatcherServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//请求转发 转发到helloServlet (注意路径的写法)
this.getServletContext().getRequestDispatcher("/hello").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
HelloServlet.java
package com.test.servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter writer = resp.getWriter();
writer.print("Hello,Servlet");
}
@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
执行结果:
?(2)重定向
? ? ? ? 做一个简单登录的跳转
????????index.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=GB2312" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>登录页面</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/login" method="get">
用户名:<input type="text" name="name"> <br>
密码:<input type="password" name="password"> <br>
<input type="submit">
</form>
</body>
</html>
登录的servlet:
LoginServlet.java
package com.test.servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name = req.getParameter("name");
ServletContext context = this.getServletContext();
context.setAttribute("name",name);
System.out.println((String) context.getAttribute("name"));
// 注意这里重定向的写法,servelet_01_war 就是webapp下面的名字
resp.sendRedirect("/servlet_01_war/index");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
登录后要跳转的servlet:
IndexServlet.java
package com.test.servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class IndexServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置编码格式
resp.setContentType("text/html");
resp.setCharacterEncoding("UTF-8");
ServletContext context = this.getServletContext();
resp.getWriter().print("登录成功,"+ (String) context.getAttribute("name"));
}
@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
输出结果:
? ? ? ? 首先是登录页面:
????????登录后:
?上面四个servlet类的配置web.xml
<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--注册Servlet-->
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.test.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>rd</servlet-name>
<servlet-class>com.test.servlet.RequestDispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>rd</servlet-name>
<url-pattern>/rd</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.test.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>index</servlet-name>
<servlet-class>com.test.servlet.IndexServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>index</servlet-name>
<url-pattern>/index</url-pattern>
</servlet-mapping>
</web-app>
|