Servlet mapping issue with SpringMVC - spring

i'm developing a J2EE App using Spring (mvc, security, etc...) and i have a problem with the mappings. I would like to redirect people who type "..../myapp" to a welcome jsp, specifically to "/myapp/welcome.html"
Previously my servlet-mapping had this config:
<servlet-mapping>
<servlet-name>MyApp</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
But i changed it, in order to catch the "/myapp" request. The newone that i wrote is the following:
<servlet-mapping>
<servlet-name>ThreddsAdminPanel</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
It works as expected but when i try to access to a page which needs an css, this error appears:
"No mapping found for HTTP request with URI"
I think that if My url-pattern is /*, the servlet is catching something that doesn't belong to it although i don't know how to do it. Does anybody know a good way to do this?
Thank you

See this: Pretty URL Mapping with Spring 3.0
Basically, change your servlet-mapping from /* to / and then you can worry about performing the redirect.
For the redirect, you should be able to do something like this (assuming use of the mvc namespace in XML config):
<mvc:view-controller path="/myapp" view-name="redirect:/myapp/welcome.html"/>

Related

java.lang.IllegalStateException: Cannot forward after response has been committed while including HTML files using jsp:include

When <jsp:include> is used for including HTML file DispatcherServlet is throwing
java.lang.IllegalStateException: Cannot forward after response has been committed
I have one servlet:
<servlet-mapping>
<servlet-name>web</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
In it, I have enabled Spring MVC annotations and have handler mapping and adapter for JSP files without controllers (converting old webapp to Spring). And I have enabled DefaultServletHttpRequestHandler in this Servlet.
Any idea how to avoid that IllegalStateException when including html files?
So if you let spring handle all html files, it will always fail on jsp:include because spring cannot handle html includes.
Best way around this for me was to leave html files on default servlet.
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
and leave rest on DispatcherServlet.
<servlet>
<servlet-name>web</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>web</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
This is definitely not a best solution, but until I convert all jsps (around 1000 of them) to mvc and something like tiles this is the only way I can see it working.
It is illegal to call forward() after some response is written to the output stream. The response may have been already sent to the client.
This article Causes of Response already committed explains why response is already committed.
I tried the proposed solution which declares the default servlet mapping for *.html url patterns and it worked fine. The only problem was that it introduced some side effects in my case (an hybrid webapp, spring and non-spring managed): html files that should have been managed by Spring's front controller now were managed by Tomcat's default controller.Fortunately, I found a couple of solutions with zero impact on the rest of the webapp.
Use .jsp file extension instead of .html. Spring won't complain if
it finds <jsp:include page="file.jsp" /> instead of <jsp:include page="file.html" />
Include the .html file in a scriptlet <%=file.html %> and avoid using the jsp "include" tag

What's the different between url-pattern

I am learning Spring MVC .
To configure servlet mapping in web.xml.
Who can tell what's difference between them
<servlet-name>login</servlet-name>
<url-pattern>/login/</url-pattern>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
<servlet-name>login</servlet-name>
<url-pattern>/</url-pattern>
<servlet-name>login</servlet-name>
<url-pattern>/*</url-pattern>
<servlet-name>login</servlet-name>
<url-pattern>/*.do</url-pattern>
Maybe more...
It is really necessary for me to know ,so that a new servlet will not be Intercept by other ones.
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
This is exact url pattern, this servlet will be invoked only if the url is like someThing.com/login
<servlet-name>login</servlet-name>
<url-pattern>/*</url-pattern>
This is directory url pattern. So /someString or /someOtherString or /some/someOther will invoke the same login servlet.
<servlet-name>login</servlet-name>
<url-pattern>/*.do</url-pattern>
This is extension url pattern. Anything that is suffixed as .do will map to this. e.g. /someUrl.do or /some/someOther.do will invoke the login servlet.
this looks rather strange,you have this
<servlet-name>login</servlet-name>
<url-pattern>/*</url-pattern>
and thats all you need, the rest of the mapping are supercilious. But calling your spring servlet login is a bit weird. Normally you would call it spring-servlet or similar, everything is then mapped to that servlet, and specific request mappings are handled by different controllers - you can use RequestMappign annoation on controller methods.

How to get a trivial case of Spring MVC view (JSP) resolving to work?

My app uses Spring MVC (latest; 3.2.2) to create a RESTful API returning JSON, and so far I haven't needed a view layer at all. But now, besides the API, I need a simple utility page (plain dynamic HTML) and wanted to use JSP for that.
I want requests to http://localhost:8080/foo/<id> to go through a controller (Java) and end up in a JSP. Should be simple, right? But I'm getting 404; something is not right in resolving the view.
HTTP ERROR 404
Problem accessing /jsp/foo.jsp. Reason:
Not Found
Controller:
#RequestMapping(value = "/foo/{id}")
public String testing(#PathVariable String id, ModelMap model) {
model.addAttribute("id", id);
return "foo";
}
Defining controllers and mapping requests works; this method gets called just fine.
Spring config:
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/jsp/" p:suffix=".jsp"/>
The problem is probably here. I've experimented with slightly different prefixes and putting the JSPs under WEB-INF, as well as stuff like <mvc:view-controller path="/*" /> but no luck yet.
(Do I even need to specify InternalResourceViewResolver, or should default view resolvers take care of this?)
JSP files. Under src/main/webapp/jsp (the project uses Maven conventions) I obviously have the JSPs.
Is there something wrong with this location?
web.xml:
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
I have browsed through Spring MVC documentation, but my problem is probably too trivial and obvious to easily find help there. :-P
Can anyone enlighten me on what I'm doing wrong?
I think what you need to do is changing
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
to
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
/* won't match if there is another folder in the path, like /jsp/foo.jsp. On the other hand / will match everything.

Spring: How to redirect non-dispatcher requests to dispatcher?

I have standard servlet-mapping for Dispatcher Servlet - /app/*. I have controller that handle /notify requests. I need to expose this controller as servlet on http://[SERVER]/notify. How to simple redirect all requests from http://[SERVER]/notify to http://[SERVER]/app/notify (but without other tools, like urlrewrite) ? I know i can write simple servlet instead of, and set servlet-mapping in web.xml, but want to have controller, not servlet ;)
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
Controller:
#Controller
public class PaymentNotificationController {
#RequestMapping("/notify")
void notify() { ... }
}
You can put another Dispatcher Servlet in
<servlet-mapping>
<servlet-name>Notification Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/notify/*</url-pattern>
</servlet-mapping>
and configure it with the same XML file as your main dispatcher servlet.
Don't discount urlrewrite - it only takes nanoseconds to execute, and years have gone into making it as fast as possible.
If you do throw up another Spring MVC servlet, you'll wind up with a second application context, which may not be desirable. DispatcherServlet is a front controller, of which there is supposed to be one - so yes - you can put as many as you want in, but they're almost like little mini-apps inside your WAR.

Can someone explain the Spring web.xml file?

I'm new to Java Enterprise and to Spring but I have a strong grasp of standard Java. I am looking through an existing web application project. The project uses Tomcat/Spring/Hibernate which I understand is fairly common. It also uses DWR for remote method invocations. I'm finding it somewhat difficult to separate responsibilities: what Tomcat is responsible for, what Spring is responsible for, how a request gets from one to the other, and how the major pieces of Spring fit together. I've read a great deal of documentation on Spring, particularly about beans and bean factory and am still in process of reading more. Any advice you guys have would be welcome, but I'll provide some specific questions.
Question 1: Where does the web.xml fit into things (when is it used/called, and where is it called from)?
Code sample 1:
<servlet>
<servlet-name>qrst</servlet-name>
<display-name>qrst Servlet</display-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
What does the above snippet do (or, what does it cause to happen)? At some point in my web app qrst.jsp gets used; is it the DispatcherServlet that calls qrst.jsp using the servlet name? Else what is the significance of the servlet name? What is load on startup?
Code sample 2:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/someLocation/some-servlet.xml
</param-value>
</context-param>
Links or explanation of what the above does? I can see from looking at the XML file that it contains bean definitions and I do understand what beans are and how they are used, but I don't know any other details about this and would like to.
Code sample 3:
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<display-name>DWR</display-name>
<servlet-class>
org.directwebremoting.servlet.DwrServlet
</servlet-class>
<init-param>
<param-name>classes</param-name>
<param-value>
somepackage.someclass
</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
From what I read about beans, I believe those init-param elements are just parameters that get set in the servlet's java class. What's the significance of the servlet name, and what about the load on startup? The web app somehow "knows" when an AJAX (dwr) call is happening versus when the web app is being loaded for the first time (when its loading for the first time it should use qrst.jsp). How does it know this? How does it decide to route the request to DWR instead of to qrst.jsp? Where does it do this?
Thanks.
Servlets are JavaEE's idiom for answering HTTP requests. You program the behavior of your application in a Servlet which will respond to a request.
Tomcat is a Servlet container, which means you deploy your application in Tomcat and it will manage all the communication infrastructure for you: it accepts connections, manages database connections(*) and will call upon your servlets to handle incoming requests.
web.xml is part of any JavaEE application, not Spring. Your code sample 1 declares that your app will use an instance of class org.springframework.web.servlet.DispatcherServlet to handle incoming requests.
Although servlets are the basic foundations for JavaEE development, it is not advised to create your own; instead, with Spring, you create MVC controllers. Then the DispatcherServlet will call upon these controllers to handle the requests. It's just another indirection (but a very powerful one!)
is it the DispatcherServlet that calls qrst.jsp using the servlet name?
Not directly. It's just a coincidence that your servlet and the JSP file have the same name.
What is loaded on startup?
Your code sample 2 instructs the DispatcherServlet to load the beans from file /someLocation/some-servlet.xml. If there are controller beans in this file and according to how you configured the url mapping, beans from this file will answer the incoming requests. See the reference.
I believe those init-param elements are just parameters that get set in the servlet's java class
The init-param elements in web.xml are for the servlet class.
The web app somehow "knows" when an AJAX (dwr) call is happening versus when the web app is being loaded for the first time (when its loading for the first time it should use qrst.jsp). How does it know this?
Missing from the question are either the <servlet-mapping> element (found in web.xml), or the url mappings (found in the spring files). These are responsible for deciding whether an URL should be handled by the dispatcher servlet or the dwr servlet.
For instance, with an servlet mapping like below:
<servlet-mapping>
<servlet-name>qsrt</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>dwr</servlet-name>
<url-pattern>*.dwr</url-pattern>
</servlet-mapping>
Then all URLs ending in .do will be answered by the dispatcher servlet, and those ending with .dwr will be handled by the dwr servlet. Here's where the names of the servlets are important.
JSP files are a different story. The container will simply use them to handle a URL ending in *.jsp. Do not create your onw servlet mapping for URLs ending in *.jsp. This will only cause headaches. This is probably unspecified behavior.
Edit:
However, the URL in the browser's address bar always looks the same: it would always invoke the qrst servlet
Then it is possible that your servlet-mapping is so broad (something like: <url-pattern>/*</url-pattern>) that it will handle anything you throw at the server and never give a chance for the other servlets to handle it.
Last but not least, when working with DWR or any Ajax technology, install the HttpFox extension for Firefox so you can monitor the Ajax calls of your application.

Resources