JavaWebDay03

用戶登錄案例需求:
1.編寫login.html登錄頁面
username & password 兩個(gè)輸入框
2.使用Druid數(shù)據(jù)庫連接池技術(shù),操作mysql,數(shù)據(jù)庫中user表
3.使用JdbcTemplate技術(shù)封裝JDBC
4.登錄成功跳轉(zhuǎn)到SuccessServlet展示:登錄成功!用戶名,歡迎您
5.登錄失敗跳轉(zhuǎn)到FailServlet展示:登錄失敗,用戶名或密碼錯(cuò)
HTTP協(xié)議:

請(qǐng)求消息:客戶端發(fā)送給服務(wù)器端的數(shù)據(jù)

數(shù)據(jù)格式:
請(qǐng)求行
請(qǐng)求頭
請(qǐng)求空行
請(qǐng)求體
響應(yīng)消息:服務(wù)器端發(fā)送給客戶端的數(shù)據(jù)

數(shù)據(jù)格式:
響應(yīng)行
組成:協(xié)議/版本 響應(yīng)狀態(tài)碼 狀態(tài)碼描述
響應(yīng)狀態(tài)碼:服務(wù)器告訴客戶端瀏覽器本次請(qǐng)求和響應(yīng)的一個(gè)狀態(tài)。

  1. 狀態(tài)碼都是3位數(shù)字
  2. 分類:
  3. 1xx:服務(wù)器就收客戶端消息,但沒有接受完成,等待一段時(shí)間后,發(fā)送1xx多狀態(tài)碼
  4. 2xx:成功。代表:200
  5. 3xx:重定向。代表:302(重定向),304(訪問緩存)
  6. 4xx:客戶端錯(cuò)誤。
  • 代表:
  • 404(請(qǐng)求路徑?jīng)]有對(duì)應(yīng)的資源)
  • 405:請(qǐng)求方式?jīng)]有對(duì)應(yīng)的doXxx方法
  1. 5xx:服務(wù)器端錯(cuò)誤。代表:500(服務(wù)器內(nèi)部出現(xiàn)異常)
    響應(yīng)頭:
  2. 格式:頭名稱: 值
  3. 常見的響應(yīng)頭:
  4. Content-Type:服務(wù)器告訴客戶端本次響應(yīng)體數(shù)據(jù)格式以及編碼格式
  5. Content-disposition:服務(wù)器告訴客戶端以什么格式打開響應(yīng)體數(shù)據(jù)
  • 值:
  • in-line:默認(rèn)值,在當(dāng)前頁面內(nèi)打開
  • attachment;filename=xxx:以附件形式打開響應(yīng)體。文件下載
  1. 響應(yīng)空行
  2. 響應(yīng)體:傳輸?shù)臄?shù)據(jù)

Response對(duì)象
功能:設(shè)置響應(yīng)消息

設(shè)置響應(yīng)行
格式:HTTP/1.1 200 ok
設(shè)置狀態(tài)碼:setStatus(int sc)
設(shè)置響應(yīng)頭:setHeader(String name, String value)
案例:

完成重定向
重定向:資源跳轉(zhuǎn)的方式
重定向和轉(zhuǎn)發(fā)的區(qū)別( forward 和 redirect 區(qū)別)
重定向的特點(diǎn):
1、 地址欄發(fā)生變化
2、重定向可以訪問其他站點(diǎn)的資源
3、重定向是兩次請(qǐng)求,不能使用request域?qū)ο髞砉蚕頂?shù)據(jù)
轉(zhuǎn)發(fā)的特點(diǎn):
1、地址欄路徑不變
2、轉(zhuǎn)發(fā)不能訪問其他站點(diǎn)的資源
3、轉(zhuǎn)發(fā)是一次請(qǐng)求,可以使用request域?qū)ο髞砉蚕頂?shù)據(jù)

1.設(shè)置流的編碼方式

 req.setCharacterEncoding("utf-8");
@Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.設(shè)置流的編碼方式
        req.setCharacterEncoding("utf-8");
//        //根據(jù)參數(shù)名稱獲取參數(shù)值
//        String username = req.getParameter("username");
//        System.out.println(username);
//        //根據(jù)參數(shù)名獲取參數(shù)值的數(shù)組
//        String[] hobbies = req.getParameterValues("hobby");
//        for (String hobby:hobbies){
//            System.out.println(hobby);
//        }
//        //獲取所有請(qǐng)求參數(shù)名稱
        Enumeration<String> parameterNames = req.getParameterNames();
        while (parameterNames.hasMoreElements()){
            String name = parameterNames.nextElement();
            System.out.println(name);
            System.out.println("==================");
            String value = req.getParameter(name);
            System.out.println(value);
            System.out.println("==================");
        }
//        Enumeration<String> parameterNames = req.getParameterNames();
//        while (parameterNames.hasMoreElements()){
//            String name = parameterNames.nextElement();
//            System.out.println(name);
//            System.out.println("==============");
//            String value = req.getParameter(name);
//            System.out.println(value);
//            System.out.println("==============");
//        }
//        獲取所有參數(shù)的map集合
//        Map<String, String[]> parameterMap = req.getParameterMap();
//        Set<String> keySet = parameterMap.keySet();
//        for (String name:keySet){
//            String[] vlaues = parameterMap.get(name);
//            System.out.println(name);
//            for (String value: vlaues){
//                System.out.println(value);
//            }
//            System.out.println("++++++++++++++++++");
//        }

    }

轉(zhuǎn)發(fā)

@WebServlet("/requestDemo4")
public class RequestDemo4 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //轉(zhuǎn)發(fā)
//        RequestDispatcher requestDispatcher = req.getRequestDispatcher("/requestDemo5");
//        requestDispatcher.forward(req,resp);
      //轉(zhuǎn)發(fā)時(shí)存儲(chǔ)數(shù)據(jù)到request域中
        req.setAttribute("msg","hello");
        req.getRequestDispatcher("/requestDemo5").forward(req,resp);
        //轉(zhuǎn)發(fā)到指定的地址  只能轉(zhuǎn)發(fā)當(dāng)前項(xiàng)目下的地址 ,外部的不行
//        req.getRequestDispatcher("http://www.neusoft.com").forward(req,resp);
        //轉(zhuǎn)發(fā)時(shí)存儲(chǔ)數(shù)據(jù)到request域中 (練習(xí))
//        req.setAttribute("123","321");
//        RequestDispatcher requestDispatcher = req.getRequestDispatcher("/requestDemo5");
//        requestDispatcher.forward(req,resp);
    }
}

重定向

@WebServlet("/responseDemo1")
public class ResponseDemo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("demo1……");
        //重定向
//       //  方法一(不常用):設(shè)置狀態(tài)碼是302
//        resp.setStatus(302);
////        //設(shè)置響應(yīng)頭 location
//        resp.setHeader("location","/responseDemo2");

        //方式二:  這個(gè)方法獲取不到值 結(jié)果為null ,但是可以跳轉(zhuǎn)到特定網(wǎng)址
//        req.setAttribute("mgs", "response_success");
//        resp.sendRedirect("/responseDemo2");

        resp.sendRedirect("http://www.baidu.com");

    }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容