0

我的 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。解决这个问题

4

2 回答 2

0

您尚未使用 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>
于 2013-11-15T06:13:18.793 回答
0

将此添加到您的 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到您的 servletJaan

于 2013-11-17T14:23:26.983 回答