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

Maclean Pinto
  • 1,075
  • 2
  • 17
  • 39
  • possible duplicate of [Calling a Servlet from a Java application](http://stackoverflow.com/questions/4349854/calling-a-servlet-from-a-java-application) – Vignesh Vino Nov 06 '13 at 05:26
  • View this link [Calling a Servlet from a Java application][1] [1]: http://stackoverflow.com/questions/4349854/calling-a-servlet-from-a-java-application – Shamse Alam Nov 06 '13 at 05:27
  • hi, Thanks for the response. Please NOte His class extends HttpServlet mine does not. SO is there a way? – Maclean Pinto Nov 06 '13 at 05:35

1 Answers1

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

}

Nasruddin
  • 1,640
  • 14
  • 20