how to set absolute path mapping in Jersey - servlets

Let's say I have an web.xml.
web.xml
<web-app>
<servlet>
<servlet-name>org.foo.rest.MyApplication</servlet-name>
</servlet>
...
<servlet-mapping>
<servlet-name>org.foo.rest.MyApplication</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>
...
</web-app>
My resource class, I have defined the path as:
#Path("/")
public class Resource {
#Path("/resources/{anything}")
#GET
public void doAnything(#PathParam("anything") String anything){
}
}
I want to configure it, so that user can hit the url with
http://localhost:8080/myapp/resources/1234
Currently user has to issue
http://localhost:8080/myapp/resources/resources/123
if web.xml and Resource class cannot be changed, any configuration I can do to make it works?
A bit of History
I am tasked to remove Enunciate 1 from a Spring 3 project. The existing web.xml used "EnunciateJerseyServletContainer" as servlet. I replaced it with "com.sun.jersey.spi.spring.container.servlet.SpringServlet", but the path has changed then. So I was asking, without modifying existing code, can i change some cofnigurations to make the original endpoints works?

Related

Set servlet as default home page in web.xml [duplicate]

This question already has answers here:
Change default homepage in root path to servlet with doGet
(2 answers)
Closed 7 years ago.
I've a servlet registered in web.xml as below.
<servlet>
<servlet-name>Manager</servlet-name>
<servlet-class>Manager</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Manager</servlet-name>
<url-pattern>/RequestManager</url-pattern>
</servlet-mapping>
Basically I want to call this servlet as my default home page when I open http://localhost:8080/appname. So, I tried registering it as welcome file in same web.xml as below:
<welcome-file-list>
<welcome-file>Manager</welcome-file>
</welcome-file-list>
But, when I run the project, I get an error saying "requested resource not available". However, if I write in the url with my servlet URL pattern, it works fine.
Specify an empty string as servlet's URL pattern.
<servlet>
<servlet-name>Manager</servlet-name>
<servlet-class>Manager</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Manager</servlet-name>
<url-pattern></url-pattern>
</servlet-mapping>
See also:
Difference between / and /* in servlet mapping url pattern
Unrelated to the concrete problem, the <welcome-file> should represent an URL path, not a servlet name. It'd have worked if you specifed <welcome-file>RequestManager</welcome-file>. But this affects all subfolders. Actually, the <welcome-file> has an entirely different meaning than "home page file" you've had in mind. It represents the default resource which should be served when a folder is been requested.
You can use index.jsp to forward to your servlet.
<jsp:forward page="servlet_context">
and add index.jsp as welcome file in web.xml
inside servlet class you can forward Control Using :
request.getRequestDispatcher("forward page URL").forward(req,res);
or else if you are using JSP then use
<% RequestDispatcher rd = servletContext.getRequestDispatcher("/pathToResource");
rd.forward(request, response); %>
or
<jsp:forward page="relative URL" />

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

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.

Spring MVC 3 - Need help in resolving the url-pattern issue

I am developing an app with spring mvc and here is my web.xml
<servlet>
<servlet-name>admin</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>admin</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
For serving static content, I added the following line in my admin-servlet.xml
<context:component-scan base-package="com.prasanna.blog.Server" />
<mvc:resources mapping="/resources/**" location="/"/>
I have 5 controllers each mapped to particular url for example adminRequests.java is mapped to /admin/*
#Controller
#RequestMapping("/admin/*")
public class AdminRequests
Then I have methods inside the class mapped to particular urls.
The problem is, I am getting 404 error when the url-pattern in web.xml is mapped as / .But when I change the url pattern in web.xml to /app or any other , my index.html is served without any issues.
I am not sure where the problem is. Please advice
When you map Url pattern / to Spring's Dispatcher Servlet, all the request to your application is forwarded to Dispatcher Servlet. Although in this case you defined the static resources using <mvc:resources> tag, but your index.html is not resolved properly because it is not rendered as /resources/index.html but /index.html.
I would suggest you to add one more <servlet-mapping> in your web.xml before spring's <servlet-mapping>. For example:
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/index.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>admin</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Hope that will work for you.
No that did not resolve the issue. But I saw another thread discussing the same issue and one of the answer there worked. I removed <mvc:resources> line from spring xml and added the below lines in the web.xml
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
Now my static content is served without any issues but I don't like this method. It feels like a very dirty hack. Now if I have to serve images, I have to add mapping for each image type. I am not sure whether the spring version is a problem because I use mongodb in the backend with spring data. It took a hell of a time to set up my pom.xml because there were some issues with the spring data version and mongodb driver.

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.

Servlet mapping in jetty server

I am trying to edit a website:
server : Jetty
framework : spring
index.html page is in ./web/
From this, mapping to webpages in ./web/WEB_INF/template using Servlet. We would like to add one more module in this index.html.. requests help on Servlet mapping.
A servlet is mapped in web.xml:
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.myclass.etcetera.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
It is thus accessible through http://localhost:8080/app/MyServlet
If you are using the latest version of the servlet API (3.0), you can map it using the #WebServlet annotation on the servlet itself (and of course, specify there the url-pattern on which the servlet will respond)

Resources