2

I used to just use Tomcat and JSP pages which I can execute query, then assign query result into the array or object then pass that data onto client side via response.

request.setAttribute("errorMessage", "this is error!!");
request.getRequestDispatcher("report.jsp").forward(request, response);

In client jsp code, I could do something like:

${errorMessage}

Then the "this is error!!" message would show up.

I want to do the same thing with REST JAX-RS GlassFish v3.

    @Path("schedule/test")
    @POST
    @Consumes("application/x-www-form-urlencoded")
    @Produces("application/vnd.ms-excel")
    public Object tmpTest(String content) {
        try {

            //just my method to execute query and get result
            Vector out = (Vector)QueryManager.executeQuery;

            //if query result is empty, I want user to redirect to report.jsp page
            if(out.isEmpty()) {
                request.setAttribute("errorMessage", "This is error!!");
                request.getRequestDispatcher("report.jsp").forward(request, response);
                return null;
            }
        ....continue code......
   }

This results in mysterious exception I've never seen.

java.lang.ClassCastException: $Proxy109 cannot be cast to org.apache.catalina.core.ApplicationHttpRequest
            at org.apache.catalina.core.ApplicationHttpRequest.getRequestFacade(ApplicationHttpRequest.java:1001)
            at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:472)
            at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:379)
            at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:336)
            at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:314)

So how can I redirect a user to report.jsp and also pass message like "This is error" ?

The client jsp expects the error msg variable to have a value:

<b>${errorMessage}</b>
Meow
  • 16,125
  • 50
  • 122
  • 176

2 Answers2

4

That's not RESTful. You need to throw a WebApplicationException with a specific status code so that the client understands what exactly went wrong. E.g. when it's actually the server's mistake:

throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);

Or when it was after all client's mistake:

throw new WebApplicationException(Response.Status.BAD_REQUEST);

See also HTTP status code definitions for an overview.


The ClassCastException which you're facing is by the way occurring because the dispatched request is actually not an instance of the servletcontainer-provided implementation (in this particular case, the one of Tomcat or a Tomcat-fork). After all, you shouldn't be doing it this way. You're developing a REST webservice, not a JSP/Servlet website. It are two distinct worlds.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • I understand not to mix the JSP/Servlet and REST implementation, and throwing WebApplicationException but how can I pass some error message String as well?? – Meow Mar 01 '11 at 02:51
  • 1
    Either create a [custom exception](http://stackoverflow.com/questions/583973/jax-rs-jersey-how-to-customize-error-handling) or just document the webservice properly under which conditions a specific response status code can occur. – BalusC Mar 01 '11 at 02:55
  • 1
    Right, but how the client side code like Javascript (or JSP) can access the error message? I got: throw new WebApplicationException(Response.ok("errorTest").build());

    I'm using regular POST method so this cause entire page to refresh to http 400.
    – Meow Mar 01 '11 at 04:47
  • This is a good answer, but it still doesn't answer the question. Why is the ClassCastException occuring? You say something about two different implementations of RequestDispatcher (?), but how do I identify the *correct* vendor implementation of the Dispatcher that I'm using (glassfish) ? – Chris J Sep 20 '12 at 01:45
  • @Chris: it's a proxied request object. Say, it's not the same request instance as you'd get in a servlet `doGet()` method. It's a wrapper around it implementing the same `HttpServletRequest` interface, but it's not the container-specific instance itself. Read up the proxy pattern in wikipedia if you don't understand its uses. – BalusC Sep 20 '12 at 02:16
3

As mentioned before, you should try WebApplicationException.

I believe you this would give you your desired answer:
Try this:

        if(out.isEmpty()) {
             java.net.URI location = new java.net.URI("report.jsp");
             throw new WebApplicationException(Response.seeOther(location).build());
         }


Matin Kh
  • 4,912
  • 6
  • 44
  • 74