JavaWeb 学习笔记 day06
Servlet
HttpServletRequest 类
简介:每次只要有请求进入 Tomcat 服务器,Tomcat 服务器就会把请求过来的 HTTP 协议信息解析好封装到 Request 对象中。 然后传递到 service 方法(doGet 和 doPost)中给我们使用。我们可以通过HttpServletRequest 对象,获取到所有请求的信息
public class HelloServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
//获取请求的资源路径 /day07_servlet/helloServlet
System.out.println(request.getRequestURI());
//获取请求的url(绝对路径) http://localhost:8080/day07_servlet/helloServlet
System.out.println(request.getRequestURL());
//获取客户端ip 127.0.0.1
System.out.println(request.getRemoteHost());
//获取请求头 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36
System.out.println(request.getHeader("User-Agent"));
//获取请求方式 GET
System.out.println(request.getMethod());
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
//在 Post 请求中,通过设置字符集解决中文乱码问题
request.setCharacterEncoding("UTF-8");
request.setAttribute("key","value");
String username = request.getParameter("username");
String password = request.getParameter("password");
String[] hobbies = request.getParameterValues("hobby");
System.out.println(username);
System.out.println(password);
System.out.println(Arrays.asList(hobbies));
System.out.println(request.getAttribute("key"));
}
}
请求的转发
简介:请求转发是指,服务器收到请求后,从一次资源跳转到另一个资源的操作叫请求转发。一般用于一个请求需要多个程序共同完成时使用。
public class Servlet1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
System.out.println(username);
req.setAttribute("key","value");
//设置要转发的资源路径
RequestDispatcher requestDispatcher = req.getRequestDispatcher("/servlet2");
//开始转发
requestDispatcher.forward(req,resp);
}
}
public class Servlet2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
System.out.println(username);
Object value = req.getAttribute("key");
System.out.println(value);
}
}
注意:请求转发的多个程序共享一个 HttpServletRequest 对象
HttpServletResponse 类
简介:HttpServletResponse 类和 HttpServletRequest 类一样。每次请求进来,Tomcat 服务器都会创建一个 Response 对象传 递给 Servlet 程序去使用。HttpServletRequest 表示请求过来的信息,HttpServletResponse 表示所有响应的信息, 我们如果需要设置返回给客户端的信息,都可以通过 HttpServletResponse 对象来进行设置
注意:仍然存在中文乱码问题,这里提供两种方式解决
- 两个输出流的说明。
字节流 getOutputStream(); 常用于下载(传递二进制数据)
字符流 getWriter(); 常用于回传字符串(常用)
public class ResponseTest extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//方式一:
// resp.setCharacterEncoding("UTF-8");
// resp.setHeader("Content-Type","text/html; charset=UTF-8");
//方式二:(推荐)
//同时设置服务器和客户端的字符集
//此方法一定要在获取流对象之前调用才有效
resp.setContentType("text/html; charset=UTF-8");
PrintWriter writer = resp.getWriter();
writer.write("你好啊");
}
}
请求重定向
简介:请求重定向,是指客户端给服务器发请求,然后服务器告诉客户端说。我给你一些地址。你去新地址访问。叫请求重定向(因为之前的地址可能由于项目迭代已经被废弃)。
public class Response1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//方式一:
// //设置响应码
// resp.setStatus(302);
// //设置要重定向的地址
// resp.setHeader("Location","http://localhost:8080/day07_servlet/response2");
//方式二:(推荐)
resp.sendRedirect("http://localhost:8080/day07_servlet/response2");
}
}
public class Response2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter writer = resp.getWriter();
writer.write("response2 results!");
}
}