Servlet与Jsp页面的通信
2013-05-23 14:31
375 查看
package javapapers; import java.io.IOException; import java.util.Vector; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletToJSP extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // communicating 一个简单的字符串 String message = "fuck中文 Example source code of Servlet to JSP communication."; request.setAttribute("message", message); // communicating 一个向量对象 Vector vecObj = new Vector(); vecObj.add("麻痹中文Servlet to JSP communicating 一个对象"); request.setAttribute("vecBean", vecObj); // Servlet JSP communication RequestDispatcher reqDispatcher = getServletConfig() .getServletContext().getRequestDispatcher("/jsp/NewFile.jsp"); reqDispatcher.forward(request, response); } }
<servlet> <servlet-name>obama</servlet-name> <servlet-class>javapapers.ServletToJSP</servlet-class> </servlet> <servlet-mapping> <servlet-name>obama</servlet-name> <url-pattern>/gplus</url-pattern> </servlet-mapping>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import="java.util.Vector"%> <!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=utf-8"> <title>西安Java培训西安小升初</title> </head> <body> <% String message = (String) request.getAttribute("message"); out.println("Servlet communicated message to JSP: <br/>" + message); out.println("<br/>"); Vector vecObj = (Vector) request.getAttribute("vecBean"); out.println("Servlet to JSP communication of an object: <br/>" + vecObj.get(0)); %> </body> </html>
原文:http://javapapers.com/servlet/servlet-jsp-communication/
源代码:http://pan.baidu.com/share/link?shareid=466234&uk=3878681452
相关文章推荐
- question:applet与浏览器通信,同页面内applet之间通信,jsp控制applet异步运行,servlet启动applet方法 jsp获取applet返回值
- 从Servlet传值给JSP页面
- 关于springMVC和传统servlet框架从jsp页面向后台请求url问题
- 在JSP表单提交中怎样在Servlet中处理文件上传并且获取JSP页面传过来的一些参数
- 用servlet如何实现jsp登陆页面的跳转??搜索
- jsp页面提示“Multiple annotations found at this line: - The superclass "javax.servlet.http.HttpServlet" w
- 页面访问时发生错误: java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config
- Servlet与jsp间的传值,页面刷新和iframesession失效问题
- JSP如何在servlet将一个数据模型对象传递给jsp页面
- Servlet和JSP的通信
- servlet 重定向到jsp 页面
- JSP与Servlet之后台页面单条删除与多条删除的页面跳转之实现
- Servlet_JSP(3) Servlet和JSP的通信
- MyEclipse设置Servlet模板和JSP页面默认编码
- Eclipse: 在JSP 页面中 找不到类 javax.servlet.http.HttpServletResponse
- 马士兵 Servlet_JSP(3) Servlet和JSP的通信(源代码)
- jsp页面引入css文件报错 javax.servlet cannot be resolved to a type
- jsp页面出现Multiple annotations found at this line: - The superclass "javax.servlet.http.HttpServlet" w
- jsp页面、servlet显示中文乱码详解
- 关于springMVC和传统servlet框架从jsp页面向后台请求url问题