servlet execution error in eclipse - java

HTTP Status 404 - /Project/
type Status report
message /Project/
description The requested resource (/Project/) is not available.
Apache Tomcat/7.0.16
<?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>Project</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
this is the autogenerated web.xml in eclipse

You have not defined a servlet and its mapping in your web.xml so that the server can call its doGet or doPost method when any request comes in for your servlet.
Add this in your web.xml
The comments describe each tag usage. You can remove the part <!-- ..... --> from each tag
<servlet>
<servlet-name>myservlet</servlet-name> <!-- You can give any name -->
<servlet-class>com.abu.MyFirstServlet</servlet-class> Your servlet class's fully qualified name
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name> the name that you mentioned in the above tag
<url-pattern>/index.html</url-pattern> the url for which the request must be sent to the servlet.
</servlet-mapping>
This means that whenever you type http://localhost:8080/YourPorject/index.html then the conatiner will call your servlet's doGet or doPost method. You should have a look at this wiki page for gaining basic knowledge about servlets

you need to put one file in root directory from above list.

Related

How to redirect from one servlet to another? [duplicate]

This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 5 years ago.
The xml file is located in WebContent/WEB-INF/web.xml of my project. I am using Eclipse and running Tomcat (which is not installed via Eclipse. I prefer it to be a separate installation).
<?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" 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>EmployeeManagement</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>name</param-name>
<param-value>Pramod</param-value>
</context-param>
<servlet-mapping>
<servlet-name>Registration</servlet-name>
<url-pattern>/EmployeeManagement/WebContent/Registration</url-pattern>
</servlet-mapping>
</web-app>
It doesnt work when the form page submits to the servlet. I am getting a 404 error everytime. I have been encountering this problem for a while. Somebody please help me.
You are missing <servlet>...</servlet> tag which is important to mapping. So use following :
<?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" 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>EmployeeManagement</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>name</param-name>
<param-value>Pramod</param-value>
</context-param>
<servlet>
<servlet-name>Registration</servlet-name>
<servlet-class>com.yourPackageName.yourServletName</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Registration</servlet-name>
<url-pattern>/EmployeeManagement/WebContent/Registration</url-pattern>
</servlet-mapping>
</web-app>
and you should give action value on your form like following:
<form action="/EmployeeManagement/WebContent/Registration" method="post">
//Some code here
</form>
and also note it down all values are case sensitive on the following code:
<servlet>
<servlet-name>Registration</servlet-name>
<servlet-class>com.yourPackageName.yourServletName</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Registration</servlet-name>
<url-pattern>/EmployeeManagement/WebContent/Registration</url-pattern>
</servlet-mapping>
Your servlet name Registration should be same on both tags <servlet>...</servlet> and <servlet-mapping>...</servlet-mapping> and also package name should be same where your servlet class is located.
you have not mapped servlet name to servlet class, It should be like given below,
In <servlet-class> give the path of your servlet
<servlet>
<servlet-name>Registration</servlet-name>
<servlet-class>com.Registration<servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Registration</servlet-name>
<url-pattern>/EmployeeManagement/WebContent/Registration</url-pattern>
</servlet-mapping>
Check your form action.
Is the path there
/EmployeeManagement/WebContent/Registration
or
YOURAPPCONTEXT/EmployeeManagement/WebContent/Registration
or
YOURAPPNAME/EmployeeManagement/WebContent/Registration
You have specifyed a servlet-mapping and used the name Registration in servlet-name without defining it before.
You need to define the servlet before using it in a servlet mapping
<servlet>
<servlet-name>Registration</servlet-name>
<servlet-class>[fully qualifyied name of your servlet]</servlet-class>
</servlet>
You are missing another part to define the servlet in the web.xml
<servlet>
<servlet-name>Registration</servlet-name>
<servlet-class>
package.path.to.RegistrationServlet
</servlet-class>
</servlet>
You forgot a vital part of the configuration. You should add this to your web.xml before servlet-mapping tag:
<servlet>
<servlet-name>Registration</servlet-name>
<servlet-class>com.name.of.your.servlet.class</servlet-class>
</servlet>

Web.xml error while adding servlet mapping

I have
<?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>DBTest</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Test</servlet-name>
<servlet-class>com.package1.Test</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/Test</url-pattern>
</servlet-mapping>
</web-app>
The servlet Test.java is in
src/com.package1/Test.java
Also
WebContent/Login.jsp has
<form method="POST" action="../Test">
</form>
When I add the servlet mapping in web.xml and run on server, it cannot connect to localhost.
you have to save the complied copy of servlet (.class) in WEB-INF/classes folder....
And also check the CLASSPATH is set or not for servlet and jsp
it seem like this \tomcat\lib\servlet-api.jar and tomcat\lib\jsp-api.jar

index.html ignored or default servlet got priority over index.jsp and index.html? Why?

web.xml
<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" id="WebApp_ID" version="2.5">
<display-name>TestFilter1</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>ser1</servlet-name>
<servlet-class>com.gaurav.test.Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ser1</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
index.html
<body>
HelLoSTHTML
</body>
index.jsp
<body>
HelLoSTJSP
</body>
Hello.java,implementing javax.servlet.Servlet
service method
PrintWriter out=paramServletResponse.getWriter();
out.println("HelloTextStart");
out.println(config);
out.println("HelloTextEnd");
out.close();
deployed on jboss-5.1.0GA
now cases
**requesting
/TestFilter1/
**
showing
"HelloTextStart
org.apache.catalina.core.StandardWrapperFacade#1a878065 HelloTextEnd "
but not showing
"HelLoSTHTML"
**requesting
/TestFilter1/index.html
** then also
showing
"HelloTextStart
org.apache.catalina.core.StandardWrapperFacade#1a878065 HelloTextEnd "
but not showing
"HelLoSTHTML"
**requesting
/TestFilter1/index.jsp
**
showing
"HelLoSTJSP"
So what is the order of processing/prioritizing this request?
(html,jsp.Servlet ser1)
Your application works as configured. You tell to your webapp :
When you see request coming send them to the servlet class com.gaurav.test.Hello
Conf:
<servlet>
<servlet-name>ser1</servlet-name>
<servlet-class>com.gaurav.test.Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ser1</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
So this servlet is responding to your request.
I think want you need is to configure the servlet javax.servlet.Servlet
I hope this will help
we can configure the URL-patterns in 3 ways
1) Absolute/Exact Matching (Ex: <url-pattern>/test1</url-pattern>)
2) Extension Matching (Ex: <url-pattern>*.do</url-pattern>)
3) Directory Matching (Ex: <url-pattern>/abc/*</url-pattern>)
<url-pattern>/</url-pattern> means Every Request go to Hello.java
First it will check Exact matching then Extension there after Directory Matching.
Above code for every request is going to Hello.java.
go through this link you will get clarity which one will be given priority in Tomcat 6: index.html or index.jsp?

Mapping servlet in web.xml [duplicate]

This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 5 years ago.
The xml file is located in WebContent/WEB-INF/web.xml of my project. I am using Eclipse and running Tomcat (which is not installed via Eclipse. I prefer it to be a separate installation).
<?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" 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>EmployeeManagement</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>name</param-name>
<param-value>Pramod</param-value>
</context-param>
<servlet-mapping>
<servlet-name>Registration</servlet-name>
<url-pattern>/EmployeeManagement/WebContent/Registration</url-pattern>
</servlet-mapping>
</web-app>
It doesnt work when the form page submits to the servlet. I am getting a 404 error everytime. I have been encountering this problem for a while. Somebody please help me.
You are missing <servlet>...</servlet> tag which is important to mapping. So use following :
<?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" 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>EmployeeManagement</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>name</param-name>
<param-value>Pramod</param-value>
</context-param>
<servlet>
<servlet-name>Registration</servlet-name>
<servlet-class>com.yourPackageName.yourServletName</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Registration</servlet-name>
<url-pattern>/EmployeeManagement/WebContent/Registration</url-pattern>
</servlet-mapping>
</web-app>
and you should give action value on your form like following:
<form action="/EmployeeManagement/WebContent/Registration" method="post">
//Some code here
</form>
and also note it down all values are case sensitive on the following code:
<servlet>
<servlet-name>Registration</servlet-name>
<servlet-class>com.yourPackageName.yourServletName</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Registration</servlet-name>
<url-pattern>/EmployeeManagement/WebContent/Registration</url-pattern>
</servlet-mapping>
Your servlet name Registration should be same on both tags <servlet>...</servlet> and <servlet-mapping>...</servlet-mapping> and also package name should be same where your servlet class is located.
you have not mapped servlet name to servlet class, It should be like given below,
In <servlet-class> give the path of your servlet
<servlet>
<servlet-name>Registration</servlet-name>
<servlet-class>com.Registration<servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Registration</servlet-name>
<url-pattern>/EmployeeManagement/WebContent/Registration</url-pattern>
</servlet-mapping>
Check your form action.
Is the path there
/EmployeeManagement/WebContent/Registration
or
YOURAPPCONTEXT/EmployeeManagement/WebContent/Registration
or
YOURAPPNAME/EmployeeManagement/WebContent/Registration
You have specifyed a servlet-mapping and used the name Registration in servlet-name without defining it before.
You need to define the servlet before using it in a servlet mapping
<servlet>
<servlet-name>Registration</servlet-name>
<servlet-class>[fully qualifyied name of your servlet]</servlet-class>
</servlet>
You are missing another part to define the servlet in the web.xml
<servlet>
<servlet-name>Registration</servlet-name>
<servlet-class>
package.path.to.RegistrationServlet
</servlet-class>
</servlet>
You forgot a vital part of the configuration. You should add this to your web.xml before servlet-mapping tag:
<servlet>
<servlet-name>Registration</servlet-name>
<servlet-class>com.name.of.your.servlet.class</servlet-class>
</servlet>

java web app- unable to see jsp file after adding content to web.xml for servlet context listener

I have added the following content to my web.xml for a servlet context listener---
<listener>
<listener-class>
com.anyaservices.log4j.ApplicationServletContextListener
</listener-class>
</listener>
I have added this immediately after the end of "welcome-file-list"
The last line of the welcome-file-list node is given below--
</welcome-file-list>
Now, I have made some changes to a servlet so that it can use the context listener... in addition to the servlet there is a "Hello.jsp" file that just displays a welcome message.
I have not made any changes to the JSP file. However after my code changes for the servlet context listener, when I try to go to Hello.jsp I get an error in Tomcat= The requested resource is not available.
What have I done wrong here? Why is the JSP file not being shown now?
EDIT-- I have posted the entire content of my web.xml below--
<?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" id="WebApp_ID" version="2.5">
<display-name>Test Web Application</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>Hello.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>
com.test.log4j.ApplicationServletContextListener
</listener-class>
</listener>
<servlet>
<description>Used to run a single crawl job</description>
<display-name>runsinglecrawljob</display-name>
<servlet-name>runsinglecrawljob</servlet-name>
<servlet-class>com.test.runsinglecrawljob</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>runsinglecrawljob</servlet-name>
<url-pattern>/runsinglecrawljob</url-pattern>
</servlet-mapping>
<servlet>
<description>Home page</description>
<display-name>home</display-name>
<servlet-name>home</servlet-name>
<servlet-class>home</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>home</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
<servlet>
<description>Run a single crawl job after asking users for parameters of that job</description>
<display-name>runsinglejob</display-name>
<servlet-name>runsinglejob</servlet-name>
<servlet-class>com.test.runsinglecrawljob</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>runsinglejob</servlet-name>
<url-pattern>/runsinglejob</url-pattern>
</servlet-mapping>
</web-app>
The URL I am trying to access is http ://localhost:8080/test_web_app/Hello.jsp
Tomcat handles JSP files through org.apache.jasper.servlet.JspServlet. Perhaps somehow your listener class is interfering with the operation of the JspServlet. There is no need to list all of those files in your welcome file list. Any jsp file inside the war should be available (but it should not be placed inside the WEB-INF or META-INF folders.)
I think you should check your log4j configuration. Did you initialize log4j with a log4j.properties file in WEB-INF/classes?

Categories