This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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).