如何从类调用 servlet



我写了一个类来处理 Rest 调用。从方法中,我想调用一个 Servlet。现在我的问题是如何在类中创建HttpServletRequest和HttpServletResponse对象。在 jsp 中,我们不创建任何请求对象。我们可以直接使用它。但是在一个类中,我们要么必须扩展 HttpServlet,要么从调用方法传递请求和响应对象。那么这里的JSP和CLAS有什么区别呢?两者最终都被编译为类权利。

问候

麦克莱恩·莫里斯·平托

如果你要求在 REST 类中创建 HttpServletRequest 和 HttpServletResponse 对象,那么@Context注释。

@Path("/employee/{joiningdate}")公共类员工 {

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

}

最新更新