javaweb开发(六):servlet请求转发、重定向、EL表达式
-
构建1个web项目,导入依赖
-
编写TestServlet
@WebServlet(name = "testServlet",urlPatterns = {"/test"})
public class TestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
PrintWriter printWriter = resp.getWriter();
printWriter.write("<div> TestServlet doGet </div>");
}
}
-
配置端口
-
测试
-
请求转发
-
新建1个admin.jsp
-
编写DispatchServlet
@WebServlet("/forward")
public class DispatchServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("DispatchServlet doGet");
req.getRequestDispatcher("/WEB-INF/admin.jsp").forward(req,resp);
}
}
- 启动项目测试
# 访问http://localhost:8080/index.jsp可以访问到页面
# 访问http://localhost:8080/admin.jsp无法访问页面
# 访问http://localhost:8080/WEB-INF/admin.jsp无法访问页面
# 访问http://localhost:8080/forward可以访问页面admin.jsp
# servlet内部转发
- 转发到其他servlet
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("DispatchServlet doGet");
req.getRequestDispatcher("request").forward(req,resp);
}
- 测试
http://localhost:8080/forward?userName=jack&age=11&sport=ball&sport=sleep
- 控制台打印
DispatchServlet doGet
应用上下文路径 getContextPath=
客户端发出请求时的完整URL getRequestURL=http://localhost:8080/request
请求行中的资源名部分 getRequestURI=/request
请求行中的参数部分 getQueryString=userName=jack&age=11&sport=ball&sport=sleep
发出请求的客户机的IP地址 getRemoteAddr=0:0:0:0:0:0:0:1
客户机发请求使用的网络端口号 getRemotePort=51171
获取请求头 getHeader(Accept)=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
客户端请求参数 getParameter=jack
客户端请求参数列表,多个值 getParameterValues=[Ljava.lang.String;@57d4dac
客户端请求参数封装成的map类型 getParameterMap=org.apache.catalina.util.ParameterMap@68a7bf4d
EL表达式
# DispatchServlet中编写入下
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("DispatchServlet doGet");
req.setAttribute("name","xdclass");
req.getRequestDispatcher("/index.jsp").forward(req,resp);
}
- index.jsp中获取领域对象中值
name = ${name}
- 启动测试
# 直接访问index.jsp不能获取到领域对象中的值
http://localhost:8080/index.jsp
# 访问时使用重定向可获取到领域对象中值
http://localhost:8080/forward
- 新建1个实体类
public class User {
private int id;
private String name;
private String host;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
}
- 编写servlet
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("DispatchServlet doGet");
User user = new User();
user.setHost("https://xdclass.net");
user.setId(1);
user.setName("老王");
req.setAttribute("user",user);
req.getRequestDispatcher("/index.jsp").forward(req,resp);
}
- 编写index.jsp
name = ${user.name}
id= ${user.id}
host= ${user.host}
- 启动测试
http://localhost:8080/forward
重定向
,是一个外部的转发,有2次请求,无法获取到领域对象中的值
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("DispatchServlet doGet");
req.setAttribute("name","jack");
resp.sendRedirect("/index.jsp");
}
- 测试