Servlet and Web Services

Source: Internet
Author: User
Tags define comments config empty execution soap stack trace web services
The Servlet|web|web service servlet plays a big role in Web services, parsing soap files (messages and attachment envelopes), using WSDL to validate SOAP messages, and so on. As a basic programming, you can use a servlet to parse SOAP messages, especially for SOAP messages with attachments, which is transparent, but the servlet cannot serve as a Web service, and of course the extended JAXM can be done. Now those projects that support Web services are implementing the servlet's functionality at the bottom to complete http+soap communication without requiring programmers to consider the communication process based on HTTP SOAP messages (no such misunderstanding, HTTP communication on the Java Platform Service program is servlet/jsp, and now you do not use servlet and JSP to communicate, it is not a contradiction? The fact is not contradictory, remember that the bottom has achieved the servlet function, the specific communication has its own management, directly up to the business logic of the service programming. Of course, you can sometimes write a servlet that parses a SOAP message, and that's just soap traffic (a servlet cannot be described as a service).

The following configuration files describe some of the underlying relationships to Web services:

      Review the servlet mapping pattern. We know that the servlet inherits from Javax.servlet.http.HttpServlet, is loaded into the JVM execution on the server side, and then outputs the HTML stream to the client.
servlet web.xml file (in  webapps/foo/web-inf directory):

<?xml version= "1.0"  encoding= "UTF-8" "
<! doctype web-app public  "-//sun microsystems, inc.//dtd web application  2.2//en "
                           "Http://java.sun.com/j2ee/dtds/web-app_2.2.dtd"
<web-app>
<servlet-mapping>
<servlet-name>invoker</servlet-name>
< Url-pattern>/servlet/*</url-pattern>
</servlet-mapping>
</web-app>
invoker  servlet  is actually: Org.apache.catalina.servlets.InvokerServlet
provides a small service program by class name. For example, if you call  foo/servlet/helloservlet,
Invoker servlet loads the HelloServlet (if it is in its classpath) and executes it.

Looking at the web.xml above, it seems that only a servlet mapping is given, but no invoker servlet is defined.
In fact, the invoker servlet is defined in the Web.xml in Tomcat's conf directory::
<servlet>
<servlet-name>invoker</servlet-name>
<servlet-class>
Org.apache.catalina.servlets.InvokerServlet
</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
So, if we put aside the tomcat_home/conf/web.xml, we would define a web.xml, which seems to be a clearer indication of the problem:
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Web-app Public "-//sun Microsystems, INC.//DTD Web Application 2.2//en"
"Http://java.sun.com/j2ee/dtds/web-app_2.2.dtd" >
<web-app>
<servlet-name>MyInvoker</servlet-name>
<servlet-class>
Org.apache.catalina.servlets.InvokerServlet
</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyInvoker</servlet-name>
<url-pattern>/servlet/*</url-pattern>
</servlet-mapping>
</web-app>
That is, all/servlet/* mode URL, will be handed to Org.apache.catalina.servlets.InvokerServlet to handle.
Or, all/servlet/* pattern URLs are actually calls to the Invokerservlet class, and Invokerservlet itself is
A servlet, which is also inherited from HttpServlet.
In this way, our own servlet can be executed through a specific URL, that is,/servlet/ourservlet.
Of course, if you're happy, you can define any URL pattern, not necessarily the/servlet/*, which, as we are behind
See the way axis handles SOAP messages.

Further, if you don't want Invokerservlet to "get in the middle", we can of course define our own servlet directly:

<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Web-app Public "-//sun Microsystems, INC.//DTD Web Application 2.2//en"
"Http://java.sun.com/j2ee/dtds/web-app_2.2.dtd" >
<web-app>
<servlet-name>MyInvoker2</servlet-name>
<servlet-class>
Com.foo.MyServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyInvoker2</servlet-name>
<url-pattern>/AnyName/*</url-pattern>
</servlet-mapping>
</web-app>
JSP is the same reason, with the above analysis,
Look at the following statements in the Tomcat_home/conf/web.xml can be JSP processing method, here is no longer nonsense:
....
<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
<init-param>
<param-name>logVerbosityLevel</param-name>
<param-value>WARNING</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
....
Get to the point.
Let's take a look at the web.xml that deploys the Web service:
<?xml version= "1.0" encoding= "Iso-8859-1"?>
<! DOCTYPE Web-app Public "-//sun Microsystems, INC.//DTD Web Application 2.2//en" "Http://java.sun.com/j2ee/dtds/web-app_ 2_2.dtd ">
<web-app>
<servlet>
<servlet-name>Axis</servlet-name>
<!--the actual servlet program, this is axisservlet-->
<servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
</servlet>

<!--### defines the corresponding relationship between a servlet and a URL-->

<servlet-mapping>
<servlet-name>Axis</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>

</web-app>


All/services/* mode URLs will be handed to org.apache.axis.transport.http.AxisServlet for processing,
Axisservlet, of course, was inherited from HttpServlet. This is why the Web services that we deploy are invoked in the
The service name is preceded by a services/.

As you can say, Axisservlet is the gateway to all Web service calls.
So what does Axisservlet do when he takes over a Web service call?

The client uses Call.invoke () to invoke the Web service with post, so the entry is axisservlet.dopost ...
Rather than axisservlet.doget ...


Let's take a look at Axisservlet's dopost function, which gives you only the key statements and comments:

/**
* Process a POST to the servlet by handing it out to the Axis Engine.
* Here is where SOAP messages are received
* @param req Posted Request
* @param res respose
* @throws servletexception Trouble
* @throws IOException different trouble
*/
public void DoPost (HttpServletRequest req, httpservletresponse Res)
Throws Servletexception, IOException
{
Msgcontext = Createmessagecontext (engine, req, res);//Get Customer request information

Engine.invoke (Msgcontext); Calling client-requested services

Responsemsg = Msgcontext.getresponsemessage ()//The return result of the call

Sendresponse (Getprotocolversion (req), ContentType, res, responsemsg);//Send the results to the client
}

In this way, the ins and outs of Web service invocations are generally clear ...

In order to clear the front of our three URLs
Http://192.168.0.1/test/services
http://192.168.0.1/test/services/sayHelloService?wsdl
Http://192.168.0.1/test/services/sayHelloService?method=sayHelloTo&aname=everybody
How to get the output, and then look at the Axisservlet doget function, here only gives the process framework and comments:

**
* Process GET requests. Because Axis does not support the Get-style
* Pseudo execution of SOAP methods, this handler deals with queries
* of various kinds, not real SOAP actions.
*
* @todo for secure installations, dont stack trace on faults
* @param request Request in
* @param response Request out
* @throws servletexception
* @throws IOException
*/
public void Doget (HttpServletRequest req, httpservletresponse Res)
Throws Servletexception, IOException
{

If the path is empty, for example: Http://localhost/wstk/services or http://localhost/wstk/services/*
if ((PathInfo = null | | pathinfo.equals ("")) &&!realpath.endswith (". JWs"))

{
Read all deployed service information from the SERVER-CONFIG.WSDD file and list all deployed services to the client.
Include methods that each service can invoke.

}else
If the path is not empty, for example: Http://localhost/wstk/services/sayHelloService
if (Realpath!= null)
{
If WSDL is requested, for example: http://localhost/wstk/services/sayHelloService?wsdl
if (wsdlrequested)
{
Create a sayhelloservice WSDL file and route it to the client
} else
This is the portal that uses URLs to invoke Web services, such as Http://192.168.0.1/test/services/sayHelloService?method=sayHelloTo&aname=everybody
if (Req.getparameternames (). hasMoreElements ())
{
If the method invoked by the client is correct, axis invokes the corresponding JavaBean and returns the result of the JavaBean
Encapsulated as a SOAP message stream returned to the client.
}
}
}


And how does axis find the JavaBean we're asking for? The answer is SERVER-CONFIG.WSDD file.

Server-config.wsdd

<?xml version= "1.0" encoding= "UTF-8"?>
<deployment xmlns:java= "Http://xml.apache.org/axis/wsdd/providers/java" xmlns= "http://xml.apache.org/axis/wsdd/" >

<service name= "Sayhelloservice" provider= "Java:rpc" >
<parameter name= "ClassName" value= "SayHello"/>
<parameter name= "Allowedmethods" value= "Sayhelloto"/>
</service>


<transport name= "http" >
<requestFlow>
</requestFlow>
</transport>

</deployment>

WSDD is the abbreviation for Web service Deployment descriptor.

The outermost <deployment> element indicates that this is WSDD and defines the Java namespace.

The next <service> element defines the service. A service is a target chain that includes request requests, content provider provider, and response response.
In this example, we point out that the service name is Sayhelloservice and provider is "JAVA:RPC", which is the axis tag that indicates this is a Java RPC service,
and the real class to deal with it is org.apache.axis.providers.java.RPCProvider.

Then we'll tell Rpcprovider in <parameter> how it instantiates and invokes the correct class (such as: Com.foo.MyService).
The classname of the <parameter> element indicates the class name, and Allowedmethods tells the engine that the shared methods are invoked through SOAP.
"*" means all public methods, and we also list the method names, which can be separated by spaces or commas.



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.