Spring servlet in web.xml with missing contextConfigLocation param-value - spring-mvc

I have a web.xml file with (among other things) a servlet that defines an init-param to specify the contextConfigLocation, but the param-value is BLANK?
Why is the developer doing this. I can't for the life of me find anything in the documentations for Spring 3.X that tells me what effect this has.
<servlet>
<servlet-name>restservices</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

By default the DispatcherServlet will load a xml file named [servlet-name]-servlet.xml.
This is when no init-param named contextConfigLocation is defined.
However in your case there is an init-param named contextConfigLocation defined, which tells the DispatcherServlet to load nothing but only delegate to the parent context (the one loaded by the ContextLoaderListener).
So in short there is a difference between no init-param defined or an empty init-param.
See also https://jira.springsource.org/browse/SPR-4746.

it just because the developer had nothing to declare in the servlet configuration.
he had maybe defined all what he needs in the root context.

Ah. Generally, the dispatcher servlet would follow the convention of searching for servlet-name - servlet.xml to load the WebAppContext.
It might be (and this is just guess work because i dont know your config) that there already is a file restservices-servlet.xml which is
loaded using the ContextLoaderListener
Or imported in your applicationContext.xml ( or its equivalent)
Or isnt needed, because all the beans for the Controller/ViewResolver
are configured in your applicationContext.xml
Typically, the DispatcherServlet config (WebappContext) should contain the Controller/ViewResolver bean definitions.

Related

Skipping view files/pages without going through Dispatcher Servlet?

I am using Spring MVC framework for writing a web application. The first step is to modify the web.xml to make entry for the dispatcher servlet.
The snippet of the web.xml which has this change:
<servlet>
<servlet-name>MediumScaleProject</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<servlet-mapping>
<servlet-name>MediumScaleProject</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
I have following questions:
1) Does this mean anything with respect to context root has to go through this servlet mapping? (assume context root for this web application is /contextroot)
2) I want to capture user details in userDetails.html; with the above arrangement it is not working, that is if I access like this: /contextroot/userDetails.html; it is giving HTTP 404 error. What is the best strategy for handling these kind of scenarios?
If you want to make a project with the use of Spring MVC framework, you will need the Model, View and Controller.
yes everything will pass through
you better start to implement the MVC-idea

Can I refer a servlet or filter in web.xml from web-fragment.xml?

So I have a web.xml that defines a servlet or a filter that has some servlet-mapping or filter-mapping associated with it, then I want to add some more servlet-mapping/filter-mapping entries in web-fragment.xml, will that work?
Say web.xml:
<servlet>
<servlet-name>A</servlet-name>
<servlet-class>com.A</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>A</servlet-name>
<url-pattern>/Path1</url-pattern>
</servlet-mapping>
then in web-fragment.xml I have
<servlet-mapping>
<servlet-name>A</servlet-name>
<url-pattern>/Path2</url-pattern>
</servlet-mapping>
From my testing it doesn't work but I don't understand why would it be so.
I experienced this same issue. I believe the answer is that he 3.1 servlet spec says
<servlet-mapping> elements with the same <servlet-name> are additive
across web-fragments. <servlet-mapping> specified in the web.xml
overrides values specified in the web-fragments with the same
<servlet-name>
(And something analogous for filter-mappings.)
They are additive only across web-fragments, not across both the web.xml and the web-fragments. If they occur in the web.xml, they override all the ones on the fragments. This seemed to be born out by my testing with Tomcat using the logEffectiveWebXml feature.
So you could make this work by moving these mapping out of the web.xml into a web-fragment.xml. (In my a case, I decided to abandon the web-fragments approach as it became too hard to manage the ordering across multiple files.)
As of Servlet 3.0 Specification web-fragment is introduced for for pluggability of library jars which are packaged under WEB-INF/lib. The content of web.xml and web-fragment.xml are almost the same.
I guess you are not linking your fragments to web.xml, so probably you have to do something like this:
<absolute-ordering>
<name>fragment3</name>
<name>fragment2</name>
<name>fragment1</name>
<others/>
...
</absolute-ordering>

How to use wildcards in Spring MVC servlet mapping URLs?

I've got a controller set up in my web.xml:
<servlet>
<servlet-name>controller</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>controller</servlet-name>
<url-pattern>/console/index</url-pattern>
</servlet-mapping>
And a matching bean defined in controller-servlet.xml:
<bean name="/console/index" class="com.package.OverviewController"/>
Which works correctly - when I get "/appName/console/index" it behaves as I expect. But when I change web.xml to this:
<servlet>
<servlet-name>controller</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>controller</servlet-name>
<url-pattern>/console/*</url-pattern>
</servlet-mapping>
It no longer functions, giving me the following exception:
WARN (org.springframework.web.servlet.PageNotFound) - No mapping for [/appName/console/index] in DispatcherServlet with name 'controller'
So my question is how do I use wildcards in the servlet mappings, so that different URLs all go through the single DispatcherServlet but may go to one of several controller beans?
FYI: I'm stuck on Spring 2.0, as it's an established application used in government.
When you use wildcards in <url-pattern>, controller names by default correspond to wildcard part of the pattern.
So, you can either rename your controller to /index, or set the alwaysUseFullPath property of your HandlerMapping to true.

servlet mapping [duplicate]

This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 7 years ago.
I have created Sample.java servlet, it is in src folder.
and created HTML page in pages directory i.e, pages/First.html
Now I need to provide in servlet mapping as pages/Sample that I am not getting why pages directory name should mention in servlet url mapping.
As it is in root folder.
You should never put any class in the root package.
Once you have put your Sample class in a package (example: com.foo.andy.sample), you need to declare the servet in the web.xml of your web application, and declare one (at least) mapping for this servlet.
You might follow this tutorial to know how to do it.
You need these lines in the web.xml:
<servlet>
<servlet-name>SampleServlet</servlet-name>
<servlet-class>com.foo.andy.sample.Sample</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SampleServlet</servlet-name>
<url-pattern>/pages/Sample</url-pattern>
</servlet-mapping>
And your servlet will be accessible at .../yourWebApp/pages/Sample
We need servlet mapping to ensure that which servlet is going invoked at which type of url request . To do so you need to write web.xml file.
lets assume your class located at com.example package.
<servlet>
<servlet-name>SampleServlet</servlet-name>
<servlet-class>com.example.Sample</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SampleServlet</servlet-name>
<url-pattern>/Sample</url-pattern>
</servlet-mapping>
when you complete this code put the url (/Sample) at your <form action="/Sample"> in HTML page.
make sure you should not put class in root directory.

how to specify param-value in init-param for a servlet using url-pattern?

I want my web.xml to be able to specify param-value for a init-param for a servlet using the url used at runtime. For example:
<servlet>
<servlet-name>startup</servlet-name>
<servlet-class>com.abc.xyz.WebServlet</servlet-class>
<init-param>
<param-name>productCode</param-name>
<param-value>prod1</param-value>
</init-param>
</servlet>
Now, i want the param-value to be set based on the url the user types(prod2 or prod3). Something similar to how we can specify servlet mapping based on url-pattern. Is there a way?
Note: I'm using Weblogic. If there is a way to implement this functionality from Weblogic, that's also fine.
Thanks!

Resources