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 :5325
Related posts:
- Context Params How to get context parameters in servlets Retrieving context params in servlets
- Servlet Init Params How to get init parametrs in servlets Retreiving init params in servlets
- How To Get The Parameters From One Servlet To Another Using Request
- Tomcat Welcome file list Tomcat directory listing setup welcome files in tomcat
Tags: java, jsp, JSP/Servlets interview questions, Parameter vs. Attribute, parameters vs attributes, servlet, Servlet Attributes and Parameters, Tomcat server, What is the difference between the request attribute and request ...
This entry was posted on Sunday, May 6th, 2012 at 11:12 pm and is filed under Java, software, Tech-Tips, Tips & Tricks. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.