原理:设置 web.xml,使 Servlet 在项目启动时自动创建对象,各用户访问 Servlet 可实现在 Servlet 下的对象共享,再通过 Session 可将 JSP 也实现对象共享。
将要共享的对象 ShareInfo.java
/** * */ package test; /** * @author Administrator * */ public class ShareInfo { private int val = 0; /** * */ public ShareInfo() { // TODO Auto-generated constructor stub } /** * 累加 */ public void add() { val = val + 1; } /** * 取值 * * @return */ public int getVal() { return val; } }
启动时加载的 Servlet
package servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import test.ShareInfo; /** * Servlet implementation class Info */ @WebServlet(name = "info", urlPatterns = { "/info" }) public class Info extends HttpServlet { private static final long serialVersionUID = 1L; // 声明共享对象 private ShareInfo shareInfo = null; /** * @see HttpServlet#HttpServlet() */ public Info() { super(); // TODO Auto-generated constructor stub } /** * @see Servlet#init(ServletConfig) */ public void init(ServletConfig config) throws ServletException { // TODO Auto-generated method stub // 创建共享对象 shareInfo = new ShareInfo(); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub // 对象得到共享 // 对象内数字累加 shareInfo.add(); // 存入 session,jsp 页面也将获得共享 HttpSession httpSession = request.getSession(); httpSession.setAttribute("INFO", shareInfo); processRequest(String.valueOf(shareInfo.getVal()), request, response); } /** * return text to explorer * * @param strMessage * @param request * @param response * @throws ServletException * @throws IOException */ protected void processRequest(String strMessage, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { out.print(strMessage); } finally { out.close(); } } }
设置启动 Servlet 的 web.xml
<servlet> <servlet-name>Info</servlet-name> <servlet-class>servlet.Info</servlet-class> <load-on-startup>1</load-on-startup> </servlet>
获得共享的 jsp
<%@page import="test.ShareInfo"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ page import="test.ShareInfo"%> <% ShareInfo shareInfo = null; Object object = session.getAttribute("INFO"); if (object != null) { shareInfo = (ShareInfo) object; shareInfo.add(); } else { out.print("没有找到对象,用户需要访问一次 servlet 获取 session 赋值"); return; } %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>GlassFish JSP Page</title> </head> <body> <%=shareInfo.getVal()%> </body> </html>
源码下载
http://download.csdn.net/detail/joyous/5345508
Q群讨论:236201801