本文为大家介绍了servlet转发、servlet包含,供大家参考,具体内容如下
1.什么是servlet转发?
servlet将用户的请求通过一组servlet来处理,每个servlet都有自己特定的职责,当该servlet完成自己的工作后将请求转发到下一个servlet来处理,这种通过不同servelet相互转发请求来完成对某个请求的处理过程就叫做servlet转发。
2.什么是servlet包含?
servlet包含就是源servlet将其他web组件(servlet,html,jsp),生成的结果包含到自己的结果中。
3.为什么需要servlet转发和包含?
servlet对象是由Servlet容器创建,Servlet对象的Service()方法是由servlet容器调用,servlet之间是不允许相互调用(servlet API 2.1以前版本可以)。但是我们又需要通过多个servlet组件协调完成任务,同时方便代码维护管理,提高开发效率。这样就可以需要servlet转发和包含的技术支持。
4、转发和包含的相同点
(1)都是对同一个请求对象处理,源组件和目标组件共享同一个ServletRequest对象和ServletResponse对象。
(2)目标组件都可以是servlet,jsp,html。
(3)都依赖javax,servlet.RequestDispatcher 接口。
5.RequestDIspatcher 对象
该接口表示请求分发器
(1)获取RequestDispatcher对象方法
调用ServletContext的getRequestDispather(String path)方法获得。
调用ServletRequest的getRequestDispather(String path)方法获得。
(2)两种方法的区别
ServletContext调用方法的参数必须为绝对路径,而ServletRequest调用方法参数可以是绝对路径也可以是相对路径。
6.请求转发案例
CheckServlet 类用于处理用户请求参数。
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 用于输出处理结果
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.在转发之前不要提交响应结果。
2.转发过程中,源组件的响应结果不会返回,只会返回目标组件的响应结果。
web.xml配置
<servlet> <servlet-name>check</servlet-name> <servlet-class>com.learn.CheckServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>check</servlet-name> <url-pattern>/check</url-pattern> </servlet-mapping> <servlet> <servlet-name>out</servlet-name> <servlet-class>com.learn.OutServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>out</servlet-name> <url-pattern>/out</url-pattern> </servlet-mapping>
7.包含(include)案例
MainServlet 用于包含其他组件,并返回响应结果
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("<html><head><title>mainServlet</title></head>"); out.println("<body>"); 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("</body></html>"); out.close(); } }
header.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>header</title> </head> <body> <font size="6">Welcome to ab</font> </body> </html>
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")+", <p>"); out.close(); } }
footer.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>footer</title> </head> <body> <span>Thanks for stopping</span> </body> </html>
web.xm配置
<servlet> <servlet-name>main</servlet-name> <servlet-class>com.learn.MainServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>main</servlet-name> <url-pattern>/main</url-pattern> </servlet-mapping> <servlet> <servlet-name>greet</servlet-name> <servlet-class>com.learn.GreetServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>greet</servlet-name> <url-pattern>/greet</url-pattern> </servlet-mapping>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。