利用servlet怎么實(shí)現(xiàn)一個(gè)轉(zhuǎn)發(fā)功能?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
湘西土家族ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書(shū)未來(lái)市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)建站的ssl證書(shū)銷(xiāo)售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書(shū)合作)期待與您的合作!
1.什么是servlet轉(zhuǎn)發(fā)?
servlet將用戶的請(qǐng)求通過(guò)一組servlet來(lái)處理,每個(gè)servlet都有自己特定的職責(zé),當(dāng)該servlet完成自己的工作后將請(qǐng)求轉(zhuǎn)發(fā)到下一個(gè)servlet來(lái)處理,這種通過(guò)不同servelet相互轉(zhuǎn)發(fā)請(qǐng)求來(lái)完成對(duì)某個(gè)請(qǐng)求的處理過(guò)程就叫做servlet轉(zhuǎn)發(fā)。
2.什么是servlet包含?
servlet包含就是源servlet將其他web組件(servlet,html,jsp),生成的結(jié)果包含到自己的結(jié)果中。
3.為什么需要servlet轉(zhuǎn)發(fā)和包含?
servlet對(duì)象是由Servlet容器創(chuàng)建,Servlet對(duì)象的Service()方法是由servlet容器調(diào)用,servlet之間是不允許相互調(diào)用(servlet API 2.1以前版本可以)。但是我們又需要通過(guò)多個(gè)servlet組件協(xié)調(diào)完成任務(wù),同時(shí)方便代碼維護(hù)管理,提高開(kāi)發(fā)效率。這樣就可以需要servlet轉(zhuǎn)發(fā)和包含的技術(shù)支持。
4、轉(zhuǎn)發(fā)和包含的相同點(diǎn)
(1)都是對(duì)同一個(gè)請(qǐng)求對(duì)象處理,源組件和目標(biāo)組件共享同一個(gè)ServletRequest對(duì)象和ServletResponse對(duì)象。
(2)目標(biāo)組件都可以是servlet,jsp,html。
(3)都依賴javax,servlet.RequestDispatcher 接口。
5.RequestDIspatcher 對(duì)象
該接口表示請(qǐng)求分發(fā)器
(1)獲取RequestDispatcher對(duì)象方法
調(diào)用ServletContext的getRequestDispather(String path)方法獲得。
調(diào)用ServletRequest的getRequestDispather(String path)方法獲得。
(2)兩種方法的區(qū)別
ServletContext調(diào)用方法的參數(shù)必須為絕對(duì)路徑,而ServletRequest調(diào)用方法參數(shù)可以是絕對(duì)路徑也可以是相對(duì)路徑。
6.請(qǐng)求轉(zhuǎn)發(fā)案例
CheckServlet 類(lèi)用于處理用戶請(qǐng)求參數(shù)。
package com.learn; 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; import java.io.PrintWriter; /** * Created by Administrator on 2017/09/26. */ public class CheckServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String userName = req.getParameter("name"); String message = ""; if(userName == null){ message = "please input your name"; } else { message = userName; } req.setAttribute("mess",message); RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("/out"); PrintWriter out = resp.getWriter(); out.print("before forward "); System.out.println("before forward"); requestDispatcher.forward(req,resp); out.print("after forward"); System.out.println("after forward"); out.close(); } }
OutServlet 用于輸出處理結(jié)果
package com.learn; import org.json.HTTP; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; /** * Created by Administrator on 2017/09/26. */ public class OutServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String message = (String) req.getAttribute("mess"); PrintWriter printWriter = resp.getWriter(); printWriter.print(message); printWriter.close(); } }
注意: 1.在轉(zhuǎn)發(fā)之前不要提交響應(yīng)結(jié)果。
2.轉(zhuǎn)發(fā)過(guò)程中,源組件的響應(yīng)結(jié)果不會(huì)返回,只會(huì)返回目標(biāo)組件的響應(yīng)結(jié)果。
web.xml配置
check com.learn.CheckServlet check /check out com.learn.OutServlet out /out
7.包含(include)案例
MainServlet 用于包含其他組件,并返回響應(yīng)結(jié)果
package com.learn; import javax.servlet.RequestDispatcher; import javax.servlet.Servlet; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; /** * Created by Administrator on 2017/09/26. */ public class MainServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html"); PrintWriter out = resp.getWriter(); out.println("mainServlet "); out.println(""); ServletContext servletContext = getServletContext(); RequestDispatcher headDisPatcher = servletContext.getRequestDispatcher("/header.html"); RequestDispatcher greetDisPatcher = servletContext.getRequestDispatcher("/greet"); RequestDispatcher footeDisPatcher = servletContext.getRequestDispatcher("/footer.html"); headDisPatcher.include(req,resp); greetDisPatcher.include(req,resp); footeDisPatcher.include(req,resp); out.print(""); out.close(); } }
header.html
header Welcome to ab
GreetServlet
package com.learn; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; /** * Created by Administrator on 2017/09/26. */ public class GreetServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { PrintWriter out = resp.getWriter(); out.print("hi! "+req.getParameter("name")+","); out.close(); } }
footer.html
footer Thanks for stopping
web.xm配置
main com.learn.MainServlet main /main greet com.learn.GreetServlet greet /greet
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。