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.
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
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
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>
I have setup Javamelody in my Tomcat container to view the statistics of my currently used website.
My website is configured in server.xml as follows and is not deployed as war but the classes and the jsp files are manually copied to "/dun/student/webapps" location as shown in the mapping:
<Context path="/stud" docBase="/dun/student/webapps" crossContext="false" debug="0" reloadable="true">
</Context>
I have copied the jar files javamelody.jar and jrobin-1.5.9.1.jar in the lib directory of my web-app.
Now I have modified the web.xml of my web-app to have the following lines and I have pasted it above my servlet declarations.
<filter>
<filter-name>monitoring</filter-name>
<filter-class>net.bull.javamelody.MonitoringFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>monitoring</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
I have restarted tomcat as well.
Now I'm trying to access my java melody statistics using the url:
http://student.studentgrade.com:8080/stud/monitoring
But I'm getting a 404 status saying requested resource is not available.
Now my web app which has many jsp files which were working fine too cannot be accessed and there too I'm getting a 404 error.
http://student.studentgrade.com:8080/stud/hello.jsp
But if I remove those lines which I added for javamelody in the web.xml file my jsp can be accessed.
What am I doing wrong here.
Please advice how I can fix this.
Do you add
net.bull.javamelody.SessionListener
into your web.xml?
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>