Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Servlet Parameters vs Attributes

My question is - if I'm using a gatekeeper servlet to forward pages to other servlets, is it better to let the second servlet refer to the parameters or create attributes for them to refer to?

Say I have a form of:

<form action=www.ermehgerdpuppies.com/Gatekeeper id = puppyForm> 
    <select name=puppyList>
    <option value=cutePuppyServlet_12>CutePuppy
    <option value=uglyPuppyServlet_14>UglyPuppy
</select></form>

I submit this form which gets to the Gatekeeper servlet:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

   if (request.getParameterMap().containsKey("puppyList"))
   {
        String qString = request.getParameter("puppyList");
        String[] qsArray = qString.split("_");
        request.setAttribute("merPuppy", qsArray[1]);
        RequestDispatcher rd = getServletContext().getRequestDispatcher(qsArray[0]);
        rd.forward(request, response);  
    }
}

Which then goes to the cutePuppyServlet (for this example, it goes to cutePuppy)

Now in my cutePuppyServlet.java, I can either refer to the data this way:

request.getParameter("puppyList");

OR

request.getAttribute("merPuppy");

With parameter, I can check if it exists in order to prevent blowing everything up. My question is, which is better for maintainability? Should I just stick with forwarding the parameter or should I create an attribute?

like image 919
Vernah Avatar asked Jan 13 '23 13:01

Vernah


1 Answers

Advantages of using a parameter for the inner servlet:

  • The nested servlet can stand on its own without the parent servlet, if need be.
  • Developers are more aware of parameters (I don't know why, but I rarely see request attributes used)
  • Less code because the container passes them in from client implicitly.

Advantages of using a request attribute:

  • Includes, forwards, etc. will include them because the request does not change, though its URL may.
  • This is what attributes are actually meant for, messaging passing between components. Therefore, you are adhering to the servlet design.

At the end of the day, it's not going to matter much. I would pick attributes because I care more about doing things the standard way (even if it is a standard that nobody cares about or follows) than doing it quickly.

like image 169
Brandon Avatar answered Jan 17 '23 13:01

Brandon