Servlet and mapping name - java

Hello I am a newbie and I am new at JEE.
I try to connect my servlet class to the web.xml file but I always have this error:
Servlet should have a mapping name
and I don't know why and what is the purpose of adding a mapping name
Here is my web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>SelectLiquorServlet</servlet-name>
<servlet-class>com.sample.SelectLiquorServlet</servlet-class>
</servlet>

First : Servlet mapping specifies the web container of which java servlet should be invoked for a url given by client. It maps url patterns to servlets. When there is a request from a client, servlet container decides to which application it should forward to. Then context path of url is matched for mapping servlets.
See first bullet of step # 2 on Wikipedia page, Java Servlet. (Emphasis added)
The following is a typical user scenario of these methods.
Assume that a user requests to visit a URL.
The browser then generates an HTTP request for this URL.
This request is then sent to the appropriate server.
The HTTP request is received by the web server and forwarded to the servlet container.
The container maps this request to a particular servlet. ⬅
The servlet is dynamically retrieved and loaded into the address space of the container.
The container invokes the init() method of the servlet.
…
To solve your problem you need to add the following lines:
<servlet-mapping>
<servlet-name>SelectLiquorServlet</servlet-name>
<url-pattern>/SelectLiquor</url-pattern>
</servlet-mapping>
Happy Coding

Related

How do I fix error 404 resource not found while I try to upload and display Image [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

#WebServlet annotation web.xml welcome-file

I would like to set the welcome-file of my JSP/JavaBeans project. I have a servlet named 'Controller.java' with the following #WebServlet annotation:
#WebServlet(name="Controller", urlPatterns={"/login", "/show_dbs"})
and I hava a web.xml file with the following content:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<welcome-file-list>
<welcome-file>Controller</welcome-file>
</welcome-file-list>
</web-app>
Almost all things are going well, I can open http://localhost:8080/PROJECT/login and http://localhost:8080/PROJECT/show_dbs and I come to Controller.java. But when I open http://localhost:8080/PROJECT/ I get a 404 error.
I'm using Eclipse with a 'Dynamic Web Project', the Controller.java file is located under /src (default package) and the web.xml file is under /WebContent/WEB-INF.
I hope you have a tip for me.
Thank you for your help. Here comes my solution:
If you want to set your servlet as welcome file you have to do the following:
Define a standard html as welcome-file such as index.html in your web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
Make sure this file (index.html) doesn't exist.
Define your urlPatterns in #WebServlet like this:
#WebServlet(name="Controller", urlPatterns={"/index.html", "/login", "/show_dbs"})
Now every request to http://.../PROJECT/ (root) will be redirected to http://.../PROJECT/index.html and this calls the servlet.
In the welcome file list you must specify the URIs. But you have specified the name of the servlet.
Quote from the Java™ Servlet Specification version 3.0 (emphasis mine):
10.10 Welcome Files
Web Application developers can define an
ordered list of partial URIs called welcome files in the Web
application deployment descriptor. The deployment descriptor syntax
for the list is described in the Web application deployment descriptor
schema.
The purpose of this mechanism is to allow the deployer to
specify an ordered list of partial URIs for the container to use for
appending to URIs when there is a request for a URI that corresponds
to a directory entry in the WAR not mapped to a Web component. This
kind of request is known as a valid partial request.
The use for this facility is made clear by the following common example: A welcome file
of 'index.html' can be defined so that a request to a URL like
host:port/webapp/directory/, where 'directory' is an entry in the WAR
that is not mapped to a servlet or JSP page, is returned to the client
as 'host:port/webapp/directory/index.html'.
If a Web container receives a valid partial request, the Web container must examine the
welcome file list defined in the deployment descriptor. The welcome
file list is an ordered list of partial URLs with no trailing or
leading /. The Web server must append each welcome file in the order
specified in the deployment descriptor to the partial request and
check whether a static resource in the WAR is mapped to that request
URI. If no match is found, the Web server MUST again append each
welcome file in the order specified in the deployment descriptor to
the partial request and check if a servlet is mapped to that request
URI. The Web container must send the request to the first resource in
the WAR that matches. The container may send the request to the
welcome resource with a forward, a redirect, or a container specific
mechanism that is indistinguishable from a direct request.
If no matching welcome file is found in the manner described, the container
may handle the request in a manner it finds suitable. For some configurations this may
mean returning a directory listing or for others returning a 404 response.
P.S.
Also see the examples in the specification in the chapter 10.10
This is what your web.xml should be. Create an index.jsp. Use the index.jsp as your welcome file. The controller class is your sevlet. So define a servlet in your web.xml as follows. This should cause all requests to be directed to the Controller class. And you should still be able to access localhost:8080/PROJECT/, in which case it will be directed to your welcome file.
If you don't want to create index.jsp, you can use your existing jsp file, may be your login.jsp file as your welcome file. In which case, just replace the index.jsp with login.jsp
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Controller</servlet-name>
<servlet-class>com.company.Controller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>*</url-pattern>
</servlet-mapping>
</web-app>
You can specify the url "/Controller" in urlPatterns in #WebServlet annotation. I think it will work.
The default welcome file is index.html, so just add the URL pattern.
#WebServlet(name="Controller", urlPatterns={"/index.html","/login", "/show_dbs"})

making the welcome page of a website to be a servlet

Can I make the welcome-file of the website to be a servlet ? If yes , how ? I was trying something like :
<welcome-file-list>
<welcome-file>FilterForwarded</welcome-file>
</welcome-file-list>
<!-- FilterForwarded is a servlet -->
While deploying I do not see any error but when I try to open the website abc.com I get a message from the browser that it is unable to connect to this website.Why is it so ?
I want when anyone visits the website,I should be able to store the client's public IP. To do this I wrote a Filter which after taking the IP , passed it to the servlet (from there I could update the logs). After storing the IP , client be automatically redirected to index.jsp. Is there any way to achieve this ?
EDIT :
<servlet-mapping>
<servlet-name>FilterForwarded</servlet-name>
<url-pattern>/FilterForwarded</url-pattern>
</servlet-mapping>
This is the mapping defined in web.xml . When I use /FilterForwarded in welcome-file I get this message when I try to deploy : Bad configuration: Welcome files must be relative paths: /FilterForwarded
From the logs :
com.google.apphosting.utils.config.AppEngineConfigException: Welcome files must be relative paths: /FilterForwarded
at com.google.apphosting.utils.config.WebXml.validate(WebXml.java:125)
at com.google.appengine.tools.admin.Application.<init>(Application.java:150)
at com.google.appengine.tools.admin.Application.readApplication(Application.java:225)
at com.google.appengine.tools.admin.AppCfg.<init>(AppCfg.java:145)
at com.google.appengine.tools.admin.AppCfg.<init>(AppCfg.java:69)
at com.google.appengine.tools.admin.AppCfg.main(AppCfg.java:65)
If you map the filter to /* you should be able to intercept all requests and then log the IP from there.
Or is your requirement to only log Client IP for the landing page?
If so, you could change the default servlet for the Servlet container, but bear in mind this will change the default servlet for all requests that do not match mappings in your web.xml.
<servlet-mapping>
<servlet-name>FilterForwarded</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
A more complex, but potentially better solution, is to front your Java web container with a web server and use rewrite rules to proxy to your backend Servlets. This way will mean that you can control the Servlet that is accessed for your landing page without overriding the default servlet for all non-matching requests. This might be overkill for your problem though.

Deploy simple Jax-RS example in JBoss community AS 7

I'm trying to deploy a simple REST example in JBOSS AS 7 but seems that I'm doing something wrong and I'm now clueless. I'm using Eclipse plugin to deploy.
The class I wrote is the following
#Path("/resources")
#Consumes({MediaType.APPLICATION_JSON})
#Produces({MediaType.APPLICATION_JSON})
public class ReceivedImagePersister {
#POST
#Path("/image")
public Response save(String entry) {
return Response.ok().build();
}
}
Then I create a web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>API</display-name>
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>
</web-app>
And a jboss-web.xml
<jboss-web>
<context-root>api</context-root>
</jboss-web>
When I deploy the application and try to send some data to the RESTful servlet the response is always:
(Could not find resource for relative : /image of full path: http://localhost:8080/api/resources/image)
Do I have to make some changes in configuration?
Thanks a lot in advance.
I think your web.xml your url pattern should /* i.e.
<url-pattern>/resources/*</url-pattern>
should be
<url-pattern>/*</url-pattern>
What i am getting at is you need let javax.ws.rs.core.Application parse your url starting from http://localhost/api/ only then will it able to detect that there is a resource named "resources" which has method "image"
Right now it is trying to lookup a resource named "image" which is under the context path /api/resources/
The following url may work for the way its been setup now
http://localhost:8080/api/resources/resources/image
I don't have any experience with JBoss but you should probably use a Servlet 3.0 web.xml instead of a 2.4 if you want the best compatibility with the Java EE 6 deployment model.
The correct url for accessing what you have declared is:
http://localhost:8080/api/resources/resources/image
The first resources is the servlet mapping, and the second is the name of the rest controller.

redirecting requests based on the body tomcat

I have a tomcat 7 application which I can get requests from external sources.
Most of them call my request like this:
http://localhost:8080/MyWeb/exRequest
and I build servlet with URL pattern inside MyWeb app.
However, one external source must send the request like this:
http://localhost:8080/
and in the body:
<xml name="test" />
Since I don't want to declare a general servlel (like tomcat default) since it means that any request will need to go through my servlet, I thought to change index.jsp of ROOT to redirect to my servlet.
Is it the best option?
Is there an option to create a default servlet that will be invoked only if there is a special parameter in the body?
EDITED
Please note that I get the requests to localhost:8080 and not localhost:8080/MyWeb - it's general to tomcat and not to a specific web app
You can't choose a servlet to invoke based on the request body, but you can set a servlet as the "welcome-file" in your web.xml.
<servlet>
<servlet-name>index</servlet-name>
<servlet-class>com.y.MyWelcomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>index</servlet-name>
<url-pattern>/index</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index</welcome-file>
</welcome-file-list>
If you want to preserve the "welcome" function of some existing index.jsp, your servlet could forward requests without the correct XML in the body to an index.jsp file located under the WEB-INF directory.
No, but you can create a Filter and forward/redirect to a specific servlet whenever the request meets certain conditions.
If using servlet 3.0 map it with #WebFilter, otherwise use web.xml and <filter> + <filter-mapping>. You should map it be executed before the default servlet.

Categories