javaWeb03-请求转发和请求重定向【包含浏览器与响应编码格式不一致的解决方法】
阅读原文时间:2023年07月08日阅读:1

本文主要讲述javaWeb的请求转发和请求重定向的区别

一. 请求转发

  Servlet1的代码示例如下

public class Servlet1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("Servlet1收到请求");
req.setAttribute("key","Servlet1盖的章");
req.getRequestDispatcher("/servlet2").forward(req,resp);
}

@Override  
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

}  

}

  Servlet2的代码示例如下

public class Servlet2 extends HttpServlet {
@Override
// 请求转发,处理的是同一个请求
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Object key = req.getAttribute("key");
System.out.println("Servlet2收到了" + key);
resp.getWriter().write("Servlet2响应的内容");
}

@Override  
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

}  

}

  浏览器收到响应,显示结果如下

  思考1

  响应response没有设置编码规则,导致默认的编码规则不支持中文字符,验证如下

public class Servlet2 extends HttpServlet {
@Override
// 请求转发,处理的是同一个请求
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Object key = req.getAttribute("key");
System.out.println("Servlet2收到了" + key);
System.out.println(resp.getCharacterEncoding());
resp.getWriter().write("Servlet2响应的内容");
}

@Override  
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

}  

}

  tomcat日志如下

Servlet1收到请求
Servlet2收到了Servlet1盖的章
ISO-8859-1

  由此看出,字符集是ISO-8859-1应该不支持中文字符

  将resp的字符集设置为utf-8,声明如下

resp.setCharacterEncoding("utf-8");

  浏览器收到响应,显示结果如下,仍是乱码

  思考2

  resp响应设置了字符集utf-8,但是浏览器编码格式不是utf-8,而是windos-1252。字符集不相同,导致乱码

  由思考和思考2,引出问题:如何保证浏览器和响应resp的字符集相同呢?

  首先介绍Content-Type

  解决方法

  Servlet2声明如下

public class Servlet2 extends HttpServlet {
@Override
// 请求转发,处理的是同一个请求
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Object key = req.getAttribute("key");
System.out.println("Servlet2收到了" + key);
// 解决浏览器与resp响应的编码格式不一致问题:
// 方式一:
resp.setHeader("Content-Type","text/html;charset=utf-8");
// 方式二:
resp.setContentType("text/html;charset=utf-8");
resp.getWriter().write("Servlet2响应的内容");
}

@Override  
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

}  

}

  ① 请求转发,一直是同一个请求【请求的数据可以共享】

② 在resp响应浏览器内容时,如果有中文字符,则在响应之前,需要设置content-type,即 resp.setContentType("text/html;charset=utf-8");

二. 请求重定向

  Servlet1的代码示例如下

public class Servlet1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("Servlet1收到请求");
System.out.println("Servlet1请求重定向给Servlet2");
resp.sendRedirect("/servlet2");
}

@Override  
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

}  

}

  Servlet2的代码示例如下

public class Servlet2 extends HttpServlet {
@Override
// 请求转发,处理的是同一个请求
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("Servlet2收到请求");
System.out.println("请求路径为: ");
StringBuffer requestURL = req.getRequestURL();
System.out.println(requestURL);
}

@Override  
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

}  

}

  浏览器收到响应,结果如下

  思考

  请求重定向的路径写的不对,下面解释一下 " / "

在浏览器中," / "是指 http://localhost:8080,即http://ip地址:端口号

在web项目中," / "是指http://localhost:8080/javaWeb03,即http://ip地址:端口号/项目名称

  解决方法

  Servlet1声明如下

public class Servlet1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("Servlet1收到请求");
System.out.println("Servlet1请求重定向给Servlet2");// 解决方式1:相对路径
resp.sendRedirect("/javaWeb02/servlet2");
// 解决方式2:绝对路径
resp.sendRedirect("http://localhost:8080/javaWeb02/servlet2");
}

@Override  
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

}  

}

  tomcat日志结果如下

Servlet1收到请求
Servlet1请求重定向给Servlet2
Servlet2收到请求
请求路径为:
http://localhost:8080/javaWeb02/servlet2

  ① 注意浏览器和web项目中的" / "的不同含义;

② 请求重定向是浏览器向服务器发了两次请求