How to use DispatcherServlet in tomcat with multiple servlets - java

I am working with a tomcat app with multiple servlets.
I want to be able to initialize these and do dependency injection on server start up.
I understand that I will have to declare a org.springframework.web.servlet.DispatcherServlet .
But I am not sure how should my web.xml look like. Currently it looks like following:
<servlet>
<servlet-name>AddAccount</servlet-name>
<servlet-class>com.addressbook.servlets.AddAccount</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AddAccount</servlet-name>
<url-pattern>/AddAccount</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>com.addressbook.servlets.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/Login</url-pattern>
</servlet-mapping>
Currently, any request to add an account is sent directly to /AddAccount and for login the request is sent to /Login.
With DispatcherServler, how should my new web.xml and request structure look like? Do I have to make a new servlet that implements DispatcherServlet and forward each request to this new servlet which then forwards to correct Servlet?

You dont need multiple servlets for several kind of actions in your app. The DispatcherServlet is a front controller that handles all requests and dispatches them to your controllers. As Sotirios suggested, have a look at the Spring MVC manual first. Only the Dispatcher servlet is neccessary.

Related

How to configure Spring 4 WebSocket with sockjs using xml configuration

Hi my current websocket configuration is.
<websocket:message-broker application-destination-prefix="/app">
<websocket:stomp-endpoint path="/chat">
<websocket:sockjs></websocket:sockjs>
</websocket:stomp-endpoint>
<websocket:simple-broker prefix="/topic"/>
</websocket:message-broker>
and my url pattern configuration is
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
if i change to pattern to '/' then it works fine else
the give an error
GET http://localhost:8080/demoapp/chat/info 404 (Not Found)
what is wrong please suggest.
thanks
When using the *.html pattern, you're only mapping those requests to the DispatcherServlet - all other requests (in this case, all SockJS/websocket/etc requests) are ignored and an HTTP 404 error is returned by the servlet container.
So yes, you should be using "/" as a mapping pattern otherwise it won't work.
It can be hard to start writing a websocket application - a lot of new concepts and things to pay attention to. But the programming model is actually quite close to Spring MVC.
Here are a few pointers to help you:
Some "light reading" of the reference documentation (schemas are really helpful)
Try the official getting started guide on websocket
Take a look at well designed example applications such as a portfolio app or a chat room app
How about
add this code
<async-supported>true</async-supported>
on web.xml file
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
I'm use like this code
It work and doesn't show Path error

doGet Method of the Faces Servlet

I'm using Apache Tomcat 7.0 Servlet Container. I've been trying to look at the request handling in JavaServer Faces. I can see the following chunk of config in the web.xml :
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
I've looked at the source of the Faces Servlet, but I haven't found doGet method inside. I thought doGet method is one of the principal method to handle HTTP GET request in the Java Servlets. So who exact handles incoming GET request in JSF? I would like to look at the method, which do that.
FacesServlet doesn't extend from HttpServlet class containing a.o. doGet(). It just implements Servlet interface which offers the base service() method. Look here.
JSF is designed to be compatible with both servlets and portlets. Portlets doesn't use HttpServlet, but PortletServlet which shares the common Servlet interface.

App engine default page to be servlet

I have App engine application, with that servlet:
<servlet>
<servlet-name>Authorization</servlet-name>
<servlet-class>x.y.z.Authorization</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Authorization</servlet-name>
<url-pattern>/pattern</url-pattern>
</servlet-mapping>
everything works.
servlet invokes with this pattern http ://localhost/pattern
now I want to create AutorizationServlet default page. I need to invoke that servlet with this pattern:
http ://localhost
if I write <url-pattern></url-pattern> I have AppEngineConfigException:
Try this :
<url-pattern>/</url-pattern>

Hide .jsp extension in JSP project using servlet mapping

I am writing an application in JSP, and I need to remove the ".jsp" extension from the URL. For example, I need:
http://example.com/search.jsp?q=stackoverflow
To be:
http://example.com/search?q=stackoverflow
I know that this can be done using the ".htaccess" file, but I need some other way. I have tried the following code:
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.</url-pattern>
</servlet-mapping>
However, this did not work. Does anyone have some suggestions for ways to accomplish this? Thanks in advance for any help.
With a servlet mapping you need to specify each JSP individually like follows:
<servlet>
<servlet-name>search</servlet-name>
<jsp-file>/search.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>search</servlet-name>
<url-pattern>/search</url-pattern>
</servlet-mapping>
It's easier if all those JSPs are in a common path. E.g. /app/*.
<servlet>
<servlet-name>app</servlet-name>
<servlet-class>com.example.FriendlyURLServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
with
request.getRequestDispatcher("/WEB-INF" + request.getPathInfo() + ".jsp").forward(request, response);
This assumes the JSPs to be in /WEB-INF folder so that they cannot be requested directly. This will show /WEB-INF/search.jsp on http://example.com/app/search.
Alternatively, you can use Tuckey's URLRewriteFilter. It's much similar to Apache HTTPD's mod_rewrite.

Google App Engine - Blob Store Service with a Dispatcher Servlet

I have a central dispatcher servlet that has a servlet mapping of :
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
When i try to use the blob store service's createUploadUrl("/uploadComplete") it maps to a URL for e.g '/_ah/upload/agp0d2VldG15cGljchsLEhVfX0Jsb2JVcGxvYWRTZXNzaW9uX18YEgw'.
Before the Blob store service can handle the upload and redirect to /uploadComplete; my dispatcher servlet gets called and i am therefore not being able to upload anything.
Is there a servlet/ filter that i can map to /_ah/upload/* in my web.xml ?
How do i avoid the dispatcher servlet from getting called before the Blob store service can do its thing?
Have you tried mapping your uploadComplete servlet?
I would add a mapping like:
<servlet>
<servlet-name>UploadComplete</servlet-name>
<servlet-class>com.Rahul.test.UploadComplete</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UploadComplete</servlet-name>
<url-pattern>/uploadComplete</url-pattern>
</servlet-mapping>
just before your mapping
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
URLs under /_ah/ are reserved by App Engine, and will be directed to the appropriate subsystem regardless of what your config says. What makes you think requests are being sent to your handler instead of the blobstore one?
On further investigation, i have determined that the handler is getting called, however the request is not getting rewritten to "/uploadComplete" on the DevAppServer.
The blobs get uploaded to the blob store, but the forwarded request from the Blobstore service reads /_ah/upload/...* which is an invalid url as far as my end of the web application is concerned.
Thank you for all your answers thus far.

Categories