May 6, 2012 0
Parameters and Attributes in Servlets And JSPs
Parameters and attributes in servlets and JSPs in java web applications are ways of communication among various entities(servlets and JSPs). However, there are differences between parameters and attributes.
The differences are as follows :
Attributes in Servlets and JSPs:
Attributes in servlets and JSPs can be of various scope.e.g.-request,session,servlet context(Application)
When attributes in servlets and JSPs are obtained in the code the return type is object of an “Object” class.
Attributes in servlets and JSPs can be set or got from the code of the of servles and JSPs.
Parameters in servlets and JSPs:
Parameters in servlets and JSPs are valid till the request response cycle only.
The return type of he code for obtaining the parameters in servlets and JSPs is an object of the type String.
Parameters in servlets and JSPs can only be read from the code.
Parameters in servlets and JSPs can be set only through web.xml or appended in url from html parameters(text boxes etc.)
Code for getting a parameter is:: request.getParameter("paramname"); code for seting an attribute is :: scope.setAttribute("name","value"); code of getting an attribute value is :: scope.getAttribute("name"); Here scope symbolizes different scopes(Request,Session or Application))
Parameters can be set from web.xml file as follows:
<servlet> <servlet-name>myservlet</servlet-name> <servlet-class>MyServlet</servlet-class> <init-param> <param-name>pname</param-name> <param-value>pvalue</param-value> </init-param> </servlet> or <web-app> <context-param> <param-name>pname</param-name> <param-value>pvalue</param-value> </context-param> ... </web-app>
Conceptually Parameters in servlets and JSPs are used to capture user input/s in the application or to read from some configuration file.
Attributes in servlets and JSPs are used to pass data from one servlets or JSP to another servlet or JSP.Since attributes in servlets and JSPs can be associated to various scopes,its usage spans form maintaining session to maintain request attributes or maintain or get a value across the application itself. It is the concept of usage which itself describes the differences between the two.
Number of View :243