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