能否在OneServlet中保存值到请求域中,在另一个TwoServlet中打印出来?
如果是不同的请求,不能取出来。如果是同一次请求是可以取出来的。
在Servlet中实现页面的跳转有两种方式:转发和重定向
由服务器端进行的页面跳转
实现从OneServlet中转发到TwoServlet
package com.itheima.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
与请求域相关的方法
*/
@WebServlet(name = "Demo6OneServlet", urlPatterns = "/one")
public class Demo6OneServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.print("这是One
");
//向请求域中添加键和值
request.setAttribute("name","Jack");
request.setAttribute("age", 20); //整数类型
String name = (String) request.getAttribute("name");
int age = (int) request.getAttribute("age");
//从请求域中取出值
out.print("名字:" + name + "
");
out.print("年龄:" + age );
out.print("
");
//删除其中一个
request.removeAttribute("age");
//从请求域中取出值,直接输出Object对象
out.print("名字:" + request.getAttribute("name") + "
");
out.print("年龄:" + request.getAttribute("age") );
System.out.println("这是one");
//开始转发,参数是:要跳转到的地址
request.getRequestDispatcher("/two").forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
package com.itheima.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
//从请求域中取出值
@WebServlet(name = "Demo7TwoServlet", urlPatterns = "/two")
public class Demo7TwoServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.print("这是Two,从请求域中取出one中的值:" + request.getAttribute("name"));
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
转发使用哪个方法?
request.getRequestDispatcher("/地址").forward(request, response);
由浏览器端进行的页面跳转
从OneServlet重定向到TwoServlet
package com.itheima.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
与请求域相关的方法
*/
@WebServlet(name = "Demo6OneServlet", urlPatterns = "/one")
public class Demo6OneServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.print("这是One
");
//向请求域中添加键和值
request.setAttribute("name","Jack");
request.setAttribute("age", 20); //整数类型
String name = (String) request.getAttribute("name");
int age = (int) request.getAttribute("age");
//从请求域中取出值
out.print("名字:" + name + "
");
out.print("年龄:" + age );
out.print("
");
//删除其中一个
request.removeAttribute("age");
//从请求域中取出值,直接输出Object对象
out.print("名字:" + request.getAttribute("name") + "
");
out.print("年龄:" + request.getAttribute("age") );
System.out.println("这是one");
//开始转发,参数是:要跳转到的地址
//request.getRequestDispatcher("/two").forward(request, response);
//使用重定向
response.sendRedirect("two");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
package com.itheima.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
//从请求域中取出值
@WebServlet(name = "Demo7TwoServlet", urlPatterns = "/two")
public class Demo7TwoServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.print("这是Two,从请求域中取出one中的值:" + request.getAttribute("name"));
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
问:什么时候使用转发,什么时候使用重定向?
如果要保留请求域中的数据,使用转发,否则使用重定向。
以后访问数据库,增删改使用重定向,查询使用转发。
问:转发或重定向后续的代码是否还会运行?
无论转发或重定向后续的代码都会执行
区别 | 转发forward() | 重定向sendRedirect() |
---|---|---|
根目录 | 包含项目访问地址 | 没有项目访问地址 |
地址栏 | 不会发生变化 | 会发生变化 |
哪里跳转 | 服务器端进行的跳转 | 浏览器端进行的跳转 |
请求域中数据 | 不会丢失 | 会丢失 |