[转载]RequestDispatcher.include(String str)用法。
来源:互联网 发布:电子地图矢量数据下载 编辑:程序博客网 时间:2024/06/08 17:40
只是在书上看到了RequestDispatcher对象的Include方法可以将多个单功能的文件例如.jsp文件整合成一个Servlet文件
今天上网找到一个很好的实例来说明这一点。很感谢此作者!^_^
public class TestDispatcher extends javax.servlet.http.HttpServlet implements
javax.servlet.Servlet ......{
public TestDispatcher() ......{
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException ......{
doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException ......{
RequestDispatcher rd ;
rd = request.getRequestDispatcher("TestDispatcher1");
rd.include(request, response);
//String hello = (String) request.getAttribute("hello");
//System.out.println(hello);
rd = request.getRequestDispatcher("/test/test51.jsp");
rd.include(request, response);
rd = request.getRequestDispatcher("/test/test5.jsp");
rd.include(request, response);
}
}
javax.servlet.Servlet ......{
public TestDispatcher() ......{
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException ......{
doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException ......{
RequestDispatcher rd ;
rd = request.getRequestDispatcher("TestDispatcher1");
rd.include(request, response);
//String hello = (String) request.getAttribute("hello");
//System.out.println(hello);
rd = request.getRequestDispatcher("/test/test51.jsp");
rd.include(request, response);
rd = request.getRequestDispatcher("/test/test5.jsp");
rd.include(request, response);
}
}
public class TestDispatcher1 extends javax.servlet.http.HttpServlet implements
javax.servlet.Servlet ......{
public TestDispatcher1() ......{
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException ......{
doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException ......{
request.setAttribute("hello", "hello");
response.getWriter().write("this is servlet2<br>");
response.sendRedirect("http://www.csdn.net");
}
}
javax.servlet.Servlet ......{
public TestDispatcher1() ......{
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException ......{
doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException ......{
request.setAttribute("hello", "hello");
response.getWriter().write("this is servlet2<br>");
response.sendRedirect("http://www.csdn.net");
}
}
<%...@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%...@ taglib uri="/WEB-INF/c.tld" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<c:out value="${hello}"></c:out>
<br>
this is test51.jsp.
<br>
</body>
</html>
pageEncoding="ISO-8859-1"%>
<%...@ taglib uri="/WEB-INF/c.tld" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<c:out value="${hello}"></c:out>
<br>
this is test51.jsp.
<br>
</body>
</html>
<%...@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
this is test5.jsp.
</body>
</html>
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
this is test5.jsp.
</body>
</html>
输出结果: this is servlet2
hello
this is test51.jsp
this is test5.jsp
总结:servlet1中include另外一个servlet2,会计算servlet2,最终转向servlet1决定,servlet2中的request和response对象中存储的数据被和并到servlet1的request和response对象中。
servlet中include一个jsp页,相当于:jsp中<%@ include file="XXX.jsp" %>
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1805489
- [转载]RequestDispatcher.include(String str)用法。
- [转载]RequestDispatcher.include(String str)用法。
- [转载]RequestDispatcher.include(String str)用法。
- RequestDispatcher.include(String str);用法。
- RequestDispatcher.include(String str)用法
- 【转载】JSP中RequestDispatcher的用法
- String.indexof(str)的用法
- RequestDispatcher.include方法
- 关于requestdispatcher .include
- RequestDispatcher include与forward
- #include 后还是提示string str;string未定义的标识符
- c++ string c.str()的用法
- string 中 str.assign() 的用法
- str.charCodeAt()与String.fromCharCode()用法
- RequestDispatcher中的forward和include
- RequestDisPatcher的用法
- RequestDispatcher的用法
- Difference between forward and include of RequestDispatcher
- .Net类库中实现的HashTable
- AJAX的小应用 JS的HASH 传到后台 java
- 为Asp.net控件写单元测试(ViewState)
- Java字符集
- RESPONSE对象
- [转载]RequestDispatcher.include(String str)用法。
- JDBC报错:Connection refused(DESCRIPTION=(TMP=)(VSNNUM=150999297)(ERR=12505)(ERROR_STACK=(ERROR=(CODE=12505)(EMFI=4))))
- Eclipse开发struts完全指南 (版本较旧)
- C#中的构造函数
- 解决页面刷新后跳回顶部的问题
- My Page StartKit项目概览
- 断思,妄想,狂人
- C#中实现树的遍历
- Asp.net程序中生成Excel报表