How can I map the “root” servlet so that other scripts are still running?

I am trying to create a servlet that invokes a JSP page similar to the following:

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { req.getRequestDispatcher("/WEB-INF/main.jsp").forward(req, resp); } 

I need this servlet to respond to the domain root (for example: http://example.com/ ), so I use the following mapping on the Internet .xml:

 <servlet-mapping> <servlet-name>MainServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> 

The problem I am facing is that it matches everything, so when the dispatcher forwards "/WEB-INF/main.jsp", it matches the url pattern, so Servlet starts again. This results in a loop that runs until it dies with java.lang.StackOverflowError .

How can I map the root without interfering with other scripts?

+52
java google-app-engine servlets
Jun 23 '09 at 2:01
source share
8 answers

Use an empty template like

 <servlet-mapping> <servlet-name>MainServlet</servlet-name> <url-pattern></url-pattern> </servlet-mapping> 

The servlet 3.0 specification clarified this:

An empty string ("") is a special URL pattern that exactly matches the root context of the application

Therefore, it should at least work with container 3.0, and I checked that it works on Jetty 8

+40
Feb 29 '12 at 17:20
source share

Using the welcome element of the web.xml file worked for me, on the application engine . Here's mine:

 <web-app> <servlet> <servlet-name>RootServlet</servlet-name> <servlet-class>com.foo.RootServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>RootServlet</servlet-name> <url-pattern>/foo</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>foo</welcome-file> </welcome-file-list> </web-app> 
+29
Dec 14 '10 at 19:57
source share

The original question does not mention that they are trying to map the root servlet in App Engine - this is easy on Tomcat (and other servlet containers, as far as I know), but App Engine is not a regular servlet container.

My usual way to create a web application with servlets is to extend the HttpServlet, add a “page” object with a title, content, errors, messages, etc. and for output forward to the JSP template. It was an absolute nightmare running in App Engine.

  • JSP files cannot be "named" without a "/" at the beginning.
  • JSP files cannot be in a subdirectory
  • Servlets cannot be mapped to the root of your application using the url template //

Here is my web.xml (edited for brevity) that finally worked.

 <web-app> <servlet> <!-- this servlet needs to redirect to a NamedDispatcher called "template" --> <servlet-name>Home</servlet-name> <servlet-class>my.domain.HomeSv</servlet-class> </servlet> <servlet> <!-- jsp file must apparently be in root directory and have "/" at start of path --> <servlet-name>template</servlet-name> <jsp-file>/template.jsp</jsp-file> </servlet> <servlet-mapping> <!-- map your home servlet to somewhere other than "/" --> <servlet-name>Home</servlet-name> <url-pattern>/home</url-pattern> </servlet-mapping> <welcome-file-list> <!-- make your Home servlet the welcome file --> <welcome-file>home</welcome-file> </welcome-file-list> </web-app> 

I was not particularly trained in testing all of this, but it seems to work for me now, and I am very pleased with it.

+7
Sep 10 '09 at 6:25
source share

You can create a welcome file named index.jsp in the root directory using the following code using JSTL or otherwise.

 <c:redirect url="/main"/> 

So, in the web.xml file, you will have the following:

 <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> 

This way, any user requesting root will be redirected to / main. Now your servlet can be mapped to the main one.

 <servlet-mapping> <servlet-name>MainServlet</servlet-name> <url-pattern>/main</url-pattern> </servlet-mapping> 
+4
Jun 23 '09 at 2:10
source share

Try simply removing the '*' from the template, i.e.

 <url-pattern>/</url-pattern> 
+1
Jun 23 '09 at 5:53
source share

You cannot redirect to WEB-INF. The servlet container will never serve requests for documents in this folder.

If you want your application (not just the servlet, but the entire application) to be accessible in the root context ("/" from http://www.domainname.com/ "), you need to configure a context entry for it - not display servlets.

With Tomcat, you add a new <Context> mapping (in one of three different possible locations).

0
Jun 23 '09 at 2:12
source share

Have you tried this below? (Note the missing * , which is a wild card and is the reason that your configuration catches everything.)

 <servlet-mapping> <servlet-name>MainServlet</servlet-name> <url-pattern>/index.jsp</url-pattern> </servlet-mapping> 

(Edited according to comments from / only.)

0
Jun 23 '09 at 4:40
source share

The solution is mentioned in another URL chain to map the servlet in web.xml using URLrewrite -> http://tuckey.org/urlrewrite/

0
Aug 20 '09 at 15:05
source share



All Articles