实例化servlet类时出错 - HTTP状态500

时间:2015-05-08 14:30:22

标签: java servlets

尝试创建一个新的servlet ....但是收到错误

HTTP状态500 - 实例化servlet类时出错

  1. 我的servlet类看起来像

    package com.example.tutorial;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @WebServlet(description = "Test servlet this is", urlPatterns = { "/servletexample" })
    public class ServletExample extends HttpServlet {
    private static final long serialVersionUID = 1L;
    
    public ServletExample() {
        super();
    }
    
    protected void service(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("Testing servlet !!!");
    }
    
    }
    
  2. 我的web.xml如下:

    <?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" 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>ServletJSPExample</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>
    
      <servlet>
        <servlet-name>ServletExample</servlet-name>
        <servlet-class>com.example.tutorial.ServletExample</servlet-class>
      </servlet>
    
      <servlet-mapping>
        <servlet-name>ServletExample</servlet-name>
        <url-pattern>/servletexample</url-pattern>
      </servlet-mapping>
    
    </web-app>
    
  3. 不知道发生了什么......会感激帮助.....

1 个答案:

答案 0 :(得分:0)

您可以删除注释或web.xml映射。

如果您需要两种方法,则应类似于以下内容:

@WebServlet(name = "ServletExample", urlPatterns = {"/servletExample"})
public class ServletExample extends HttpServlet {
....
}

并且web.xml中的映射应为:

<servlet>
    <servlet-name>ServletExample</servlet-name>
    <servlet-class>com.example.tutorial.ServletExample</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>ServletExample</servlet-name>
    <url-pattern>/servletExample</url-pattern>
</servlet-mapping>

我认为您错过了&#34; 名称&#34; @WebServlet注释中的属性。把它再试一次。