JAX-WS multiple endpoints in sun-jaxws.xml - java

Just started using JAX-WS. I created 2 test web services in the one WAR file as follows....
package com.djs;
import javax.jws.WebService;
#WebService()
public class AddTwoInts {
public int performAdd(int firstNum, int secondNum) {
return firstNum + secondNum;
}
}
And.....
package com.djs;
import javax.jws.WebService;
#WebService()
public class SayHello {
public String sayHello(String inwards) {
return "Hello " + inwards;
}
}
This is my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<listener>
<listener-class>
com.sun.xml.ws.transport.http.servlet.WSServletContextListener
</listener-class>
</listener>
<servlet>
<servlet-name>jaxws</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>jaxws</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
This is the sun-jaxws.xml
<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns='http://java.sun.com/xml/ns/jax-ws/ri/runtime' version='2.0'>
<endpoint name='performAdd' implementation='com.djs.AddTwoInts' url-pattern='/AddTwoInts' />
<endpoint name='sayHello' implementation='com.djs.SayHello' url-pattern='/SayHello' />
</endpoints>
I deploy into Tomcat 7 and use http://localhost:8080/MyApp/AddTwoInts?wsdl to get the WSDL for AddTwoInts OK.... But when I execute http://localhost:8080/MyApp/SayHello?wsdl I get a 404 page not found error....
Any advice appreciated.

Dave,
I guess you are missing the servlet-mapping for the two end points you have.
Add the following to your web.xml and try again. Let me know if this solve the problem.
<servlet>
<servlet-name>AddTwoInts</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddTwoInts</servlet-name>
<url-pattern>/AddTwoInts</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>SayHello</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SayHello</servlet-name>
<url-pattern>/SayHello</url-pattern>
</servlet-mapping>

You want the web.xml to reference only one servlet, at urlMapping /:
<servlet>
<servlet-name>services</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>services</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Then, include multiple endpoints at the full desired path in sun-jaxws.xml:
<endpoint name='performAdd' implementation='com.djs.AddTwoInts' url-pattern='/AddTwoInts' />
<endpoint name='sayHello' implementation='com.djs.SayHello' url-pattern='/couldhavemore/SayHello' />
Note the "couldhavemore" in there... you can add to the relevant path in the sun-jaxws.xml file to get the full desired path. I've gotten a single service to work with a web.xml entry of something other than /, but then you need a web.xml entry for every service. It seems to get multiple to work you need to use / and then put the full path in sun-jaxws.xml.

Related

why when rewrote web app to multi module style a have "Error instantiating servlet class [servlet.LoginServlet]"

I understand that there are a lot of similar questions, but i can't find where is my problem. So, my question is: When I had all my code in src package of my project and when I started my tomcat app all was good, but when I rewrote my app to multi module architecture I got error. Maybe I should change some paths or so on? In root target web.xml it gives me:
my web.xml: https://i.stack.imgur.com/K8wRE.png
this is error screen: https://i.stack.imgur.com/7j1Ac.png
my target/web.xml: https://i.stack.imgur.com/Oknrq.png
Building with mvn clean package is ok.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>MyWebApp</display-name>
<servlet>
<servlet-name>WelcomeServlet</servlet-name>
<servlet-class>servlet.WelcomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WelcomeServlet</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>CustomerServlet</servlet-name>
<servlet-class>servlet.CustomerServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>servlet.LoginServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>servlet.LogoutServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>RegisterServlet</servlet-name>
<servlet-class>servlet.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegisterServlet</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/logout</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>CustomerServlet</servlet-name>
<url-pattern>/customers</url-pattern>
</servlet-mapping>
</web-app> ```
Thanks for attention.
So, my solution was to change artifact. When I change the location of my tomcat webserver, I should change artifact
The Maven dependencies of IntelliJ are not up to date, see the "m"-button. This might be the cause of the problem.
Click on the "m"-button with "turning arrows" in the top right corner of your editor window.
mvn and IntelliJ manage dependencies independently.

i am not able to link my jsp files in web-inf folder

`<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<welcome-file-list>
<welcome-file>Index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>AddToItinerary</servlet-name>
<servlet-class>servlets.AddToItinerary</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddToItinerary</servlet-name>
<url-pattern>AddToItinerary</url-pattern>
</servlet-mapping>
</web-app>`
// my class files are in WEB-INF-classes-servlets folders .while i am trying to access it displaying 404 error
Imagine for a second that your class is this:
package com.test;
public class MyServlet extends ... {
}
Then your XML will have
<servlet>
<servlet-name>AnyName</servlet-name>
<servlet-class>com.test.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AnyName</servlet-name>
<url-pattern>/any/path/you/want</url-pattern>
</servlet-mapping>
<servlet-mapping>
You are missing the definition which should look something like -
<servlet>
<servlet-name>AddToItinerary</servlet-name>
<servlet-class>{Class Name with package}</servlet-class>
</servlet>
Also, seems like the url-pattern is wrong, shouldn't your url-pattern start with / ?
<servlet>
<servlet-name>ServletName</servlet-name>
<servlet-class>package.ClassName</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletName</servlet-name>
<url-pattern>/Url pattern</url-pattern>
</servlet-mapping>
servlet-name - Give your servlet name you want to give
servlet-class - Give the absolute path of the class like A Class is in package com.test then it will be com.test.A
url-pattern - Give the url pattern to associate a filter or servlet
Hope it helps!!
in the Html page remove the "/" from the action it will start navigating from the project folder along with URL.

remove .py from the jython based web application url

This is the follow up of this question how to remove .py from the url in jython , I couldnt get it solved till now, and hope to get some more suggestions for it.
Here is my web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
<servlet>
<servlet-name>PyServlet</servlet-name>
<servlet-class>org.python.util.PyServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>PyServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>NewJythonServlet</servlet-name>
<servlet-class>NewJythonServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>NewJythonServlet</servlet-name>
<url-pattern>/NewJythonServlet</url-pattern>
</servlet-mapping>
</web-app>
any ideas please?
Try:
<servlet-mapping>
<servlet-name>PyServlet</servlet-name>
<url-pattern>/serv/*</url-pattern>
</servlet-mapping>
I think the url had to start with something that isn't a wildcard

How do I remove ending "/" from URL path in a Servlet?

I want to map my RPC service to http://path.com/RPC2 rather than /RPC2/
Inside my web.xml file, I currently have the url-pattern set to /
<servlet-mapping>
<servlet-name>RPC2</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
I tried to merely remove the url-pattern, but this didn't work. When I remove the url-pattern entry, Tomcat won't deploy it and Jetty works but at ../RPC2/
Here's the full XML file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<description>Automatos RPC Server</description>
<servlet-name>RPC2</servlet-name>
<servlet-class>RPCServlet</servlet-class>
<init-param>
<param-name>streamMessages</param-name>
<param-value>1</param-value>
</init-param>
<init-param>
<!-- Optional! Defaults to text/xml and ISO-8859-1 -->
<param-name>contentType</param-name>
<param-value>text/xml; charset=ISO-8859-1</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>RPC2</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
Deploy your webapp on context root (in Tomcat, just rename the WAR to ROOT.war or set <Context path=""> instead of <Context path="/RPC2">). This way your webapp will be deployed to http://path.com. This way you can map the servlet on an URL pattern of /RPC2 and the servletcontainer won't auto-redirect to the webapp root / anymore.

Resteasy paths JBoss

I am having trouble navigating to my restful service using resteasy 2.1 on localhost and was hoping someone here might be able to help me.
I have built an EAR file with a WAR inside and is seems to compile and deploy to JBoss5 ok.
My service simplified:
#Path("RequestReply")
public class Replier {
#GET
#Path("request")
public String getReply(#QueryParam("id") #DefaultValue("") String id){
if (id.length > 0){
return "ACK";
}
return "NACK";
}
}
My web.xml file is standard:
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<display-name>Test service</display-name>
<listener>
<listener-class>
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class>
</listener>
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
So I try to access my API using
http://localhost:8080/RequestReply/request?id=1234
But I get 404 errors.
Can anyone tell me what I am doing wrong?
Application.xml:
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="java.sun.com/xml/ns/javaee"; xmlns:xsi="w3.org/2001/XMLSchema-instance"; xsi:schemaLocation="java.sun.com/xml/ns/javaee java.sun.com/xml/ns/javaee/application_5.xsd"; version="5">
<display-name>Reseasy</display-name>
<module>
<java>simple.jar</java>
</module>
</application>
Ok so I would suggest trying something like:
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd"
version="5">
<display-name>Reseasy</display-name>
<module>
<web>
<web-uri>Replier.war</web-uri>
<context-root>/api</context-root>
</web>
</module>
</application>
Here the Replier.war refers to the name of the WAR file you create and /api refers to the base of your request URL. So it should look like:
http://localhost:8080/api/RequestReply/request?id=1234
Give that a shot!

Categories