开发者

@WebServlet annotation and error 404 [duplicate]

This question already has answers here: Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available" (19 answers) Closed 6 years ago.

First of all: I use GlassFish 3.1 + Eclipse Java EE indigo. I want to testing cache solutions with javaee so I made a dummy app. I have a big generated database and I list, search, modify, etc some data. To do that I wrote some basic servlet and I call with GET parameters. e.g.: /app/list?page=product&pageSize=100 The ListServlet is annotated with

@We开发者_开发问答bServlet({ "/ListServlet", "/list" })

and it works like a charm, I can use both urls. So I need some additional servlet (for search, modify). I created them and annotated the same way. But when I type the url http://localhost/app/modify or /app/search?id=1 I get error 404. I tried to write a very dummy helloservlet which is print a hello world message but it didn't work: error 404. I restarted the glassfish server and the computer but not helped.

What's the problem? Did I miss something?

EDIT: the servlets are the same package uses the same imports...


Are you sure your url patterns are correct? Try something like this:

@WebServlet( name="ListServlet", displayName="ListServlet", urlPatterns = {"/list","/modify", "/search"}, loadOnStartup=1)

If you want all the patterns go into the same servlet. If not, you would have to have a different servlets for each pattern, and those servlets should be named differently I guess.

Anyway, for this kind of behaviour I would recommend using for example Restlet routing.

EDITED:

I tested it. Here you have my servlets working like a charm:

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(asyncSupported = false, name = "HelloServlet1", urlPatterns = {"/hello1"})
public class TestServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        out.write("<h2>Hello Servlet One </h2>");
        out.close();
    }


}

and the second one:

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(asyncSupported = false, name = "HelloServlet2", urlPatterns = {"/hello2"})
public class TestServlet2 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        out.write("<h2>Hello Servlet Two </h2>");
        out.close();
    }


}

I do call them like: http://localhost:8080/eetest/hello1 and http://localhost:8080/eetest/hello2 and they print 'Hello Servlet One' and 'Hello Servlet Two' respectivelly. (tested on JBoss AS 7 - web profile)


I had this issue and the problem was a forgotten import statement in my servlet. Make sure your servlet is compiling correctly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜