users@jersey.java.net

[Jersey] Re: Forward to Servlet

From: Paul Sandoz <Paul.Sandoz_at_oracle.com>
Date: Thu, 16 Dec 2010 15:23:38 +0100

Hi,

I am not entirely sure what you mean by:

   "do we have better way to catch the exception in same page"

Do you want to use the same template to render both a success and an
error?

You can also throw a WebApplicationException with a response built
using Viewable as an entity. Or use your own special exception that
takes a viewable and use an exception mapper.

You need to be explicit about the template reference, or provide a
context that defines how the relative template reference is resolved
to an absolute template reference. You can pass a class that defines
such a context. That class can be a resource class and will obey the
same rules as that for views returned normally.

Hth,
Paul.

On Dec 16, 2010, at 1:13 AM, cmdotmani wrote:

>
> Hi Paul,
>
> Sorry I am may not be able to share my company code due to NDA :)
> I tried the below and it worked perfectly.
>
> <servlet>
> <servlet-name>MyWebApplication</servlet-name>
> <servlet-class>com.myproj.servlet.UiResourceServlet</servlet-class>
> <init-param>
> <param-name>com.sun.jersey.config.feature.Redirect</param-name>
> <param-value>true</param-value>
> </init-param>
> <init-param>
>
> <param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</
> param-name>
> <param-value>/views/</param-value>
> </init-param>
> <init-param>
>
> <param-name>com.sun.jersey.config.property.WebPageContentRegex</
> param-name>
> <param-value>/(images|css|jsp)/.*</param-value>
> </init-param>
> </servlet>
>
> <servlet-mapping>
> <servlet-name>MyWebApplication</servlet-name>
> <url-pattern>/myproj/*</url-pattern>
> </servlet-mapping>
>
>
> @Component
> @Path ("/myproj")
> public class AdminUiResource extends AbstractResource {
>
> private static final Logger LOG =
> LoggerFactory.getLogger(AdminUiResource.class);
>
> @GET
> @Path("/signup")
> @Produces("text/html")
> public Viewable getSignUp() {
> return new Viewable("/public/signup", "Test");
> }
>
> --------------
>
> Now my question is while doing a POST like below, I want to throw
> exception
> from at request pojo
>
> @POST
> @Path("/signup")
> @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
> public Response postSignUp(MyRequestObject request) throws
> Exception {
> ....
>
> "MyRequestObject" pojo does some validation and responses with error
> message
> which would look some thing like this...
>
> <error><code>0003</code><message>Incorrect request. [Email Address
> invalid].</message></error>
>
> I saw your example from "Devoxx2010_JAX_RS_Samples" in which you
> have some
> thing like this..
>
> @Provider
> public class RuntimeExceptionMapper implements
> ExceptionMapper<RuntimeException> {
> @Override
> public Response toResponse(RuntimeException exception) {
> return Response.status(500).
> entity(new Viewable("/exception", exception)).
> build();
> }
> }
> which will call exception page with some scriplet in JSP page
> <% Exception e = (Exception)request.getAttribute("it");
> ..... %>
>
> do we have better way to catch the exception in same page. I can put
> the
> scriplet in my /signup page, but using scriplet sounds old way, is
> there are
> better approach ?
>
> See its just not about form validation errors, might be some other bad
> request error.
>
> Thanks
> Chandra
> --
> View this message in context: http://jersey.576304.n2.nabble.com/Forward-to-Servlet-tp5351731p5838644.html
> Sent from the Jersey mailing list archive at Nabble.com.