无法连接到服务器(错误-404) [英] unable to connect to server (error-404)
问题描述
我在Eclipse中创建了一个示例Dynamic Web Project,但是当我使用Tomcat 5.5运行我的项目时,我收到此错误:
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状态404 - / greetingservlet /
类型状态报告
message / greetingservlet /
description所请求的资源(/ greetingservlet /)不可用。
HTTP Status 404 - /greetingservlet/
type Status report
message /greetingservlet/
description The requested resource (/greetingservlet/) is not available.
我的HTML / JSP源代码是:
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>
我的servlet源代码是:
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();
}
}
我的 web.xml
是:
<?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>
推荐答案
404 或 Not Found HTTP响应代码表示客户端 能够与服务器通信,但服务器找不到请求的资源。确实......
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...
首先,根据您的 web.xml
,您的Servlet映射到 / greet
,而不是 / greetingservlet
。那么为什么要尝试访问 / greetingservlet
?这是JSP的名称吗?如果是,则它缺少 .jsp
扩展名。如果不是,那么你应该使用 / greet
代替。
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.
其次,你的 web .xml
看起来不正确, servlet-class
与servlet的实际名称不匹配。它应该是:
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>
用真实包裹替换 com.acme
您的servlet,如果有的话(!)。并重新部署整个。
Replace com.acme
with the real package of your servlet, if any(!). And redeploy the whole.
提醒一下,访问servlet的URL构造如下:
As a reminder, the URL to access the servlet is constructed like this:
http://localhost:8080/mywebapp/greet
A B C D
其中:
- A是Tomcat运行的主机名(本地机器)
- B是Tomcat正在侦听的端口(默认为8080)
- C是您的webapp的上下文路径(默认情况下为战争名称)
- D是在web.xml中声明的用于调用servlet的模式
- 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
这篇关于无法连接到服务器(错误-404)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!