I just started with Spring Web MVC. I'm trying to avoid file extenstions in the url. How can i do this? (I'm using Spring 2.5.x)
Bean:
<bean name="/hello.htm" class="springapp.web.HelloController"/>
I want it to be:
<bean name="/hello" class="springapp.web.HelloController"/>
I cannot get it to work. Any ideas?
Edit:
Url-mapping
<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
I have tried changing the url-pattern with no luck (* and /*).
In 3.0, / seems to work. That is...
<url-pattern>/</url-pattern>
As far as I know you can't do this if you're using JSP's as your view for controllers.
Because when you pass a model to a JSP, Spring MVC internally performs a 'forward' to the URL of the JSP. If you use <url-pattern>/*</url-pattern> then this forward will also be handled by your DispatcherServlet and not by your JSP view.
What you can do is use <url-pattern>/something</url-pattern> and have your JSP's in a different directory
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Then you need to register your urls to be handled by a particular controller. See the following
http://static.springsource.org/spring/docs/2.0.x/reference/mvc.html
In Spring 3.2 at least, the accepted answer above is very nearly but not quite what's needed. The web.xml bit below just worked for me, and I'm adding it to the thread here for reference of whoever googles this next...
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Try first:
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
If that doesn't work then problem is somewhere else. Is your Apache set up to forward those urls to Tomcat? Something like:
JkMount /hello worker1
Have you tried <url-pattern>/*</url-pattern>
in the servlet mapping and <bean name="/hello" .../> ?
Related
I have a confusion regarding the structure of the web.xml for the servlet mapping, I don't have any problem by executing it but I am trying to figure it how why we have such a pattern in the deployment descriptor.
<web-app>
<servlet>
<servlet-name>Servlet1</servlet-name>
<servlet-path>foo.Servlet</servlet-path>
</servlet>
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/enroll</url-pattern>
</servlet-mapping>
</web-app>
Now as far as my understanding whenever a request is comes for url-pattern "/enroll", servlet container is going to match the servlet-name with the url-pattern and will try to find the corresponding servlet-path and will forward the control to foo.Servlet. so basically there would be two passes one for finding servlet-name and another for servlet-path, my question is if container is designed to work in the following way
<web-app>
<servlet>
<servlet-name>foo.Servlet</servlet-path>
<url-pattern>/enroll</url-pattern>
</servlet>
</web-app>
what would be the drawback if we use the following approach. Wouldn't that be more efficient and the response time would be fast.
It allows servlets to have multiple servlet mappings:
<servlet>
<servlet-name>Servlet1</servlet-name>
<servlet-path>foo.Servlet</servlet-path>
</servlet>
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/enroll</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/pay</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/bill</url-pattern>
</servlet-mapping>
It allows filters to be mapped on the particular servlet:
<filter-mapping>
<filter-name>Filter1</filter-name>
<servlet-name>Servlet1</servlet-name>
</filter-mapping>
Your proposal would support neither of them. Note that the web.xml is read and parsed only once during application's startup, not on every HTTP request as you seem to think.
Since Servlet 3.0, there's the #WebServlet annotation which minimizes this boilerplate:
#WebServlet("/enroll")
public class Servlet1 extends HttpServlet {
See also:
How do servlets work? Instantiation, sessions, shared variables and multithreading
Difference between each instance of servlet and each thread of servlet in servlets?
Our Servlets wiki page
I have a Dynamic Web application, and because of the requirements, I am specifying two types of servlet mappings in the web.xml file; Faces Servlet & Jersey(JAX-RS implementation).
My problem is, that if I try to use '/' as the base url-pattern in the Jersey configuration, then the resources of Faces Servlets stop working, i.e., nothing happens if I make REST call to those resources, otherwise everything works fine if I place something like'/rest/' in the Jersey Configuration. My web.xml file looks like this:
<!-- Jersey -->
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.saf.web.v2.beans</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<!-- Faces Servlet -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>100</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
Is there a way to specify the Jersey mapping so there is nothing in the url-pattern but '/*' and Faces Servlet resources also work fine at the same time.
Thanks!
If you define that Jersey should serve all requests (this is what /* means) the Faces Servlet doesn't have a chance any more. So in general: There is no such way.
Maybe you could work around this be mapping Jersey to /rest and writing an own Servlet mapped to /* which dispatches to one of the other servlets. I would not recommend that.
I had the same problem but I fixed it by using
/rest/*
for jersey's servlet
and other part of application can have any other url-pattern, as in your case it is *.xhtml for JSF's servlet.
I am using google app engine to develop my software's backend using java along with Restlet framework. I have index.jsp under my war directory which I want to treat as default page when somebody goes to my website(e.g. example.com). So I have mentioned it under welcome-file-list section in web.xml.
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
Also, I have my Restlet servlet mapped to "/*" in web.xml.
<servlet-mapping>
<servlet-name>RestletServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
But the problem occurs here, because even the call to default page i.e. example.com, also goes to the restlet which obviously doesn't find the mapping in its router. So I decided to instead map restlet servlet to "/restlet/*".
<servlet-mapping>
<servlet-name>RestletServlet</servlet-name>
<url-pattern>/restlet/*</url-pattern>
</servlet-mapping>
But with this I get the HTTP 404 error because somehow even though web.xml successfully routes the call to restlet, but restlet doesn't find the mapping in this case in its router object. I have obviously changed the mapping in the restlet router to match the new pattern "restlet/*".
router.attach("/restlet/doSomething",DoSomething.class);
It would be really great if someone can help me with this. Following is my complete web.xml:
<servlet>
<servlet-name>RestletServlet</servlet-name>
<servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
<init-param>
<param-name>org.restlet.application</param-name>
<param-value>com.mWallet.loyaltyCardCase.LoyaltyCardCaseWebService
</param-value>
</init-param>
</servlet>
<!-- Catch all requests -->
<servlet-mapping>
<servlet-name>RestletServlet</servlet-name>
<url-pattern>/restlet/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
Thanks!
Manas
You don't need to change the mapping in the restlet router to match the new pattern "restlet/*" because the restlet router will now be considering "example.com/restlet/" as the base url.
So, if you change the router to match "/restlet/doSomething", your actual url will be "example.com/restlet/restlet/doSomething", which obviously will not work.
So, change your restlet routing to:
router.attach("/doSomething",DoSomething.class);
I did it in my project and its working.
i think you forgot to write below code in web.xml
<servlet>
<servlet-name>RestletServlet</servlet-name>
<servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
<init-param>
<param-name>org.restlet.application</param-name>
<param-value>com.wa.gwtamazon.server.RestApi</param-value>
</init-param>
<!-- Catch all requests -->
<servlet-mapping>
<servlet-name>RestletServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
and i already answer in this link Restlet API example may will help you.
I have a confusion regarding the structure of the web.xml for the servlet mapping, I don't have any problem by executing it but I am trying to figure it how why we have such a pattern in the deployment descriptor.
<web-app>
<servlet>
<servlet-name>Servlet1</servlet-name>
<servlet-path>foo.Servlet</servlet-path>
</servlet>
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/enroll</url-pattern>
</servlet-mapping>
</web-app>
Now as far as my understanding whenever a request is comes for url-pattern "/enroll", servlet container is going to match the servlet-name with the url-pattern and will try to find the corresponding servlet-path and will forward the control to foo.Servlet. so basically there would be two passes one for finding servlet-name and another for servlet-path, my question is if container is designed to work in the following way
<web-app>
<servlet>
<servlet-name>foo.Servlet</servlet-path>
<url-pattern>/enroll</url-pattern>
</servlet>
</web-app>
what would be the drawback if we use the following approach. Wouldn't that be more efficient and the response time would be fast.
It allows servlets to have multiple servlet mappings:
<servlet>
<servlet-name>Servlet1</servlet-name>
<servlet-path>foo.Servlet</servlet-path>
</servlet>
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/enroll</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/pay</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/bill</url-pattern>
</servlet-mapping>
It allows filters to be mapped on the particular servlet:
<filter-mapping>
<filter-name>Filter1</filter-name>
<servlet-name>Servlet1</servlet-name>
</filter-mapping>
Your proposal would support neither of them. Note that the web.xml is read and parsed only once during application's startup, not on every HTTP request as you seem to think.
Since Servlet 3.0, there's the #WebServlet annotation which minimizes this boilerplate:
#WebServlet("/enroll")
public class Servlet1 extends HttpServlet {
See also:
How do servlets work? Instantiation, sessions, shared variables and multithreading
Difference between each instance of servlet and each thread of servlet in servlets?
Our Servlets wiki page
The following code accesses a servlet's name: servletConfig.getServletName().
Can I access a servlet's URL pattern in a similar way?
An excerpt from web.xml:
<servlet-mapping>
<servlet-name>This is the servlet's name</servlet-name>
<url-pattern>/this-is-its-url-pattern/*</url-pattern>
</servlet-mapping>
In Servlet 3.0 (or Java EE 6) spec exist something:
http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRegistration.html
You can get a ServletRegistration using ServletContext.html#getServletRegistration.
Nothing is available in the Servlet API. Either parse the web.xml yourself, or duplicate it as an <init-param> of the servlet wherein you'd like to access it.
<servlet>
<servlet-name>servlet</servlet-name>
<servlet-class>com.example.Servlet</servlet-class>
<init-param>
<param-name>url-pattern</param-name>
<param-value>/servlet</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>servlet</servlet-name>
<url-pattern>/servlet</url-pattern>
</servlet-mapping>
This way it's available by servletConfig.getInitParameter("url-pattern").