Currently, we have "root" (/) mapped to a static index.html page, but we want to upgrade to a jsp to have dynamic content. Trying to figure out how to do this. We have content that is mapped to the default content server (e.g. /css), so we don't want to change too much.
We tried:
Changing the .html to .jsp. This resulted in a blank page.
Changing the .html to .jsp and then moving the file into the WEB-INF directory. This resulted in a 404.
Trying to subclass the DefaultServlet class that content servlet is currently mapped to. This through a 500, with a class assertion error (it checked to see if it was the same class).
Adding another servlet to that url, but it overwrote the current one.
I've searched StackOverflow, but still haven't found an answer that works.
Thanks!
If I understand your question correctly, this is trivial using Spring MVC:
<mvc:default-servlet-handler/>
And in web.xml:
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<async-supported>true</async-supported>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/META-INF/spring/your-applicationContext-web.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Is this what you have tried already?
Just set up a controller method mapped to / that returns a view name, which is your jsp file. And make sure your view resolver is set up correctly. Any of the spring mac tutorial hellos world programs out there will show how.
Related
please see my project structure here.I am using Spring MVC here is my code.
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
when I hit run the project on server I got below error .
HTTP Status 404 – Not Found
as per the concept if we do not specify the welcome list tag then automatically index.html file will be searched.but in my case it is not working however it is working if I change the extension to .jsp.
If any one knows the answer please help me .
Note: I dont want to change the url mapping. I know there are alternatives but I want to know why this is happening and not showing me the result
I have a Servlet that is mapped to the root directory "/":
<servlet>
<servlet-name>Main</servlet-name>
<servlet-class>com.motorola.triage.MainServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Main</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
In this servet there is a couple of small things that are done there, like authentication and retrieve of Google Plus information. After that, I'm doing a forward to a JSP file called "index.jsp"
req.getRequestDispatcher("index.jsp").forward(req,resp);
When I'm accessing "localhost:8080/" the static file "index.jsp" is loaded without passing through the servlet. For architecture reasons I can not change the name of index.jsp. I would like to ask if is there any way to change this behavior of the server and make it look to the servlet before look the index.jsp file.
This is occurring specifically because you used the name index.jsp.
This has been covered elsewhere, such as here and here and here.
I'm at the end of my rope on this one. I'm try to get a super simple webapp up and I can't seem to get tomcat to not 404 static files.
I'm using the gradle tomcat plugin with tomcat version 7.0.39
My html file is at hey-world/src/main/webapp/index.html
My web.xml looks like this:
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>HeyWorldApplication</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/static/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
So I thought this setup would map localhost:8080/hey-world/static/index.html to the file, but it 404's everytime. Is this a problem with some convention in the gradle tomcat plugin?
The URL-patterns used in web.xml/servlet-mapping is often a little simplistic. I think in your case, the /* pattern for Resteasy will work as a catch-all, so that no other mapping will really matter.
For debugging, I suggest you remove the Resteasy-servlet altogether, and see if you can serve static files from a custom URL with your mapping.
If that works, re-enable Resteasy, but on a different URL-pattern (eg. /rest/*).
If that works, well, then everything really works fine, it's just that the URL-mapping for /* blocks anything else from working.
The easiest solution would probably be to server static files as per default (no mapping), and serve rest-stuff from another URL.
Alternatively use two web apps. One with context root /static, one with context root /.
I have two servlets defined in the web.xml file, namely the default2 and myservlet. The default2 servlet is used to map the static files like the javascript and css. The myservlet is used for getting dynamic content.
<servlet>
<servlet-name>default2</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:my-servlet.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
The servlet mapping is defined as follows
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default2</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>
When i try to access any files under /resources, i get a 404. Any ideas why this config is not working or change this config to make it work.
Tomcat's default servlet before 6.0.30 actually serves a static resource identified by HttpServletRequest.getPathInfo(), so that /style.css will be returned when /resources/style.css is requested.
Tomcat's behavior has changed from version 6.0.30 onwards. So the original configuration from the question works in newer versions of Tomcat. See "50026: Add support for mapping the default servlet to URLs other than /. (timw)" in the changelog.
Jetty's default servlet uses a full path.
It should work fine. Are those files in real also located in the /resources folder?
Your web.xml looks correct (except I would change your <load-on-startup> constants).
Make sure that your /resources exists and is a publicly visible folder in your project path and not under /WEB-INF folder.
Try changing your url-pattern for myservlet to /, and optionally adding <mvc:default-servlet-handler /> (see here) to your Spring configuration.
Removed wrong portion of the answer as per #BalusC comment.
Set a break point in your servlet and perform a debug session. Look for the path that your servlet is picking up these files at. Make sure they match the location
I'm trying to write a web application using SpringMVC. Normally I'd just map some made-up file extension to Spring's front controller and live happily, but this time I'm going for REST-like URLs, with no file-name extensions.
Mapping everything under my context path to the front controller (let's call it "app") means I should take care of static files also, something I'd rather not do (why reinvent yet another weel?), so some combination with tomcat's default servlet (let's call it "tomcat") appears to be the way to go.
I got the thing to work doing something like
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>tomcat</servlet-name>
<url-pattern>*.ext</url-pattern>
</servlet-mapping>
and repeating the latter for each one of the file extensions of my static content. I'm just wondering why the following setups, which to me are equivalent to the one above, don't work.
<!-- failed attempt #1 -->
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>tomcat</servlet-name>
<url-pattern>*.ext</url-pattern>
</servlet-mapping>
<!-- failed attempt #2 -->
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>tomcat</servlet-name>
<url-pattern>/some-static-content-folder/*</url-pattern>
</servlet-mapping>
Can anyone shed some light?
I think I may know what is going on.
In your working web.xml you have set your servlet to be the default servlet (/ by itself is the default servlet called if there are no other matches), it will answer any request that doesn't match another mapping.
In Failed 1 your /* mapping does appear to be a valid path mapping. With the /* mapping in web.xml it answers all requests except other path mappings. According to the specification extension mappings are implicit mappings that are overwritten by explicit mappings. That's why the extension mapping failed. Everything was explicitly mapped to app.
In Failed 2, App is responsible for everything, except content that matches the static content mapping. To show what is happening in the quick test I set up. Here is an example. /some-static-content-folder/ contains test.png
Trying to access test.png I tried:
/some-static-content-folder/test.png
and the file was not found. However trying
/some-static-content-folder/some-static-content-folder/test.png
it comes up. So it seems that the Tomcat default servlet (6.0.16 at least) drops the servlet mapping and will try to find the file by using the remaining path. According to this post Servlet for serving static content Jetty gives the behavior you and I were expecting.
Is there some reason you can't do something like map a root directory for your rest calls. Something like app mapped to /rest_root/* than you are responsible for anything that goes on in the rest_root folder, but anywhere else should be handled by Tomcat, unless you make another explicit mapping. I suggest setting your rest servlet to a path mapping, because it declares the intent better. Using / or /* don't seem appropriate, since you have to map out the exceptions. Using SO as an example, my rest mappings would be something like
/users/* for the user servlet
/posts/* for the posts servlet
Mapping order
Explicit (Path mappings)
Implicit (Extension mappings)
Default (/)
Please correct anything that I got wrong.
For reference, the "failed attempt #2" is perfectly correct in version of Tomcat >= to 6.0.29.
It was the result of a Tomcat bug that get fixed in version 6.0.29:
https://issues.apache.org/bugzilla/show_bug.cgi?id=50026
<!-- Correct for Tomcat >= 6.0.29 or other Servlet containers -->
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/some-static-content-folder/*</url-pattern>
</servlet-mapping>
I've never tried to map a servlet like this, but I would argue that /* does technically both start with / and end with /*, even though the same character is used for both matches.