What is the difference between Attributes and Parameters. that is difference between two methods request.getAttribute() and request.getParameter() and why we have this two methods?
Please help me
Joined: Nov 13, 2002
1. Parameters are strings sent along with the http request. They arrive from the client browser. E.g: If a user prints the following in his browser's address: http://yourHost/yourWebapp/yourServlet?favoriteColor=red Then your servlet gets a parameter (named 'favoriteColor'): // your Servlet.service() method: String c=request.getParameter("favoriteColor"); // "red"
2. Attributes are server-side objects, created by the servelt programmer. Each HttpRequest object has a map-like storage area, where the servlet programmers can store any objects they like (mapped by names or keys). So, somewhere in your servlet code, a programmer can decide to store some value (note this comes from the programmer, and has nothing to do with the browser): // your Servlet.service() method: request.setAttribute("favoriteNumber", new Integet(7)); ...// and it can later be retrieved using: Integer i=(Integer) request.getAttribute("favoriteNumber").
Note: request attributes make most sense where used along with the RequestDispatcher API. Namely, when a single http request is handled by a chain of servlets/JSPs (e.g. one servelt does some calculation, another servlet or JSP builds the page header, another builds the main content...). In such cases, the first servlet can do some calculation and store as a request attribute, so that it's available for the next servlets in the chain. Attributes are *not* very useful if your request is handled by one simple Servlet (and you don't split the work between other servlets / JSPs).