Treatment mode:
Interface Main party
(1) Configure the appropriate servlet in Web.xml to handle all requests
<servlet>
<servlet-name>hessianServerServlet</servlet-name>
<servlet-class>com.psychcn.cmi3.web.servlet.HessianServerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hessianServerServlet</servlet-name>
<url-pattern>/hessian/*</url-pattern>
</servlet-mapping>
Writing a servlet
Import java.io.IOException;
Import Java.util.concurrent.ConcurrentHashMap;
Import Java.util.concurrent.ConcurrentMap;
Import Javax.servlet.ServletConfig;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.remoting.caucho.HessianServiceExporter;
Import Org.springframework.web.util.UrlPathHelper;
Import Com.psychcn.util.SpringUtils;
public class Hessianserverservlet extends HttpServlet {
Private static final long serialversionuid = -7579598882061647014l;
concurrentmap<string, hessianserviceexporter> hessianexportermappings =
New concurrenthashmap<string, hessianserviceexporter> ();
Urlpathhelper urlpathhelper = new Urlpathhelper ();
Private ApplicationContext AC = null;
@Override
protected void Service (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
String Requestpath = urlpathhelper.getpathwithinservletmapping (request);
if (requestpath!= null && requestpath.length () > 1) {
Hessianserviceexporter exporter = Hessianexportermappings.get (Requestpath);
if (exporter!= null) {
Exporter.handlerequest (request, response);
} else {
Response.setcharacterencoding ("Utf-8");
Response.getwriter (). println ("Do not find the appropriate service, please check that the Hessian URL is correct (error URL: + requestpath +)");
}
} else {
Response.setcharacterencoding ("Utf-8");
Response.getwriter (). println ("The root path is requested, please check that the Hessian URL is configured correctly");
}
}
@Override
public void init (ServletConfig config) throws servletexception {
This.ac = Springutils.getwebapplicationcontext (Config.getservletcontext ());
if (this.ac!= null) {
To cache the Hessianserviceexporter defined in the spring container in this instance variable
This.hessianExporterMappings.putAll (Ac.getbeansoftype (Hessianserviceexporter.class));
} else {
Super.log ("Error, Hessian service component cannot get Spring context");
}
}
@Override
public void Destroy () {
AC = null;
Hessianexportermappings.clear ();
Super.destroy ();
}}
Configuration in the spring XML
<bean id= "Cmiservice" class= "Com.psychcn.cmi3.remoting.hessian.CmiServiceHessianImpl" >
<constructor-arg index= "0" ref= "Maindatasource"/>
</bean>
<bean name= "/cmiservice" class= "Org.springframework.remoting.caucho.HessianServiceExporter" >
<property name= "service" ref= "Cmiservice"/>
<property name= "Serviceinterface" value= "Com.psychcn.cmi3.remoting.CmiService"/>
</bean>
Write the appropriate interface that implements the class:
Public interface Cmiservice {}
public class Cmiservicehessianimpl extends Serviceimpl implements Cmiservice, Initializingbean, Applicationcontextaware {}