Interface communication between Java Systems 1

Source: Internet
Author: User

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 {}

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.