Maclean Pinto
Maclean Pinto

Reputation: 1135

How to call a servlet from a class

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

Upvotes: 0

Views: 254

Answers (1)

Nasruddin
Nasruddin

Reputation: 1690

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();
    }
}

}

Upvotes: 3

Related Questions