I am working with eclipse and I run my project with Apache Tomcat.
I have the following files:
1. applicationContext.xml
2. dispatcher-Servlet.xml
3. web.xml
4. pom.xml
I also have the following packages
controller, dao, impl, model.
I would like my homepage to be "MyHomePage". So when I run the project my first url will be : http://localhost:8080/MyHomePage.
In which of the above files/packages I have to define this mapping?
If you meant http://localhost:8080/MyHomePage.html
then you specify the home page in web.xml like this
<web-app>
....
<welcome-file-list>
<welcome-file>MyHomePage.html</welcome-file>
</welcome-file-list>
</web-app>
You will also need to place an html file by the name MyHomePage.html at the root folder of the web site, which, in the case of Tomcat, is WebContent:
WebContent
|-- META-INF
|-- WEB-INF
| `-- web.xml
`-- MyHomePage.html
Related
I'm a beginner in servlets and for a "hello world style" servlet I installed apache tomcat and created the following under it's webapps directory:
servletdir
|- index.xhtml
|- WEB-INF
|- web.xml
|- classes
|- mypackage
|- ServletClass.class
Initialized everything so I could access my servlet on localhost:
web.xml
<?xml version="1.0" encoding="utf-8"?>
<web-app>
<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>mypackage.ServletClass</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/servleturl/*</url-pattern>
</servlet-mapping>
</web-app>
and the index.xhtml contains a form that has action="/servleturl" method="get"
In theory, when I start up tomcat, I should be able to access both the servlet and index.xhtml file now through localhost:8080/servletdir/index.xhtml and submitting the form there. Reaching index is no problem, but when I submit the form, I get a 404 error for the servlet, saying that it "The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.".
My servlet class should not be the problem, it extends HttpServlet and overrides doGet to output a simple html via the ServletResponse parameter's getWriter().println(). Can an error in the servlet class cause a 404? Is my directory structure or web.xml wrong?
I have tried multiple directory structures and solutions, including:
flat dir structure in WEB-INF and class not being in package (no "classes" directory)
no WEB-INF, everything in servlet dir directly
wrap WEB-INF and index.xhtml in a directory named "WebContent"
keep "classes" directory but use no package, put class files directly into classes*
I have followed multiple resources including some tutorials and many questions here which are not directly related to my problem, but contain some info, but as I'm a complete newbie to this technology I can't deduce which resource is good and which isn't, that't why I tried almost everything I found.
The directory structure described at the top is directly on the disk as this structure, not in a war file, but as I can access some parts, I guess this isn't the problem.
*: note that I recompiled the java files with the according "package" at the file's top line every time the directory structure representing the package changed, that's not the problem
change action="/servleturl" method="get" to action="/servletdir/servleturl" method="get" or action="servleturl" method="get".
with action="/servleturl",you are trying to access a root relative path but you are missing the context path servletdir.
If you use the action="servleturl", you don't need to put the context path in the action as it will get it from the current url
Basically I have servlet web-app and I have a directory out the my web-app project say D:\resources, here in my porject web.xml I need to specify that the resources that my web-app would need could be found at D:\resources, I would really appreciate if you tell me how to specify a directory to my project web.xml file.
Thanks in advance
Best way of achieving this is to configre your external resources with defining JNDI in your application server and then referring the same in your web.xml like for e.g.
<resource-description>
<res-ref-name>myAppResources</res-ref-name>
<jndi-name>myResource</jndi-name>
</resource-description>
After creating some sub folders under WEB-INF like js or images,i found problems loading their files i can not obtain any js or image files...what it could be the problem?.
I got this error on the browser :
GET http://localhost:8080/images/blueAqua.gif 404 (Not Found)
WEB-INF is not accessible client-side.
I usually create a static folder next to the WEB-INF one, to store images, JS, CSS, etc.
webapp
|__static
| |__css
| |__js
| |__img
|__WEB-INF
|__web.xml
Then, access the resources using:
http://localhost:8080/static/images/blueAqua.gif
It work fines now i did realise what was the problem and how to fix it, it needed just some configuration in the web.xml as below:
<servlet-name>default</servlet-name>
<url-pattern>/static/*</url-pattern>
</servlet-mapping>
and in the JSP file it was the call like this :
<body background="static/images/example.jpg">
thx for your help anyway.
Try this:
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
My structur is like
WebContent
META-INF
WEB-INF
jsp
index.jsp
web.xml
And web.xml file is as simples as can be
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<display-name>servlets</display-name>
<welcome-file-list>
<welcome-file>WEB-INF/jsp/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
But when I execute it using eclipse in browser i get 404 error, but when I move index.jsp to root directory it works correct.
EDIT:
After changes
jsp
--index.jsp
META-INF
WEB-INF
--web.xml
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<display-name>servlets</display-name>
<welcome-file-list>
<welcome-file>jsp/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
SOLUTION
I found out that my Tomcat server wasn't restarting when I was compiling. Now when tomcat restarts every time I compile servlet it works.
Thank you for your patient
you can't access file inside WEB-INF directly , that is only possible by servlets not by you.
This directory(WEB-INF) contains all resources related to the application that are not in the document root of the application. This is where your web application deployment descriptor is located. Note that the WEB-INF directory is not part of the public document. No files contained in this directory can be served directly to a client.
web.xml must be immediately inside WEB-INF( also not in a sub-directory of WEB-INF)
Also try changing this
<welcome-file>WEB-INF/jsp/index.jsp</welcome-file>
to
<welcome-file>jsp/index.jsp</welcome-file>
UPDATE:
As per your new directory structure, your <welcome-file> tag should be:
<welcome-file>jsp/index.jsp</welcome-file>
Firstly
The web.xml should be inside web-inf
Secondly
the path to index.jsp is incorrect in your web.xml.
As per your directory structure, index.jsp is inside webcontent\jsp\indiex.jsp why are you using path web-inf\jsp\index.jsp
as per your current directory structure it should be .\jsp\indiex.jsp
UPDATE : OP has updated the directory structure.
you should not put jsps in WEB-INF, you should put jsps in webcontent and the web.xml in WEB-INF.
reorganize your directory structure. put the web.xml inside web-inf and jsp folder inside webcontent then inside web.xml put path to welcome jsp as jsp\index.jsp
404 error comes when your URL is not proper.
Check if URL is proper.
Based on your directory structure your URL should be some thing like this : localhost:8080/jsp/index.jsp
Also make sure that web.xml is inside WEB-INF directory.
I have created a war file for my web application.I deployed it on Tomcat,it is working fine. My question is that if I have 3 jsp files like index.jsp,one.jsp,two.jsp, what if I want to run one.jsp first(not index.jsp) when I am running war file. Presently index.jsp is running primarily.I am using netbeans,is there any option in netbeans???
I think you can set that in web.xml in the WEB-INF folder by setting the welcome-file-list
normally it looks like
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
change it to
<welcome-file-list>
<welcome-file>one.jsp</welcome-file>
</welcome-file-list>