Glassfish服务器没有加载servlet

时间:2012-07-13 01:11:47

标签: java-ee glassfish

I am getting a 404 when I navigate to http://localhost:8080/myWebApp/generic
but not when I go to http://localhost:8080/myWebApp/test.jsp

我通过导航到myWebApp并输入" jar -cvf myWebApp.war来创建war文件。"

我把它放在glassfish autodeploy文件夹中。 我有什么遗失的吗?

我有一个标准的文件结构

--> "myWebApp/"
||--> "WEB_INF/"
||||--> "classes/"
||||||--> "genericServlet.class"
||||--> "lib/" {empty}
||--> "test.jsp"

这是genericServlet.class

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.GenericServlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;

@WebServlet(name = "GenericServletDemoServlet", 
    urlPatterns = { "/generic" },
    initParams = {
        @WebInitParam(name="admin", value="Harry Taciak"),
        @WebInitParam(name="email", value="admin@example.com")
    }
)
public class GenericServletDemoServlet extends GenericServlet {

    private static final long serialVersionUID = 62500890L;

    @Override
    public void service(ServletRequest request, 
            ServletResponse response)
            throws ServletException, IOException {
        ServletConfig servletConfig = getServletConfig();
        String admin = servletConfig.getInitParameter("admin");
        String email = servletConfig.getInitParameter("email");
        response.setContentType("text/html");
        PrintWriter writer = response.getWriter();
        writer.print("<html><head></head><body>" + 
                "Admin:" + admin + 
                "<br/>Email:" + email +
                "</body></html>");
    }
}  

1 个答案:

答案 0 :(得分:0)

看来,您需要在web.xml中映射servlet。 类似的东西:

<servlet>
<servlet-name>generic</servlet-name>
<servlet-class>servlet.package.GenericServletDemoServlet</servlet-class></servlet>

<servlet-mapping>
<servlet-name>generic</servlet-name>
<url-pattern>/generic</url-pattern></servlet-mapping>