9.1、HttpServletRequest类有什么作用
每次只要请求进入Tomcat服务器,Tomcat服务器就会把请求过来的HTTP协议信息解析好封装到Request对象中,然后传递到service方法(doGet 和 doPost)中给我们使用,我们可以通过HttpServletRequst对象,获取到所有请求的信息。
9.2、HttpServletRequest类的常用方法
9.3、HttpSerlvet请求转发
// 地址1--->>>>地址2
public class HttpServletForwardingOne extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 拿到请求参数
String username = req.getParameter("username");
System.out.println("one:" + username);
// one盖章
// getServletContext().setAttribute("token","one-successful");
req.setAttribute("token","one-successful");
// 请求two
RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("/httpServletForwardingTwo");
requestDispatcher.forward(req,resp);
}
}
public class HttpServletForwardingTwo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
System.out.println("tow:" + username);
Object token = req.getAttribute("token");
System.out.println(token);
System.out.println("tow的业务需求-------------");
}
}
9.4、web中 / 斜杠的不同意义
在 web 中 / 斜杠 是一种绝对路径
/ 斜杠 如果被浏览器解析,得到的地址是:http://ip:port/
<a href="/">斜杠</a>
/ 斜杠 如果被服务器解析,得到的地址是:http://ip:port/工程路径
<url-pattern>/servlet</url-pattern>
servletContext.getRealPath("/");
request.getRequestDispatcher("/");
特殊情况:response.sedRediect("/"); 把斜杠发送给浏览器解析,得到http://ip:port/
评论区