我的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错误原因何在?我的代码中是否有任何错误。浏览器屏幕截图中的错误是 - &gt; http://i.stack.imgur.com/CuPuy.png。解决这个问题
答案 0 :(得分: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>
答案 1 :(得分: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>
这将映射网址 - &gt; localhost:8080/HelloWorldServlets/Jaan
到您的servlet Jaan