servlet mapping [duplicate] - servlets

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.

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

how to set absolute path mapping in Jersey

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?

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.

Calling servlet results in HTTP Status 404 "The requested resource is not available" [duplicate]

This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 6 years ago.
I have a servlet register in class p1. I have a JSP jsp1.jsp. I run JSP file and see it, but when I try to apply to the servlet, Tomcat shows an error:
HTTP Status 404
The requested resource (/omgtuk/Register) is not available.
Servlet:
#WebServlet("/register")
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>omgtuk</display-name>
<servlet>
<description></description>
<display-name>register</display-name>
<servlet-name>register</servlet-name>
<servlet-class>p1.register</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>register</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>jsp1.jsp</welcome-file>
</welcome-file-list>
</web-app>
I'm using Eclipse.
The requested resource (/omgtuk/Register) is not available.
This simply means that the servlet isn't listening on an URL pattern of /Register. In other words, you don't have a #WebServlet("/Register").
In your particular case, you made a case mistake in the URL. URLs are case sensitive. You're calling /Register, but your servlet is listening on /register. Fix your form action accordingly.
So, it should not look like this:
<form action="Register">
But it should look like this:
<form action="register">
Or this, which is more robust in case you happen to move around JSPs when you're bored:
<form action="${pageContext.request.contextPath}/register">
Unrelated to the concrete problem, please note that you registered the servlet via both a #WebServlet annotation on the class and a <servlet> entry in web.xml. This is not right. You should use the one or the other. The #WebServlet is the new way of registering servlets since Servlet 3.0 (Java EE 6) and the <servlet> is the old way of registering servlets.
Just get rid of the whole <servlet> and <servlet-mapping> in web.xml. You don't need to specify both. Make sure that you're reading up to date books/tutorials. Servlet 3.0 exist since December 2009 already.
Another detail is that p1 is not a class, it's a package. I'd warmly recommend to invest a bit more time in learning basic Java before diving into Java EE.
See also:
Our servlets wiki page

Resources