servlet转发、包含详解(七)
本文为大家介绍了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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。