HttpServletRequest的获取前端传递的参数并且请求转发

写在前面:

web服务器接收到客户端的http请求,针对这个请求,分别建立了一个代表请求的HttpServletResponse对象;和一个代表响应的HttpServletRequest对象。

如果要获取客户端请求过来的参数:HttpServletRequest对象。

如果要给客户端响应一些信息:找HttpServletResponse对象。

HttpservletRequest:代表客户端的请求,用户通过Http协议访问服务器,Http请求中的所有信息会被封装到HttpServletRequest,通过这个HttpServletRequest方法,获得客户端所有信息。

HttpServletRequest的获取前端传递的参数并且请求转发

1、 创建项目

     

复制代码
 1 package com.wang.servlet;
 2 
 3 import javax.servlet.ServletException;
 4 import javax.servlet.http.HttpServlet;
 5 import javax.servlet.http.HttpServletRequest;
 6 import javax.servlet.http.HttpServletResponse;
 7 import java.io.FileInputStream;
 8 import java.io.IOException;
 9 import java.net.URLEncoder;
10 import java.util.Arrays;
11 
12 public class LoginServlet extends HttpServlet {
13     @Override
14     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
15         //解决后台接收中文乱码问题
16         req.setCharacterEncoding("utf-8");
17         resp.setCharacterEncoding("utf-8");
18 
19         String username = req.getParameter("username");
20         String password = req.getParameter("password");
21         String[] hobbys = req.getParameterValues("hobbys");
22         System.out.println("===================================");
23         System.out.println(username);
24         System.out.println(password);
25         System.out.println(Arrays.toString(hobbys));
26         System.out.println("===================================");
27         System.out.println(req.getContextPath());
28         //通过请求转发
29         req.getRequestDispatcher("/success.jsp").forward(req,resp);
30     }
31 
32     @Override
33     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
34         doGet(req, resp);
35     }
36 }
View Code
复制代码

2、 写index.jsp

删掉webapp下的index.jsp,自己重建

  

 

写index.jsp

复制代码
 1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 <html>
 3 <head>
 4     <title>登录</title>
 5 </head>
 6 <body>
 7 <h1>登录</h1>
 8 <div>
 9     <form action="${pageContext.request.contextPath}/login" method="post">
10         用户名:<input type="text" name="username"> <br>
11         密码:<input type="password" name="password"> <br>
12         爱好:
13         <input type="checkbox" name="hobbys" value="女孩">女孩
14         <input type="checkbox" name="hobbys" value="代码">代码
15         <input type="checkbox" name="hobbys" value="唱歌">唱歌
16         <input type="checkbox" name="hobbys" value="电影">电影
17         <br>
18         <input type="submit">
19     </form>
20 </div>
21 </body>
22 </html>
View Code
复制代码

3、 再写个success.jsp

 

4、 注册+映射

复制代码
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
 5                       http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
 6          version="4.0"
 7          metadata-complete="true">
 8     <!--注册和映射-->
 9     <servlet>
10         <servlet-name>LoginServlet</servlet-name>
11         <servlet-class>com.wang.servlet.LoginServlet</servlet-class>
12     </servlet>
13     <servlet-mapping>
14         <servlet-name>LoginServlet</servlet-name>
15         <url-pattern>/login</url-pattern>
16     </servlet-mapping>
17 </web-app>
View Code
复制代码

5、 指定Tomcat并运行

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2、 解决请求和响应中文乱码问题

1 req.setCharacterEncoding("utf-8");
2 resp.setCharacterEncoding("utf-8");
View Code

 

posted @   WZ_BeiHang  阅读(4590)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示