Request的方法演示

Request的其他方法

基路径的使用:
  <base href="/MvcPro/"/>

Demo: 获取表单提交的参数
  在服务器获取参数的时候使用的方法是 req.getParameter("参数名"), 参数名要和表单中的 name属性值保持一致
  表单使用post 请求的时候地址栏不会出现提交的参数, 使用 get 的时候会在地址栏出现提交的参数

 1 <body>
 2     <form action="emp" method="POST">
 3         <fieldset>
 4             <legend>请登录</legend>
 5             用户名: <input type="text" name="username"><br><br>
 6             密码: <input type="password" name="pwd">
 7             <input type="submit" value="登录">
 8         </fieldset>
 9     </form>
10 </body>
11 </html>
1 @SuppressWarnings("serial")
2 public class EmpServlet extends HttpServlet {
3     @Override
4     protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
5         String username = req.getParameter("username");
6         String pwd = req.getParameter("pwd");
7         System.out.println(username + ":" + pwd);
8     }
9 }

Demo: 保存和获取属性
  在 request 内置对象中可以保存数据, 之后可以把保存的数据再次取出来(一般是在页面取得),
  以 key=value 键值对形式保存.
在 servlet 中将数据保存之后跳转到 jsp 页面中取得数据的显示

 1 @SuppressWarnings("serial")
 2 public class EmpServlet extends HttpServlet {
 3     @Override
 4     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 5         //实例化一个 Emp 类对象
 6         Emp emp = new Emp(1001,"张三","president",5000.0,new Date(),7788,100.0,10);
 7         //将以上的对象保存 request 内置对象中
 8         req.setAttribute("emp", emp);
 9         //获取 request 内置对象的参数
10         System.out.println(req.getAttribute("emp"));
11     }
12 }

Demo: 获取额外路径信息
  额外路径信息可以用来判断调用具体的方法
1.修改配置
  可以接受/emp/后面的所有路径请求.

1   <!-- 定义出上面的 servlet 处理的路径 这个路径叫做 servlet 的映射路径 -->
2   <servlet-mapping>
3       <servlet-name>empServlet</servlet-name>
4       <url-pattern>/emp/*</url-pattern>
5   </servlet-mapping>

2.修改 servlet 代码

@SuppressWarnings("serial")
public class EmpServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pathInfo = req.getPathInfo();
        System.out.println("额外路径是: " + pathInfo);
    }
}

Demo: 实现伪登录和注销

 1 @SuppressWarnings("serial")
 2 public class EmpServlet extends HttpServlet {
 3     @Override
 4     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 5         //处理中文乱码
 6         req.setCharacterEncoding("utf-8");
 7         String pathInfo = req.getPathInfo();
 8         System.out.println(pathInfo);
 9         if ("/login".equals(pathInfo)) {
10             this.login(req,resp);
11         } else if("/logout".equals(pathInfo)) {
12             this.logout(req,resp);
13         }
14     }
15     //负责登录的方法
16     public void login(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
17         String name = req.getParameter("username");
18         String pwd = req.getParameter("pwd");
19         //查询数据库中的用户密码和用户输入的进行对比
20         if ("smith".equals(name)&&"1234".equals(pwd)) {
21             System.out.println("登录成功");
22             //跳转到欢迎页或者首页(客户端跳转)
23             resp.sendRedirect("/MvcPro/pages/welcome.html");
24         } else {
25             //重新返回登录页面再次登录
26             System.out.println("用户名或密码不正确!");
27             //服务器端跳转
28             req.getRequestDispatcher("/pages/index2.html").forward(req, resp);
29         }
30     }
31     //负责处理注销的方法
32     public void logout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
33         System.out.println("注销成功!");
34         //跳转到登录页面
35         resp.sendRedirect("/MvcPro/pages/index2.html");
36     }
37     @Override
38     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
39         this.doGet(req, resp);
40     }
41 }

创建一个 index2.html 文件

 1 <head>
 2 <base href="/MvcPro/"/>
 3 <meta charset="UTF-8">
 4 <title>Insert title here</title>
 5 </head>
 6 <body>
 7     <form action="emp/login" method="POST">
 8         <fieldset>
 9             <legend>请登录</legend>
10             用户名: <input type="text" name="username"><br><br>
11&nbsp;&nbsp;&nbsp;码: <input type="password" name="pwd"><br/><br/>
12             <input style="margin-left:60px" type="submit" value="提交">
13             <input type="reset" value="重置">
14         </fieldset>
15     </form>
16 </body>
17 </html>

创建一个 welcome.html 文件

 1 <base href="/MvcPro/"/>
 2 <meta charset="UTF-8">
 3 <title>Insert title here</title>
 4 </head>
 5 <body>
 6     <h1>
 7         欢迎光临!
 8     </h1>
 9     <h3>
10         <a href="emp/logout">注销用户</a>
11     </h3>
12 </body>
13 </html>

Demo: 获取所有请求头的信息

 1 @SuppressWarnings("serial")
 2 public class EmpServlet extends HttpServlet {
 3     @Override
 4     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 5         //取得所有请求头信息
 6         Enumeration<String> hraders = req.getHeaderNames();
 7         while (hraders.hasMoreElements()) {
 8             String hrader = (String) hraders.nextElement();
 9             //取得对应的 value 值
10             System.out.println(hrader + ":" + req.getHeader(hrader));
11         }
12     }
13 }

猜你喜欢

转载自www.cnblogs.com/yslf/p/10745437.html