1. 程式人生 > 其它 >Hexo部落格快速部署

Hexo部落格快速部署

HttpServletRequest

1、定義

HttpServletRequest代表客戶端的請求,使用者通過http協議訪問伺服器,http請求中的所有資訊會被封裝到HttpServletRequest中

通過這個HttpServletRequest的方法,我們可以獲得客戶端的所有資訊

2、應用場景

  1. 獲取前端的引數

    主要方法

    • String getParameter(String var1);
      
    • String[] getParameterValues(String var1);
      
  2. 請求轉發

    1. 實現

      • index.jsp

        <%@ page contentType="text/html;charset=UTF-8" language="java" %>
        <html>
        <head>
            <title>登入</title>
        </head>
        <body>
        <form action="${pageContext.request.contextPath}/login" method="post">
            使用者:<input type="text" name="username"></br>
            密碼:<input type="password" name="password"></br>
            愛好
            <input type="checkbox" name="hobbys" value="女孩">女孩
            <input type="checkbox" name="hobbys" value="程式碼">程式碼
            <input type="checkbox" name="hobbys" value="聽歌">聽歌
            <input type="checkbox" name="hobbys" value="電影">電影
            </br>
            <input type="submit">
        </form>
        </body>
        </html>
        
      • java

        public class LoginServlet extends HttpServlet {
            @Override
            protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                req.setCharacterEncoding("utf-8");//解決亂碼
                resp.setCharacterEncoding("utf-8");//解決亂碼
                String username = req.getParameter("username");
                String password = req.getParameter("password");
                String[] hobbys = req.getParameterValues("hobbys");
                System.out.println("=====================================");
                System.out.println(username);
                System.out.println(password);
                System.out.println(Arrays.toString(hobbys));//如果前面不設定編碼,這裡控制檯輸出會亂碼
                System.out.println("=====================================");
                //注意:請求轉發的路徑與重定向的路徑不同,在重定向中"/"就代表專案路徑,不需要再在頁面路徑前加專案路徑
                //重定向需要在頁面路徑前加專案路徑即:"/s5/susses.jsp"
                req.getRequestDispatcher("/susses.jsp").forward(req,resp);
            }
        
            @Override
            protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                doGet(req, resp);
            }
        }
        
      • susses.jsp(重定向頁面)

        <%@ page contentType="text/html;charset=UTF-8" language="java" %>
        <html>
        <head>
            <title>susses</title>
        </head>
        <body>
        <h1 style="color: aquamarine">登入成功</h1>
        </body>
        </html>
        
      • xml

        <servlet>
            <servlet-name>LoginServlet</servlet-name>
            <servlet-class>com.zhang.servlet.LoginServlet</servlet-class>
          </servlet>
          <servlet-mapping>
            <servlet-name>LoginServlet</servlet-name>
            <url-pattern>/login</url-pattern>
          </servlet-mapping>
        
  3. 重定向與轉發的區別

    • 相同點

      頁面都會實現跳轉

    • 不同點

      請求轉發時url位址列不會產生變化,狀態碼:307

      重定向時url位址列會發生變化,狀態碼:302