Servlet流程总结
Servlet流程總結(jié)
瀏覽器發(fā)起請(qǐng)求到服務(wù)器(請(qǐng)求)
服務(wù)器接受瀏覽器的請(qǐng)求,進(jìn)行解析,創(chuàng)建request對(duì)象存儲(chǔ)請(qǐng)求數(shù)據(jù)
服務(wù)器調(diào)用對(duì)應(yīng)的servlet進(jìn)行請(qǐng)求處理,并將request對(duì)象作為實(shí)參傳遞給servlet的方法
?servlet的方法執(zhí)行進(jìn)行請(qǐng)求處理
? ?? ? //設(shè)置請(qǐng)求編碼格式
? ? ? //設(shè)置響應(yīng)編碼格式
? ? ? //獲取請(qǐng)求信息
? ? ? //處理請(qǐng)求信息
? ? ?? ? ? //創(chuàng)建業(yè)務(wù)層對(duì)象
? ??? ? ? ?//調(diào)用業(yè)務(wù)層對(duì)象的方法
? ? ? //響應(yīng)處理結(jié)果
數(shù)據(jù)流轉(zhuǎn)流程:
瀏覽器------>服務(wù)器------->數(shù)據(jù)庫(kù)
瀏覽器<------服務(wù)器<-------數(shù)據(jù)庫(kù)
請(qǐng)求轉(zhuǎn)發(fā)
問(wèn)題:服務(wù)器在接收到瀏覽器的請(qǐng)求后,僅僅使用一個(gè)Servlet 進(jìn)行請(qǐng)求處理,會(huì)造成不同的 Servlet 邏輯代碼冗余,Servlet 的職責(zé)不明確。
解決:使用請(qǐng)求轉(zhuǎn)發(fā)。
?*請(qǐng)求轉(zhuǎn)發(fā)學(xué)習(xí):
?*?? ??? ?作用:實(shí)現(xiàn)多個(gè)servlet聯(lián)動(dòng)操作處理請(qǐng)求,這樣避免代碼冗余,讓servlet的職責(zé)更加明確。
?*?? ??? ?使用:
?*?? ??? ??? ??? ?req.getRequestDispatcher("要轉(zhuǎn)發(fā)的地址").forward(req, resp);
?*?? ??? ??? ??? ?地址:相對(duì)路徑,直接書(shū)寫(xiě)servlet的別名即可。
?*?? ??? ?特點(diǎn):
?*?? ??? ??? ?一次請(qǐng)求,瀏覽器地址欄信息不改變。
?*?? ??? ?注意:
?*?? ??? ??? ?請(qǐng)求轉(zhuǎn)發(fā)后直接return結(jié)束即可。
?Request 對(duì)象作用域
問(wèn)題:使用請(qǐng)求轉(zhuǎn)發(fā)后,不同的 Servlet 之間怎么進(jìn)行數(shù)據(jù)的共享呢?或者說(shuō)數(shù)據(jù)怎么從一個(gè) servlet 流轉(zhuǎn)給另外一個(gè) Servlet 呢?
解決:使用 request 對(duì)象的作用域
使用request對(duì)象實(shí)現(xiàn)不同servlet的數(shù)據(jù)流轉(zhuǎn)
使用:
? ? ? ? ?request.setAttribute(object name,Object value);
? ? ? ? ?request.getAttribute(Object obj)
作用:解決了一次請(qǐng)求內(nèi)的不同 Servlet 的數(shù)據(jù)(請(qǐng)求數(shù)據(jù)+其他數(shù)據(jù))共享問(wèn)題。
作用域:基于請(qǐng)求轉(zhuǎn)發(fā),一次請(qǐng)求中的所有 Servlet 共享
注意:使用 Request 對(duì)象進(jìn)行數(shù)據(jù)流轉(zhuǎn),數(shù)據(jù)只在一次請(qǐng)求內(nèi)有效。
特點(diǎn):服務(wù)器創(chuàng)建
? ? ? ? ? 每次請(qǐng)求都會(huì)創(chuàng)建
? ? ? ? ? 生命周期一次請(qǐng)求
PageServlet.java
package com.dym.servlet;import java.io.IOException;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;public class PageServlet extends HttpServlet {@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {//設(shè)置相應(yīng)編碼格式resp.setContentType("text/html;charset=utf-8");//獲取請(qǐng)求信息//處理請(qǐng)求//響應(yīng)處理結(jié)果//獲取request作用域數(shù)據(jù)String str=(String)req.getAttribute("str")==null?"":(String) req.getAttribute("str"); System.out.println(str);resp.getWriter().write("<html>");resp.getWriter().write("<head>");resp.getWriter().write("</head>");resp.getWriter().write("<body>");resp.getWriter().write("<font color='red' size='20px'>"+str+"</font>"); //必須把這句放在表單外面,否則將會(huì)出錯(cuò)resp.getWriter().write("<form action='login' method='get'>");resp.getWriter().write("用戶(hù)名:<input type='text' name='uname' value=''/><br/>");resp.getWriter().write("密碼:<input type='password' name='pwd' value=''/><br/>");resp.getWriter().write("<input type='submit' value='登錄'/><br/>");resp.getWriter().write("</form>");resp.getWriter().write("</body>");resp.getWriter().write("</html>");} }LoginServlet.java
package com.dym.servlet;import java.io.IOException;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import com.dym.pojo.User; import com.dym.service.LoginService; import com.dym.service.impl.LoginServiceImpl;/***請(qǐng)求中文亂碼解決:* 1、使用String進(jìn)行數(shù)據(jù)重新編碼 核心數(shù)據(jù)用這種方式* uname=new String(uname.getBytes("iso8859-1"),"utf-8");* 2、使用公共配置* get方式:* 步驟一:req.setCharacterEncoding("utf-8");* 步驟二:* 在tomcat的目錄下的conf目錄中修改server.xml文件:在Connector標(biāo)簽中增加屬性 useBodyEncodingForURI="true"* post方式:* req.setCharacterEncoding("utf-8");* Servlet流程總結(jié):* 瀏覽器發(fā)起請(qǐng)求到服務(wù)器(請(qǐng)求)* 服務(wù)器接受瀏覽器的請(qǐng)求,進(jìn)行解析,創(chuàng)建request對(duì)象存儲(chǔ)請(qǐng)求數(shù)據(jù)* 服務(wù)器調(diào)用對(duì)應(yīng)的servlet進(jìn)行請(qǐng)求處理,并將request對(duì)象作為實(shí)參傳遞給servlet的方法* servlet的方法執(zhí)行進(jìn)行請(qǐng)求處理* //設(shè)置請(qǐng)求編碼格式* //設(shè)置響應(yīng)編碼格式* //獲取請(qǐng)求信息* //處理請(qǐng)求信息* //創(chuàng)建業(yè)務(wù)層對(duì)象* //調(diào)用業(yè)務(wù)層對(duì)象的方法* //響應(yīng)處理結(jié)果*請(qǐng)求轉(zhuǎn)發(fā)學(xué)習(xí):* 作用:實(shí)現(xiàn)多個(gè)servlet聯(lián)動(dòng)操作處理請(qǐng)求,這樣避免代碼冗余,讓servlet的職責(zé)更加明確。* 使用:* req.getRequestDispatcher("要轉(zhuǎn)發(fā)的地址").forward(req, resp);* 地址:相對(duì)路徑,直接書(shū)寫(xiě)servlet的別名即可。* 特點(diǎn):* 一次請(qǐng)求,瀏覽器地址欄信息不改變。* 注意:* 請(qǐng)求轉(zhuǎn)發(fā)后直接return結(jié)束即可。*request作用域:* 解決了一次請(qǐng)求內(nèi)的servlet的數(shù)據(jù)共享問(wèn)題*重定向:* 解決了表單重復(fù)提交的問(wèn)題,以及當(dāng)前servlet無(wú)法處理的請(qǐng)求的問(wèn)題。* 使用:* resp.sendRedirect(String uri);* 示例:* resp.sendRedirect("/login/main");* 特點(diǎn):* 兩次請(qǐng)求,兩個(gè)request對(duì)象。* 瀏覽器地址欄信息改變* 時(shí)機(jī):* 如果請(qǐng)求中有表單數(shù)據(jù),而數(shù)據(jù)又比較重要,不能重復(fù)提交,建議使用重定向。* 如果請(qǐng)求被Servlet接收后,無(wú)法進(jìn)行處理,建議使用重定向定位到可以處理的資源。 ** @author Administrator**/ public class LoginServlet extends HttpServlet {@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {//設(shè)置請(qǐng)求編碼格式:req.setCharacterEncoding("utf-8");//設(shè)置響應(yīng)編碼格式resp.setContentType("text/html;charset=utf-8");//獲取請(qǐng)求信息String uname=req.getParameter("uname");//使用String進(jìn)行數(shù)據(jù)重新編碼 // uname=new String(uname.getBytes("iso8859-1"),"utf-8"); //解決了亂碼問(wèn)題String pwd=req.getParameter("pwd");System.out.println(uname+":"+pwd);//處理請(qǐng)求信息//獲取業(yè)務(wù)層對(duì)象LoginService ls=new LoginServiceImpl();User u=ls.checkLoginService(uname, pwd);System.out.println(u);//響應(yīng)處理結(jié)果if(u!=null){resp.getWriter().write("登錄成功");}else{//resp.getWriter().write("登錄失敗"); // resp.getWriter().write("<html>"); // resp.getWriter().write("<head>"); // resp.getWriter().write("</head>"); // resp.getWriter().write("<body>"); // resp.getWriter().write("<form action='login' method='get'>"); // resp.getWriter().write("用戶(hù)名:<input type='text' name='uname' value=''/><br/>"); // resp.getWriter().write("密碼:<input type='password' name='pwd' value=''/><br/>"); // resp.getWriter().write("<input type='submit' value='登錄'/><br/>"); // resp.getWriter().write("</form>"); // resp.getWriter().write("</body>"); // resp.getWriter().write("</html>");//使用request對(duì)象實(shí)現(xiàn)不同servlet的數(shù)據(jù)流轉(zhuǎn)req.setAttribute("str", "用戶(hù)名或密碼錯(cuò)誤");//使用請(qǐng)求轉(zhuǎn)發(fā)req.getRequestDispatcher("page").forward(req, resp);//請(qǐng)求轉(zhuǎn)發(fā)后直接return結(jié)束即可。return;}} }重定向
問(wèn)題:如果當(dāng)前的請(qǐng)求,Servlet 無(wú)法進(jìn)行處理怎么辦?
? ? ? ? ? 如果使用請(qǐng)求轉(zhuǎn)發(fā),造成表單數(shù)據(jù)重復(fù)提交怎么辦?
解決:使用重定向
使用:response.sendRedirect(“路徑”).
本地路徑為:uri
網(wǎng)絡(luò)路徑為:定向資源的 URL 信息
特點(diǎn):兩次請(qǐng)求
? ? ? ? ? ?瀏覽器地址欄信息改變
? ? ? ? ? ?避免表單重復(fù)提交
*重定向:
?*?? ??? ?解決了表單重復(fù)提交的問(wèn)題,以及當(dāng)前servlet無(wú)法處理的請(qǐng)求的問(wèn)題。
?*?? ??? ?使用:
?*?? ??? ??? ?resp.sendRedirect(String uri);
?*?? ??? ?示例:
?*?? ??? ??? ?resp.sendRedirect("/login/main");
?*?? ??? ?特點(diǎn):
?*?? ??? ??? ?兩次請(qǐng)求,兩個(gè)request對(duì)象。
?*?? ??? ??? ?瀏覽器地址欄信息改變
?*?? ??? ?時(shí)機(jī):
?*?? ??? ??? ?如果請(qǐng)求中有表單數(shù)據(jù),而數(shù)據(jù)又比較重要,不能重復(fù)提交,建議使用重定向。
?*?? ??? ??? ?如果請(qǐng)求被Servlet接收后,無(wú)法進(jìn)行處理,建議使用重定向定位到可以處理的資源。
LoginServlet.java
package com.dym.servlet;import java.io.IOException;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import com.dym.pojo.User; import com.dym.service.LoginService; import com.dym.service.impl.LoginServiceImpl;/***請(qǐng)求中文亂碼解決:* 1、使用String進(jìn)行數(shù)據(jù)重新編碼 核心數(shù)據(jù)用這種方式* uname=new String(uname.getBytes("iso8859-1"),"utf-8");* 2、使用公共配置* get方式:* 步驟一:req.setCharacterEncoding("utf-8");* 步驟二:* 在tomcat的目錄下的conf目錄中修改server.xml文件:在Connector標(biāo)簽中增加屬性 useBodyEncodingForURI="true"* post方式:* req.setCharacterEncoding("utf-8");* Servlet流程總結(jié):* 瀏覽器發(fā)起請(qǐng)求到服務(wù)器(請(qǐng)求)* 服務(wù)器接受瀏覽器的請(qǐng)求,進(jìn)行解析,創(chuàng)建request對(duì)象存儲(chǔ)請(qǐng)求數(shù)據(jù)* 服務(wù)器調(diào)用對(duì)應(yīng)的servlet進(jìn)行請(qǐng)求處理,并將request對(duì)象作為實(shí)參傳遞給servlet的方法* servlet的方法執(zhí)行進(jìn)行請(qǐng)求處理* //設(shè)置請(qǐng)求編碼格式* //設(shè)置響應(yīng)編碼格式* //獲取請(qǐng)求信息* //處理請(qǐng)求信息* //創(chuàng)建業(yè)務(wù)層對(duì)象* //調(diào)用業(yè)務(wù)層對(duì)象的方法* //響應(yīng)處理結(jié)果*請(qǐng)求轉(zhuǎn)發(fā)學(xué)習(xí):* 作用:實(shí)現(xiàn)多個(gè)servlet聯(lián)動(dòng)操作處理請(qǐng)求,這樣避免代碼冗余,讓servlet的職責(zé)更加明確。* 使用:* req.getRequestDispatcher("要轉(zhuǎn)發(fā)的地址").forward(req, resp);* 地址:相對(duì)路徑,直接書(shū)寫(xiě)servlet的別名即可。* 特點(diǎn):* 一次請(qǐng)求,瀏覽器地址欄信息不改變。* 注意:* 請(qǐng)求轉(zhuǎn)發(fā)后直接return結(jié)束即可。*request作用域:* 解決了一次請(qǐng)求內(nèi)的servlet的數(shù)據(jù)共享問(wèn)題*重定向:* 解決了表單重復(fù)提交的問(wèn)題,以及當(dāng)前servlet無(wú)法處理的請(qǐng)求的問(wèn)題。* 使用:* resp.sendRedirect(String uri);* 示例:* resp.sendRedirect("/login/main");* 特點(diǎn):* 兩次請(qǐng)求,兩個(gè)request對(duì)象。* 瀏覽器地址欄信息改變* 時(shí)機(jī):* 如果請(qǐng)求中有表單數(shù)據(jù),而數(shù)據(jù)又比較重要,不能重復(fù)提交,建議使用重定向。* 如果請(qǐng)求被Servlet接收后,無(wú)法進(jìn)行處理,建議使用重定向定位到可以處理的資源。 ** @author Administrator**/ public class LoginServlet extends HttpServlet {@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {//設(shè)置請(qǐng)求編碼格式:req.setCharacterEncoding("utf-8");//設(shè)置響應(yīng)編碼格式resp.setContentType("text/html;charset=utf-8");//獲取請(qǐng)求信息String uname=req.getParameter("uname");//使用String進(jìn)行數(shù)據(jù)重新編碼 // uname=new String(uname.getBytes("iso8859-1"),"utf-8"); //解決了亂碼問(wèn)題String pwd=req.getParameter("pwd");System.out.println(uname+":"+pwd);//處理請(qǐng)求信息//獲取業(yè)務(wù)層對(duì)象LoginService ls=new LoginServiceImpl();User u=ls.checkLoginService(uname, pwd);System.out.println(u);//響應(yīng)處理結(jié)果if(u!=null){//請(qǐng)求轉(zhuǎn)發(fā)//req.getRequestDispatcher("main").forward(req, resp);//重定向resp.sendRedirect("/login/main");return;}else{//使用request對(duì)象實(shí)現(xiàn)不同servlet的數(shù)據(jù)流轉(zhuǎn)req.setAttribute("str", "用戶(hù)名或密碼錯(cuò)誤");//使用請(qǐng)求轉(zhuǎn)發(fā)req.getRequestDispatcher("page").forward(req, resp);//請(qǐng)求轉(zhuǎn)發(fā)后直接return結(jié)束即可。return;}} }MainServlet.java
package com.dym.servlet;import java.io.IOException;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;public class MainServlet extends HttpServlet {@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {//設(shè)置請(qǐng)求編碼格式req.setCharacterEncoding("utf-8");//設(shè)置響應(yīng)編碼格式resp.setContentType("text/html;charset=utf-8");//獲取請(qǐng)求信息//處理請(qǐng)求信息//響應(yīng)處理結(jié)果resp.getWriter().write("<html>");resp.getWriter().write("<head>");resp.getWriter().write("</head>");resp.getWriter().write("<body>");resp.getWriter().write("<h3>歡迎"+req.getParameter("uname")+"訪(fǎng)問(wèn)dym管理系統(tǒng)</h3>");resp.getWriter().write("<hr>");resp.getWriter().write("</body>");resp.getWriter().write("</html>");} }總結(jié)
以上是生活随笔為你收集整理的Servlet流程总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: servlet——请求乱码问题解决
- 下一篇: Cookie 学习案例之三天免登录