2

我试图在war文件中创建一个简单的servlet,并将其部署到所有配置中的jboss 6。我的问题是我得到了一个测试jsp文件来使用url“localhost:8080/workbox/”,但我不能在“localhost:8080/workbox/TestServlet/”中调用servlet。servlet 返回“404 不可用”。任何帮助,将不胜感激:

小服务程序源代码:

package se.marthin.web;

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("/TestServlet")
public class TestServlet extends HttpServlet {

       @Override
       protected void doGet(HttpServletRequest req,
                            HttpServletResponse resp)
                            throws ServletException, IOException {
          resp.setContentType("text/html");
          PrintWriter writer = resp.getWriter();
          writer.println("<h1> asdasdadsadsasddas" +
                         "</h1>");
          writer.close();
       }
}

编译后的 .war 文件中的包结构:

META-INF/
    MANIFEST.MF
WEB-INF/
    web.xml
    classes/
        se/marthin/web/TestServlet.class
    lib/
index.jsp

web.xml 的源代码:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>
4

1 回答 1

0

您必须将 servlet 的配置添加到 web.xml。

喜欢:

  <servlet>
    <servlet-name>TestServlet</servlet-name>
    <servlet-class>se.marthin.web.TestServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>TestServlet</servlet-name>
    <url-pattern>TestServlet</url-pattern>
  </servlet-mapping>
于 2012-10-22T15:01:03.407 回答