一、HttpServletRequest
每当有请求进入Tomcat,Tomcat服务器就会把请求过来的HTTP协议信息解析好封装到HttpServletRequest对象中,然后传递到service方法中,可以通过HttpServletRequest对象来获取客户端发来的请求中的信息
HttpServletRequest的常用方法
- getRequestURI(),获取请求的资源路径
- gerRequestURL(),获取请求的统一资源定位符
- getRemoteHost(),获取客户端IP
- getHeader(),获取请求头
- getParameter(),获取请求的参数
- getParameterValues(),获取请求中多个参数的值
- getMethod(),获取请求方式,GET或POST
- setAttribute(key,value),设置域数据
- getAttribute(key),获取域数据
- getRequestDispatcher(),获取请求转发对象
在controller包中增加HiServlet
public class HiServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 1. 获取请求的资源路径 System.out.println("请求资源路径为:" + req.getRequestURI()); // 2. 获取请求的URL地址 System.out.println("请求的URL地址为:" + req.getRequestURL()); // 3. 获取请求的客户端IP地址 System.out.println("客户端IP地址为:" + req.getRemoteHost()); // 4. 获取请求头 System.out.println("请求头为:" + req.getHeader("User-Agent")); // 5. 获取请求方式 System.out.println("请求方式为:" + req.getMethod()); } } 复制代码
在web.xml中配置Servlet的访问路径
<servlet> <servlet-name>HiServlet</servlet-name> <servlet-class>com.lilith.servlet.HiServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HiServlet</servlet-name> <url-pattern>/hi</url-pattern> </servlet-mapping> 复制代码
重启Tomcat,浏览器访问http://localhost:8080/hi
web目录下新增form.html页面,新增一个表单
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"/> <title>Form</title> </head> <body> <form action="/param" method="get"> 用户名:<input type="text" name="username"> <br> 密码: <input type="password" name="password"> <br> 兴趣爱好:<input type="checkbox" name="hobby" value="cpp"> C++ <input type="checkbox" name="hobby" value="java"> Java <input type="checkbox" name="hobby" value="python"> Python <br> <input type="submit" value="提交"> </form> </body> </html> 复制代码
新增ParamServlet类,用于获取表单提交的数据
public class ParamServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 获取请求中的参数 String username = req.getParameter("username"); String password = req.getParameter("password"); String hobby = req.getParameter("hobby"); System.out.println("请求中username参数的值为:" + username); System.out.println("请求中password参数的值为:" + password); System.out.println("请求中hobby参数的值为:" + hobby); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doPost(req, resp); } } 复制代码
在web.xml中增加ParamServlet的访问配置
<servlet> <servlet-name>ParamServlet</servlet-name> <servlet-class>com.lilith.servlet.ParamServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ParamServlet</servlet-name> <url-pattern>/param</url-pattern> </servlet-mapping> 复制代码
重启Tomcat,浏览器输入 http://localhost:8080/form.html, 在表单中输入数据后点击提交
当参数有多个值时,需要使用getParameterValues来获取,getParameter只能获取参数的第一个值
// 获取请求中的参数(有多个值) String[] hobbies = req.getParameterValues("hobby"); System.out.println("请求中hobby参数的所有值:" + Arrays.asList(hobbies)); 复制代码
中文乱码问题
表单中输入中文,点击提交
当请求方式为GET请求时,中文可以正常显示
更改form.html中请求方式为post,将goGet方法中的代码拷贝至doPost方法中,在表单中再次提交中文信息
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("doPost方法被调用!"); // 获取请求中的参数 String username = req.getParameter("username"); String password = req.getParameter("password"); String hobby = req.getParameter("hobby"); System.out.println("请求中username参数的值为:" + username); System.out.println("请求中password参数的值为:" + password); System.out.println("请求中hobby参数的值为:" + hobby); // 获取请求中的参数(有多个值) String[] hobbies = req.getParameterValues("hobby"); System.out.println("请求中hobby参数的所有值:" + Arrays.asList(hobbies)); } 复制代码
根据控制台的输出,POST提方式提交时中文乱码了。
doPost方法中增加编码设置
req.setCharacterEncoding("UTF-8"); 复制代码
再次提交表单
中文乱码问题已解决
Servlet的请求转发
请求转发是指服务器收到请求后,从一个资源跳转到另一个资源的操作。
在controller包中新建两个Servlet类AlphaServlet和BravoServlet,用作实现请求转发
public class AlphaServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 获取请求参数 String username = req.getParameter("username"); System.out.println("alpha请求中参数username的值为:" + username); // 要转发到BravoServlet的地址 // 请求转发的地址必须要以/开头 RequestDispatcher requestDispatcher = req.getRequestDispatcher("/bravo"); // 转发到BravoServlet requestDispatcher.forward(req,resp); } } 复制代码
public class BravoServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String username = req.getParameter("username"); System.out.println("bravo请求中参数username的值为:" + username); System.out.println("BravoServlet业务处理"); } } 复制代码
在web.xml中配置AlphaServlet和BravoServlet的访问路径
<servlet> <servlet-name>AlphaServlet</servlet-name> <servlet-class>com.lilith.servlet.AlphaServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>AlphaServlet</servlet-name> <url-pattern>/alpha</url-pattern> </servlet-mapping> <servlet> <servlet-name>BravoServlet</servlet-name> <servlet-class>com.lilith.servlet.BravoServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>BravoServlet</servlet-name> <url-pattern>/barvo</url-pattern> </servlet-mapping> 复制代码
重新启动Tomcat,在浏览器中输入 http://localhost:8080/alpha?username=peter, 点击回车