javaweb学习总结4----servlet开发二

Request需要掌握

乱码问题解决方案:

  • 所有能设置编码的地方都统一设置 : 数据库,数据库字段,Servlet程序,请求,响应 , 前端,浏览器。

JSP取得项目的路径

${pageContext.request.contextPath}

Request对象

    package com.wu.servlet;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    
    public class RequestDemo01 extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
            //查看request对象的方式
            System.out.println(request.getContextPath()); //获得web项目路径
            System.out.println(request.getRequestURI()); //请求的URL路径
            //Remote 远程
            System.out.println(request.getRemoteUser()); //获得远程的用户名
            System.out.println(request.getRequestedSessionId()); //获得SessionId;
            System.out.println(request.getServletPath()); //请求servlet的url
            System.out.println(request.getLocalAddr()); //获得本地地址  防火墙
            System.out.println(request.getLocalName()); //获得本地名字
            System.out.println(request.getLocalPort()); //获得访问的端口号
    
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request,response);
        }
    }
### Request对象接收前端用户提交的参数

1. 写前端注册页面

   - 表单的提交方式 get/post

   - 表单提交的位置 action:服务项目的路径/servlet对应的请求url

     ```jsp
     <%@ page contentType="text/html;charset=UTF-8" language="java" %>
     <html>
     <head>
         <title>注册</title>
     </head>
     <body>
     
     
     <%--
     JSP的注释
     --%>
     
     
     <%--
     Form表单
         action:代表要提交的路径,表单要提交到哪里 ,可以提交到一个Servlet
             //提交到Servlet需要,获取到Web项目的路径下的Servlet
         method: 提交表单的方式
     --%>
     <h1>注册</h1>
     
     <form action="${pageContext.request.contextPath}/r2" method="post">
     
         <p>用户名:<input type="text" name="username"></p>
         <p>密码:<input type="password" name="password"></p>
     
         <p>
             <input type="submit">
             <input type="reset">
         </p>
     
     </form>
     
     
     
     </body>
     </html>
     
     ```

     

2. 写处理前端提交信息的Servlet

   - 接收前端传递的控件信息,  request.getParameter(“控件的name‘);

     ```java
     package com.wu.servlet;
     
     
     import javax.print.attribute.HashPrintRequestAttributeSet;
     import javax.servlet.ServletException;
     import javax.servlet.http.HttpServlet;
     import javax.servlet.http.HttpServletRequest;
     import javax.servlet.http.HttpServletResponse;
     import java.io.IOException;
     
     //处理前端提交过来的数据
     public class RequestDemo02 extends HttpServlet {
         @Override
         protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
             doPost(req,resp);
         }
     
         @Override
         protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     
             //通过HttpServletRequest获取前端用户提交的信息
             //req.getParameterValues(); 获得前端传递的控件信息,通过控件的name;但是可以接受多个参数的控件,比如(多选框....)
             //req.getParameter(); 获得前端传递的控件信息,通过控件的name;
     
             req.setCharacterEncoding("utf-8");
             resp.setCharacterEncoding("utf-8");
     
             String username = req.getParameter("username");
             String password = req.getParameter("password");
     
             //连接数据库,比较数据库中的信息是否匹配
     
             if (username.equals("admin")&&password.equals("1234567")){
                 System.out.println("登录成功!"); //跳转到成功页面
             }else {
                 System.out.println("登录失败!"); //提示重新注册
             }
     
             System.out.println(username);
             System.out.println(password);
     
     
         }
     }
     
     ```

3. 配置web.xml

   - 配置对应的XML文件

     ```xml
         <servlet>
             <servlet-name>RequsetDemo02</servlet-name>
             <servlet-class>com.kuang.servlet.RequestDemo02</servlet-class>
         </servlet>
     
     
         <!--=====================================================-->
     
     
         <servlet-mapping>
             <servlet-name>RequsetDemo02</servlet-name>
             <url-pattern>/r2</url-pattern>
         </servlet-mapping>
     
     ```

4. 提交测试

   - 检查英文提交情况,服务器是否接收成功!
   - 提交中文,看看乱码情况
   - 设置编码统一,解决乱码问题;



### Request实现请求转发

```java
package com.wu.servlet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class FormServlet1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理乱码
        req.setCharacterEncoding("UTF-8");
        //处理前端的请求
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        System.out.println("接收到的用户名:"+username+"\n密码:"+password+"\n爱好:");

        String[] hobbies = req.getParameterValues("hobby");

        for (String hobby : hobbies) {
            System.out.println(hobby);
        }


        //重定向:服务器告诉客户端你应该请求另外一个地址;  (地址栏会变)
        //resp.sendRedirect("url");

        //转发:服务器把这个请求转向另外一个Servlet去处理; (地址栏不会变)
        //RequestDispatcher ,需要使用RequestDispatcher来进行处理,我们需要获得这个类
        //参数就是他要转发到的页面

        /*
        RequestDispatcher requestDispatcher = req.getRequestDispatcher("/success.jsp");
        requestDispatcher.forward(req,resp);
        */

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        req.getRequestDispatcher("/success.jsp").forward(req,resp);


    }
}

你可能感兴趣的:(javaweb学习总结)