Implementation Principles of servlet, JSP, and axis WebService

Source: Internet
Author: User

Reference a good article on the network

Implementation Principles of servlet, JSP, and axis WebService

Review the servlet ing mode.

As we know, servlets are inherited from javax. servlet. http. httpservlet, loaded to the JVM on the server side for execution, and then output the HTML stream to the client.
Servlet's web. xml file (located in the 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
Provide small services by Class NameProgram. For example, if you call Foo/servlet/helloservlet,
Invoker servlet will load the helloservlet (if it is in its class path) and execute it.

Looking at the web. xml above, it seems that only one servlet ing is provided, and no invoker servlet is defined.
In fact, invoker servlet is defined in Web. XML in the conf directory of Tomcat ::
<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>

Therefore, if tomcat_home/CONF/Web. XML is left blank, we can define a web. xml file to better clarify 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 the URLs in the/servlet/* mode will be handed over to org. Apache. Catalina. servlets. invokerservlet for processing.
In other words, all the URLs in the/servlet/* mode call the invokerservlet class, And the invokerservlet itself is also
A servlet inherited from httpservlet.

In this way, our own servlet can be executed through a specific URL, that is,/servlet/ourservlet.
Of course, if you are happy, you can define any URL pattern, not necessarily/servlet /*.
The method that axis sees to process soap messages.

Further, if you do not want invokerservlet to be "zombie" in the middle, you can define your 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>

The same is true for JSP. With the above analysis,
Take a look at the following statement in tomcat_home/CONF/Web. XML to get the JSP processing method. Here we will not talk 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>
....

Next, go to the topic.

Let's first look at the web. XML for deploying web services:

<? XML version = "1.0" encoding = "ISO-8859-1"?>
<! Doctype web-app public "-// Sun Microsystems, Inc. // DTD web application 2.2 //" http://java.sun.com/j2ee/dtds/web-app_2_2.dtd ">

<Web-app>

<Servlet>
<Servlet-Name> AXIS </servlet-Name>
<! -- Actual servlet program. Here is axisservlet -->
<Servlet-class> org. Apache. axis. Transport. http. axisservlet </servlet-class>
</Servlet>

<! -- ### Define the correspondence between Servlet and URL -->

<Servlet-mapping>
<Servlet-Name> AXIS </servlet-Name>
<URL-pattern>/services/* </url-pattern>
</Servlet-mapping>

</Web-app>

All URLs in the/services/* mode will be handed over to org. Apache. axis. Transport. http. axisservlet for processing,
Axisservlet is also inherited from httpservlet. This is why the web service we deploy must be
Services/is added before the service name.

It can be said that axisservlet is the entrance to all web service calls.
So what does axisservlet do after it takes over Web service calls?

The client uses call. Invoke () to call the Web service using post, so the entry is axisservlet. dopost...
Instead of axisservlet. doget...

Let's take a look at axisservlet's dopost function. Here we only provide key statements and comments:

/**
* Process a post to the servlet by handing it off to the axis engine.
* Here is where soap messages are already ed
* @ 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 the customer request information

Engine. Invoke (msgcontext); // call the service requested by the client

Responsemsg = msgcontext. getresponsemessage (); // get the returned result of the call

Sendresponse (getprotocolversion (req), contenttype, res, responsemsg); // send the result to the client
}

In this way, the ins and outs of web service calls are roughly clear...

To better understand the preceding 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 obtain the output result. Let's take a look at the axisservlet doget function. Here we only provide 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 in
* @ Param Response Request Out
* @ Throws servletexception
* @ Throws ioexception
*/
Public void doget (httpservletrequest req, httpservletresponse res)
Throws servletexception, ioexception
{

// If the path is null, for example, http: // localhost/wstk/services or http: // localhost/wstk/services /*
If (pathinfo = NULL | pathinfo. Equals (""))&&! Realpath. endswith (". JWS "))

{
// Read information about all deployed services from the server-config.wsdd file and list all deployed services to the client,
// Includes the callable methods for each service.

} Else
// If the path is not empty, for example, http: // localhost/wstk/services/sayhelloservice
If (realpath! = NULL)
{
// If you request the WSDL, such as http: // localhost/wstk/services/sayhelloservice? WSDL
If (wsdlrequested)
{
// Create a sayhelloservice WSDL file and send it to the client
} Else
// The URL is used to call the Web service entry, such as http: // 192.168.0.1/test/services/sayhelloservice? Method = sayhelloto & aname = Everybody
If (req. getparameternames (). hasmoreelements ())
{
// If the method called by the client is correct, axis calls the corresponding JavaBean and returns the result of the JavaBean.
// Encapsulate the SOAP message stream and return it to the client.
}
}
}

How does axis find the requested JavaBean? The answer is the 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>

<Handler type = "Java: org. Apache. axis. Handlers. http. urlmapper" name = "urlmapper"/>

<Transport name = "HTTP">
<Requestflow>
<Handler type = "urlmapper"/>
</Requestflow>
</Transport>

</Deployment>

WSDD is the abbreviation of Web Service deployment descriptor.

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

The <service> element defines the service. A service is a target chain, including request, content provider, and response.
In this example, we point out that the service name is sayhelloservice, and the provider is "Java: RPC". It is the axis mark, indicating that this is a Java RPC service,
The real class for processing it is org. Apache. axis. providers. java. rpcprovider.

then we will tell rpcprovider in How to instantiate and call the correct class (for example, Com. Foo. myservice ).
the classname of the element indicates the class name. allowedmethods tells the engine that the common methods should be called through soap.
"*" indicates all public methods. We also list method names. You can separate them by spaces or commas.

Related Article

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.