Download the source code of shared objects for login users under the J2EE Application Server

Source: Internet
Author: User

Sometimes it is necessary to implement some monitoring and development under the Java Web application server and operate on multiple terminals. The efficiency of sharing information through databases is very low and inconvenient, therefore, you must create a shared object in the memory for the terminal to access,

Implementation process:

Set web. XML: specifies that a servlet automatically creates a global object for the servlet when the web application starts. Users can access the servlet to share objects under the servlet, you can transfer an object to JSP through session to share the object with JSP.

 

The object to be shared: Raise info. Java, private variable Val accumulation and extraction.

 

/***/Package test;/*** @ author administrator **/Public Class Interval info {private int val = 0;/*****/Public interval Info () {// todo auto-generated constructor stub}/*** accumulate */Public void add () {val = Val + 1 ;} /*** value: ** @ return */Public int getval () {return val ;}}

Servlet loaded at startup

 

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. using Info;/*** servlet implementati On class info */@ webservlet (name = "info", urlpatterns = {"/INFO"}) public class info extends httpservlet {Private Static final long serialversionuid = 1l; // declare the shared object so that the object accessed by each terminal will be the same private synchronized info into info = NULL;/*** @ see httpservlet # httpservlet () */public info () {super (); // todo auto-generated constructor stub}/*** @ see servlet # Init (servletconfig) */Public void Init (servletconfi G config) throws servletexception {// todo auto-generated method stub // initialize the shared object by the web. XML indicates that the servlet executes user-independent response info = new response Info ();}/*** @ see httpservlet # doget (httpservletrequest request, httpservletresponse * response) during deployment or service start) */protected void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {// todo auto-generated method stub Dopost (request, response);}/*** @ see httpservlet # dopost (httpservletrequest request, response * response) */protected void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {// todo auto-generated method stub // The object is shared // The number in the object is accumulated into info. add (); // save it to the session, and the JSP page will also be shared. // Note: every terminal that needs to access this object under JSP must complete the first access to this servlet, save the object to your own se In ssion, you can obtain this object in your JSP. Httpsession = request. getsession (); httpsession. setattribute ("info", cmdinfo); // returns the browser processrequest (string. valueof (partition info. 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 ();}}}

Set the Web. XML for starting the servlet. You do not need to declare the map, the key sentence load-on-startup, 1 is the priority, 0 is the highest.

 

<servlet><servlet-name>Info</servlet-name><servlet-class>servlet.Info</servlet-class><load-on-startup>1</load-on-startup></servlet>

 

Obtain the shared JSP, provided that the user has logged on through the servlet or accessed the servlet first.

 

<% @ Page import = "test. pageencoding" %> <% @ 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. raise Info "%> <% raise info = NULL; object = session. getattribute ("info"); If (object! = NULL) {response info = (Response info) object; Response info. add ();} else {out. print ("no object found, the user needs to access the servlet to get the session assignment"); return ;} %> <HTML> 

 

Objects can be shared among multiple servlets in a similar way.
 

Source code download

Http://download.csdn.net/detail/joyous/5345508

 

Q discussion: 236201801

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.