4

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

@WebServlet({ "/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...

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
hcpeter
  • 614
  • 3
  • 11
  • 24

2 Answers2

5

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)

Kris
  • 5,714
  • 2
  • 27
  • 47
  • Sorry maybe I haven't written clearly enough: I have seperate servlets. ListServlet, ModifyServlet, SearchServlet in same package but with different @WebServlet annotation. – hcpeter Sep 30 '11 at 08:45
  • Interesting :). Then maybe try to change your patterns to smth like "/list/*", "/modify/*" etc – Kris Sep 30 '11 at 08:52
  • I tried a lot of things but something wrong... "/list/*" doesn't help. The list servlet is working the other two is not. What a riddle... – hcpeter Sep 30 '11 at 09:01
  • can you post @webservlet annotatios from all your servlets? – Kris Sep 30 '11 at 09:12
  • Yepp @WebServlet({ "/ListServlet", "/list" }) @WebServlet({ "/HelloServlet", "/hello" }) @WebServlet(name="modify", urlPatterns={ "/modify" }, loadOnStartup=1) Only ListServlet works. – hcpeter Sep 30 '11 at 09:15
  • Hmmm I found a very interesting thing: if I delete the listservlet (which is working) and try to access in the url it still working... I restarted glassfish, and redeployed the app... I don't understand, but it seems like a stucked project in the app server. So I can do anything in my code: nothing changed in the browser. I'll try to reinstall glassfish. – hcpeter Sep 30 '11 at 11:59
  • Ok, reinstall and redeploy - it's work. Thank you for your help! Your example is working now. – hcpeter Sep 30 '11 at 12:44
0

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