【问题标题】:showing 404 error while deploying my Servlets progra部署我的 Servlets 程序时显示 404 错误
【发布时间】:2013-11-28 09:58:47
【问题描述】:

我的 Servlets 程序

package com.srccode.example;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;


public class  Jaan implements Servlet{  
ServletConfig config=null;  

public void init(ServletConfig config){  
this.config=config;  
System.out.println("servlet is initialized");  
}  

public void service(ServletRequest req,ServletResponse res)  
throws IOException,ServletException{  

res.setContentType("text/html");  

PrintWriter out=res.getWriter();  
out.print("<html><body>");  
out.print("<b>hello simple servlet</b>");  
out.print("</body></html>");  

}  
public void destroy(){System.out.println("servlet is destroyed");}  
public ServletConfig getServletConfig(){return config;}  
public String getServletInfo(){return "copyright 2007-1010";}  

} 

更新:

<?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>HelloWorldServlets</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>

在 Tomcat 服务器上部署此程序时显示 404 错误,为什么?我的代码有什么问题吗?浏览器截图中的 404 错误是 --> http://i.stack.imgur.com/CuPuy.png。解决这个问题

【问题讨论】:

  • 首先查看 Servlet Container 的 [Tomcat] 日志,以验证此 servlet 是否已在 Web 上下文中注册,以及加载此 servlet 时是否存在任何问题。最好提供 web.xml 及其文件夹结构。
  • 创建正确的 web.xml 文件并在服务器上部署 war 文件。示例 web.xml 参见此处pubs.vmware.com/vfabric53/index.jsp?topic=/…
  • @chandpriyankara 我的问题已更新为WEB.XMLsee 并告诉我出了什么问题
  • @Raju 看到我的 WEB.XML 一次

标签: jakarta-ee servlets http-status-code-404


【解决方案1】:

您尚未使用 Servlet 配置 web.xml。

<servlet>
    <description></description>
    <display-name>Jaan</display-name>
    <servlet-name>Jaan</servlet-name>
    <servlet-class>com.srccode.example.Jaan</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jaan</servlet-name>
    <url-pattern>/Jaan</url-pattern>
  </servlet-mapping>

【讨论】:

  • 我在 XML 中编辑并保存,然后启动服务器,但仍然出现相同的错误。为什么?
  • 您用来访问 servlet 的 url 是什么?
  • http://localhost:8080/HelloWorldServlets/WEB-INF/classes/com/srccode/example/Jaan.java 这是网址
  • 请理解,WEB-INF 下不提供任何服务。您应该使用网址localhost:8080/yourAppName/Jaan
  • 我的名字是HelloWorldServlets
【解决方案2】:

将此添加到您的 web.xml。

      <servlet>
        <servlet-name>Jaan</servlet-name>
        <servlet-class>com.srccode.example.Jaan</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>Jaan</servlet-name>
        <url-pattern>/Jaan</url-pattern>
      </servlet-mapping>

这会将 URL -> localhost:8080/HelloWorldServlets/Jaan 映射到您的 servlet Jaan

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-02
    • 1970-01-01
    • 2014-06-25
    • 1970-01-01
    • 2015-08-16
    • 1970-01-01
    相关资源
    最近更新 更多