I created a sample Dynamic Web Project in Eclipse, but when I am running my project with Tomcat 5.5 i am getting this error:
HTTP Status 404 - /greetingservlet/
type Status report
message /greetingservlet/ description The requested resource (/greetingservlet/) is not available.
My HTML/JSP source code is:
<!DOCTYPE greeting SYSTEM "hello.dtd">
<html>
<title>Insert title here</title>
<body BGCOLOR="YELLOW">
<center>
<h1>USER NAME ENTRY SREEN</h1>
<form action="./greet">
user name<input type="text" name="t1">
<br><br>
<input type="submit" value="get message">
</form>
</center>
</body>
</html>
My servlet source code is:
import java.io.*;
import javax.servlet.*;
/**
* Servlet implementation class Greetingservlt
*/
public class Greetingservlt extends GenericServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public void service(ServletRequest request,ServletResponse response) throws ServletException,
IOException
{
String name=request.getParameter("t1");
response.setContentType("text");
PrintWriter pw=response.getWriter();
pw.println("<html>");
pw.println("<body bgcolor=wheat>");
pw.println("<h1>HELLO " + name + " WELCOME</h1>");
pw.println("</html>");
pw.close();
}
}
My web.xml
is:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app SYSTEM "Web-app.dtd">
<web-app>
<servlet>
<servlet-name>one</servlet-name>
<servlet-class>Greetingapplication</servlet-class>
</servlet>
<servlet-mapping id="ServletMapping_1283534546609">
<servlet-name>one</servlet-name>
<url-pattern>/greet</url-pattern>
</servlet-mapping>
</web-app>
The servlet is mapped on an url-pattern
of /greet
, not /greetingservlet
as you seem to expect as per the error message. So, change the URL in browser address bar accordingly. Also, the servlet class is declared to be Greetingapplication
, but it has the actual name Greetingservlt
. So, you need to align out the one or the other, otherwise Tomcat is unable to locate/load the servlet. Also, packageless servlet classes used to fail on some specific Tomcat + JVM combinations. To be on the safe side, you'd like to place the servlet class (as every other Java class) in a package
(don't forget to update the web.xml
accordingly).
There are more problems in the code posted as far, but they are as far not relevant to this particular question. Maybe in a new question.
The 404 or Not Found HTTP response code indicates that the client was able to communicate with the server, but the server could not find the resource that was requested. And indeed...
First, according to your web.xml
, your Servlet is mapped on /greet
, and not /greetingservlet
. So why are you trying to access /greetingservlet
? Is this the name of your JSP? If yes, it's missing the .jsp
extension. If no, then you should probably use /greet
instead.
Second, your web.xml
doesn't look correct, the servlet-class
doesn't match the actual name of your servlet. It should be:
<web-app 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"
version="2.4">
<display-name>My First Web Application</display-name>
<servlet>
<servlet-name>one</servlet-name>
<servlet-class>com.acme.Greetingservlt</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>one</servlet-name>
<url-pattern>/greet</url-pattern>
</servlet-mapping>
</web-app>
Replace com.acme
with the real package of your servlet, if any(!). And redeploy the whole.
As a reminder, the URL to access the servlet is constructed like this:
http://localhost:8080/mywebapp/greet
A B C D
Where:
- A is the hostname where Tomcat is running (the local machine here)
- B is the port Tomcat is listening to (8080 is the default)
- C is the context path of your webapp (the name of the war by default)
- D is the pattern declared in the web.xml to invoke the servlet
精彩评论