0

directory structure

How to make jersey and @webservlet working together ?

jersey ResourceConfig:

@ApplicationPath("/*")
public class ApplicationConfig extends ResourceConfig {
    public ApplicationConfig() {
        register(Greetings.class);
    }
}

jersey Resource registered in resourceConfig:

@Path("/login")
public class Greetings {
    @GET
    public Response getHelloGreeting(@Context HttpServletRequest httpRequest) {
        System.out.println("In the Greetings resource");
        String url= "http://"+httpRequest.getServerName()+":"+httpRequest.getServerPort()+httpRequest.getContextPath();
        String newURL = url+"/login.jsp";
        System.out.println(newURL);
        return Response.seeOther(URI.create(newURL)).build();
    }
}

web servlet

@WebServlet(name = "LoginServlet", urlPatterns = { "/hello" })
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            ServletContext servletContext = getServletContext();
            System.out.println("inside login servlet");
            request.getRequestDispatcher("/login.jsp").forward(request, response);
            System.out.println("request forwarded");
        }
    //other functions not important so deleted
}

Case 1: on accessing this http://localhost:8088/ResponseFilterweb/login

console logs:

In the Greetings resource  
http://localhost:8088/ResponseFilterweb/login.jsp             (no ui comes)

on accessing this http://localhost:8088/ResponseFilterweb/hello

(nothing happens 404 error)

Case 2: Changing application config resource path:

@ApplicationPath("/auth")
public class ApplicationConfig extends ResourceConfig {
    public ApplicationConfig() {
        register(Greetings.class);
    }
}

on accessing this
http://localhost:8088/ResponseFilterweb/auth/login

In the Greetings resource  
http://localhost:8088/ResponseFilterweb/login.jsp             (Ui comes)

on accessing this
http://localhost:8088/ResponseFilterweb/hello

inside login servlet                                          (Ui comes)
userid is
Encoded string 
request forwarded

doubts:
don't know why login.jsp is blocked in the first case:

why http://localhost:8088/ResponseFilterweb/login not showing any ui .. i think it should come ?
why http://localhost:8088/ResponseFilterweb/hello not showing any ui ?

akitsme
  • 35
  • 13

2 Answers2

1

Using asterisk (*) won't work using @ApplicationPath

If you use /*, then you're making it too greedy and saying it should match everything all the time, and the default servlet will never be invoked

Use @ApplicationPath("/") instead

If you use /, then you're replacing the container's default servlet

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
1

If you were using web.xml, this or this would be your solution, but since you're not, this might be your only option. The problem is that when you use /* as the servlet mapping for Jersey, it hogs up all the requests. So the request to /hello would go to Jersey and not the LoginServlet. What those solutions I linked to do is cause Jersey to forward the request if it can't find it within the Jersey application. The other solution is to just change the servlet mapping to something like /api/* (which is pretty common), then you would just prefix your API requests with /api.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720