#WebServlet annotation web.xml welcome-file - java

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"})

Related

Servlet and mapping name

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

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

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.

freemarker cannot handle request for '/' on google app engine

I'm trying to get freemarker working under Google App Engine. I've gotten simple ftl template files working when requested directly however I'd like to have index.ftl work if available otherwise index.html -- or vice versa.
If I request /index.html, it renders fine as HTML.
If I request /index.ftl, it renders fine as a FreeMarker template. Variables are expanded.
If, however, I request '/' it gives the following 404 message:
Problem accessing /index.html/index.ftl.
I have the following in my web.xml file:
<?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_2_5.xsd"
version="2.5">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.ftl</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>freemarker</servlet-name>
<servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class>
<init-param>
<param-name>TemplatePath</param-name>
<param-value>file://ftl</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>freemarker</servlet-name>
<url-pattern>*.ftl</url-pattern>
</servlet-mapping>
</web-app>
I have the following in the war/ directory of my Eclipse GAE application:
/index.html
/ftl/index.ftl
/WEB-INF/...
If I switch the order of the index.ftl and index.html entries, a request for / gives the following 404 message:
Problem accessing /index.ftl/index.ftl.
Thanks for any help.
One additional bit of information is that if I have one <welcome-file> entry of index.html, it works fine. When I add the index.ftl, in any order, is when I get the errors.
Thanks for any help.
I think the problem here is pretty similar the problem of using a struts action as a welcome page.
Quoting Damien B's answer from that question
There isn't ( a better way other than using a jsp redirect). Servlet specifications
(Java Servlet Specification 2.4,
"SRV.9.10 Welcome Files" for instance)
state:
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.
Since it is mapped to directory entry and not a mapped web component, the "/" isn't forwarding to the freemarker servlet when index.ftl is the welcome file.
I suggest trying the same approach used to make actions a welcome page. Which is have a jsp fwd to your index.ftl.
I'm still looking for the solution to this (although #Andy Pryor's answer may be ultimately right) but I thought that I'd note what I've done to work around this issue.
I ended up moving all of my html files into the FreeMarker view hierarchy so that all HTML and FreeMarker files are processed by the FreeMarker servlet. I don't have to support *.ftl files since I will never be rendering them directly anyway. So the only files I have in my static hierarchy are images and the like.
This seems to be working well although I had to subclass the FreemarkerServlet to block the getSession() methods made on the request since my app does not have sessions enabled. Here's my web.xml file:
<?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_2_5.xsd"
version="2.5">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>freemarker</servlet-name>
<servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class>
<init-param>
<param-name>TemplatePath</param-name>
<param-value>file://views</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>freemarker</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>

GAE logout url giving error 404

My GAE java based application uses only one google user - the admin. For the admin web pages I generate the logout url using
UserServiceFactory.getUserService().createLogoutURL("/")
The generated url is always having a /zero at the end and clicking on it gives 'Error 404 NOT_FOUND'.
I The problem occurs on development server as well as the cloud. On dev server, this generated url is always looking like - http://localhost:8080/myapp/myurl/0 and when actually deployed on cloud it is similar http://myapp.appspot.com/myapp/myurl/0
I wonder why logout url generated is not working, is it something I am doing wrong or missing some configuration ? please help.
Check your web.xml. You have to add following section.
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
You can replace index.jsp with your choice.
Edit
I don't know what is wrong with your app. Here is a test app i have created.
http://rqtest123.appspot.com/
My web.xml look like
<?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_2_5.xsd" version="2.5">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
I think you shoul check your web.xml again.
Finally found it !!!
Earlier, through my spring controller I was passing the created logout url as
model.put("logout-url", UserServiceFactory.getUserService().createLogoutURL("/"));
And my JSP code looked like -
<a class="link" href="${logout-url}">Logout</a>
The variable name logout-url was the problem. Replaced it with logoutUrl and everything worked fine ! The - is not allowed in variable name.

Categories