0

I want to understand, If request has to be processed through multiple functions-

  1. is it good to forward through multiple Servlets OR
  2. implement as object methods

Example

      req.getRequestDispatcher("LoadSession").forward(req, resp); 
      req.getRequestDispatcher("AuthoriseRoles").forward(req, resp); 
      req.getRequestDispatcher("Boot").forward(req, resp); 

OR

     sessionLoader.loadSession(req,res); 
     authoriseService.authoriseRoles(req,res); 
     bootService.boot(req, res);
  • I do not think this question has a definite and one correct answer. I guess you will decide for yourself with time and experience and take your side. – Koray Tugay Jan 03 '17 at 05:54
  • In 99.99% of the cases, the second one, but modified. Generally, the servlet should deal with getting things out of the request, and writing things to the response. The rest should be delegated to class and methods not dealing with requests and responses, but with typed objects. – JB Nizet Jan 03 '17 at 06:42

1 Answers1

0

I assume you are at the phase of designing an API. According to REST design principles, the url should reflect the resource that is handled or requested and the HTTP method should reflect what action is required to be taken on the resource.

So, instead of /LoadSession and having the session id as query param in the Http request, it should be GET /session/{id} for example GET /session/e841092fa2194340bc40 (I am assuming LoadSession is a request to return an existing session)

You might ask yourself what is the advantage of following this design. It is that there are several libraries and frameworks that are able to parse incoming HTTP requests and take care of the routing for you (for example, Jersey is the reference JAX-RS implementation, JAX-RS being JavaEE's REST standard) . So instead of writing a servlet as you mentioned, you write the class that represents the resource and methods that are fired according to the HTTP method. you tie it all together with annotations:

@Path("/session")
import javax.ws.rs.*;
import javax.ws.rs.core.*;

@Produces({MediaType.APPLICATION_JSON})
public class SessionHandler
{
    @Context
    private HttpServletRequest httpRequest;
    @Context
    private HttpServletResponse httpResponse;

    @GET
    @Path("{id}")
    public Session load(@PathParam("id") String id) {
    ...
Sharon Ben Asher
  • 13,849
  • 5
  • 33
  • 47