0
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 自动部署文件夹中。有什么我想念的吗?

我有一个标准的文件结构

--> "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>");
    }
}  
4

1 回答 1

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>
于 2012-07-13T08:50:25.577 回答