Error trying to call method from server on GWT - java

I'm trying to call a method from my server side whose signature is
public Integer method()
but when I redid all the steps used on the StockWatcher tutorial to call it, I'm getting a 404 error which says this is the URL
<p>RequestURI=/com.medtronic.empattendance.EmployeeAttendance/empQueries</p>
I'm not sure what the correct URL should be, but this is the incorrect URL.
my web.xml says this on servlets
<servlet>
<servlet-name>empQueryServerImpl</servlet-name>
<servlet-class>com.medtronic.empattendance.server.EmpQueryServerImpl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>empQueryServerImpl</servlet-name>
<url-pattern>/empattendance/empQueries</url-pattern>
</servlet-mapping>
Where am I going wrong?

There is an alternative: Use the #RemoteServiceRelativePath (javadoc) annotation on your RPC class (The interface extending RemoteService, not the Async one).
Assuming your GWT app is /empattendance:
#RemoteServiceRelativePath("empQueries")
public interface EmpQueryServer extends RemoteService {
// your methods
}

I have solved it:
I had <url-pattern>/empattendance/empQueries</url-pattern> which was based off the tutorial, but digging deeper I found out I needed to use the full package name.
<url-pattern>/com.myCompany.empattendance.EmployeeAttendance/empQueries</url-pattern>

Related

ContainerRequestFilter method not being called

I've just started learning how to code REST web services, and I've been stuck with this for several days now. I'm coding an example application with header-based filtering, using Jersey 2 and deployed on Tomee-plus 1.7.2. No matter what I try, the ContainerRequestFilter's filter method is never called.
// TestRequestFilter.java
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.ext.Provider;
#Provider
#PreMatching
public class TestRequestFilter implements ContainerRequestFilter
{
private final static Logger log = Logger.getLogger(DemoRESTRequestFilter.class.getName());
#Override
public void filter(ContainerRequestContext requestCtx) throws IOException
{
System.out.println("FILTER-REQUEST");
}
}
My web.xml file is empty save for the required headers. The behavior right now is: the filter is recognised as #Provider and instantiated as normal, the test web service I have (just a GET returning an empty Response) can be called normally, but the filter method is never called.
Things I've tried and their effects:
Declare the filter in a class extending Application: Error on deployment.
Register the filter in a class extending ResourceConfig: Filter is instantiated twice, but filter method is still not called.
Use the classes from the com.sun.jersey.spi.container package: No effect.
Add an authentication annotation (#RolesAllowed, #PermitAll,...) to the WS method: No effect.
Add disabled=true to cfx-rs.properties in server configuration: Deployed service cannot be found at usual URL.
Add this to web.xml: No effect.
<servlet>
<servlet-name>CongressAppWS</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>org.glassfish.jersey.spi.container.ContainerRequestFilters</param-name>
<param-value>com.s4w.congressapp.auth.DemoRESTRequestFilter;com.s4w.congressapp.auth.DemoRESTResponseFilter</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.s4w.congressapp.auth;com.s4w.congressapp.resources</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Using com.sun.jersey.spi.container prefix instead of org.glassfish.jersey.spi.container in previous code: No effect.
I'm honestly running out of options here. Every time I try something new, either there is no effect or everything stops working. Any help?
I found an alternative to ContainerRequestFilter that actually works! It's javax.servlet.Filter. Extending from this class, all I had to do was annotate it with the following code and the filtering mechanism works like a charm.
#WebFilter(filterName = "AuthenticationFilter", urlPatterns = { "/*" })
The reason for this is that ContainerRequestFilter is part JAX-RS 2.0, but TomEE 1.7.X comes with JAX-RS 1.1. You'll either have to upgrade TomEE to 7.0.0+ or use a different Server (e.g. Glassfish).

Which is best method for calling a required servlet

I have a desktop application from where a client can add a new user, can chat with other user etc... I have 2 options now:
To make a handler servlet which will get post request on
according to request the handler servlet will call required
servlet(e.g loginServlet, chatServlet , addUserServlet etc)
Directly call the required servlet from client e.g make direct
request to chatServlet for chat, login and addUser
Remember my client side is a desktop application. Which method is good for implementing and future enhancements.
Thank you
Well,I think you should go with the 1 st Approach by using Front Controller pattern.
It should consist of only a SINGLE SERVLET which provides a centralized entry point for all requests.This servlet will delegate all request to the required servlet.
You need to do only following thing to apply the front controller pattern in your application:
<servlet>
<servlet-name>////servlet name: FrontController</servlet-name>
<servlet-class>////Fully qualified servlet name e.g: org.chat.controller.FrontController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FrontController<servlet-name>
<url-pattern>/*</url-pattern>
<servlet-mapping>
I couldn't clearly understand your problem from the question. But I would suggest you would start with approach 2 and start implement.
If you face any specific problems then post here.

Message level Jax-WS service

I'm trying to create a WebService stub. I like to react to all of the request in one single place. I have a sample value generator, which handles the type of the request and creates a sample response, so I don't need the code-generation things with a lots of classes. Only a really simple one.
I have found http://jax-ws.java.net/nonav/2.2.1/docs/provider.html WebServiceProvider which is exactly for getting raw SOAP messages, and create a response in a single place.
The main problem is I'm new to this magical EE world :) and I simply can not start WebServiceProvider sample anyway.
I have Spring, SpringSource ToolSuit, Axis installed/configured, all of the other things are working.
Thank you all for your help, and please excuse me if the question is too simple for you. Maybe I just did not find/read something.
M.
Finally I have found the solution (thanks for the help from my workmates).
If you are using JAX-WS, there is a simple solution.
You need a sun-jaxws.xml in your WEB-INF folder containing the following:
<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0">
<endpoint
name="RawWS"
implementation="com.stg.pack.MyServiceProvider"
url-pattern="/HotelServices200631"/>
</endpoints>
And you need a com.stg.pack.MyServiceProvider class which looks like:
package com.stg.pack;
#ServiceMode(value = Service.Mode.MESSAGE)
#WebServiceProvider(portName = "ThePortNameOfWebService",
serviceName = "TheNameOfWebService",
targetNamespace = "http://www.example.com/target/namespace/uri")
public class MyServiceProvider implements Provider<SOAPMessage> {
#Override
public SOAPMessage invoke(SOAPMessage request) {
SOAPMessage result = null;
// create response SOAPMessage
return result;
}
}
And before I forget, you need to define some things in web.xml:
<listener>
<listener-class>
com.sun.xml.ws.transport.http.servlet.WSServletContextListener
</listener-class>
</listener>
<servlet>
<servlet-name>RawWS</servlet-name>
<servlet-class>
com.sun.xml.ws.transport.http.servlet.WSServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RawWS</servlet-name>
<url-pattern>/TheNameOfWebService</url-pattern>
</servlet-mapping>
If you use it like this, all of the request are handled by the invoke method.
you basically must deploy your provider to some sort of Container. developing in J/EE basically mandates that you compile some sort of EAR or WAR or JAR and tell an app server to deploy it (be that app server a JBOSS, glassfish, Weblogic, Websphere, Tomcat, etc).
Have you tried doing this?
it also may be possible to test your provider using the javax.xml.ws.Endpoint class, although I have to admit I've never chosen to per-sue this in favor of deploying to an app server.

Servlet non-lifecycle method call from Browser

I was wondering if it is possible to call a non-life-cycle method of a servlet directly from the Browser.
For example, just as a sample code if I have something like -
public CheckServlet extends HttpServlet {
public void foo(HttpServletRequest req, HttpServletResponse res) {
// do something
}
}
web.xml -
<servlet>
<display-name>CheckServlet</display-name>
<servlet-name>CheckServlet</servlet-name>
<servlet-class>defpkg.CheckServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CheckServlet</servlet-name>
<url-pattern>/CheckServlet</url-pattern>
</servlet-mapping>
The browser URL -
http://localhost:8080/MyApp/CheckServlet
Now if I have to make a call to foo() from the browser, is it possible and if yes, how?
Kind Regards.
No this is not possible! If it was possible it would be a great security hole as a user of your webapp may call any method!
But your servlet may call want it wants in it's lifecycle methods.
The servlet spec (2.5) only supports the following;
doGet
doPost
doPut
doDelete
doHead
doOptions
doTrace
No, but you can pass in some parameters through either a GET or a POST, The first one would be the name of the action you want to execute and the other ones would be the parameters to pass in. On the Servlet side you would map the different actions to different methods and simply pass in the request parameters.
Yes the browser can call in case the web-app is servlet 2.5, The new servlet spec supports custom methods, means the servlet can have doFoo() method, and the browser can call with foo method like any normal GET, POST Http method.

GWT + GAE Servlet URL and Servlet Mapping

http://127.0.0.1:8888/socialnetwork/contactsService
That's the current URL for one of my servlets. My question is, how do I change it?
In my web.xml file, altering
<servlet-mapping>
<servlet-name>contactsServiceServlet</servlet-name>
<url-pattern>/socialnetwork/contactsService</url-pattern>
</servlet-mapping>
to
<servlet-mapping>
<servlet-name>contactsServiceServlet</servlet-name>
<url-pattern>/a/contactsService</url-pattern>
</servlet-mapping>
Makes absolutely NO difference to the URL it requests when I make an RPC-call to the servlet.
Once you have done the above you need to change where you invoke (Which is described in the Annotation below) as in...
// The RemoteServiceRelativePath annotation automatically calls setServiceEntryPoint()
#RemoteServiceRelativePath("email")
public interface MyEmailService extends RemoteService {
void emptyMyInbox(String username, String password);
}
See http://google-web-toolkit.googlecode.com/svn/javadoc/1.6/com/google/gwt/user/client/rpc/RemoteServiceRelativePath.html

Categories