您好,登录后才能下订单哦!
利用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>
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。