0

I have written a class to handle a Rest call. From the method i want to call a Servlet. Now my question is how to create a HttpServletRequest and HttpServletResponse object within a class. In jsp we don't create any request object. We can directly use it. But within a class we either have to extend HttpServlet Or pass request and response object from calling method. SO what is the difference between jsp and a clas here? Both are ultimately compiled to a class right.

Regards,

Maclean Maurice Pinto

3

1 Answer 1

3

If you asking for creating HttpServletRequest and HttpServletResponse object within a REST class, then go for @Context annotations.

@Path("/employee/{joiningdate}") public class Employee {

Date joiningdate;
@GET
@Produces("application/xml")    
public Employee(@PathParam("joiningdate") Date joiningdate, @Context Request req, 
        @Context UriInfo ui) {

    this.joiningdate = joiningdate;
    ...
    this.tag = computeEntityTag(ui.getRequestUri());
    if (req.getMethod().equals("GET")) {
        Response.ResponseBuilder rb = req.evaluatePreconditions(tag);
        // Preconditions met
        if (rb != null) {
            return rb.build();
        }
        // Preconditions not met
        rb = Response.ok();
        rb.tag(tag);
        return rb.build();
    }
}

}

Sign up to request clarification or add additional context in comments.

1 Comment

thanks man . Answer was of great help. I Have just started up with Rest. SO struggling a bit...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.