Solved

attributes in servlet

Posted on 2014-03-30
4
375 Views
Last Modified: 2014-05-02
Hi,

I am reading about attributes in servlet.


http://www.javatpoint.com/attribute

I would like to know which attribute to use in which case. Is there is a harm if i use one attribute say application attribute in every case all the time.
Any example on using each of them(request/session/application attribute) and also
using

public Enumeration getInitParameterNames():Returns the names of the context's initialization parameters as an Enumeration of String objects.
public void removeAttribute(String name):Removes the attribute with the given name from the servlet context.

please advise
Any links resources ideas highly appreciated. Thanks in advance
0
Comment
Question by:gudii9
  • 2
  • 2
4 Comments
 
LVL 27

Accepted Solution

by:
dpearson earned 500 total points
ID: 39965389
Attributes are generally stored in either the servlet context (for global data or what it calls "application scope" data) or the session (for data relating to a specific user).

The decision about which to use depends on the type of data being stored.  For example, if you have a userID, that is part of the information for a specific user so it should go on the session (since that stores data for a particular user).  If it was stored in the servlet context (the global data) then you could only handle 1 user of the entire application - because there would be just one "userID" value stored globally.

Does that make sense?

initParameters are completely different.  They are constants defined in a file (the web.xml) file.  That's a useful place to store settings, e.g. the port number to use when accessing a database.

Doug
0
 
LVL 7

Author Comment

by:gudii9
ID: 39973579
If it was stored in the servlet context (the global data) then you could only handle 1 user of the entire application - because there would be just one "userID" value stored globally.

can you please elaborate on this.

initParameters are completely different.  They are constants defined in a file (the web.xml) file.

i thought we get Global Attributes using ServletContext which are defined in initParameters . please advise
0
 
LVL 27

Expert Comment

by:dpearson
ID: 39974082
i thought we get Global Attributes using ServletContext which are defined in initParameters . please advise

Yes that's right - parameters or attributes in the servlet context are global.

can you please elaborate on this.
The servlet context is global - there is only one per application.  So it's the place to store global data e.g. you might have a link to the database in the servlet context because there's only one database used by the application.

The session is one per user - so it's usually the right place to store data about a user e.g. you might store the user id in the session.

Does that make sense?

Doug
0
 
LVL 7

Author Comment

by:gudii9
ID: 40030807
The servlet context is global - there is only one per application.  So it's the place to store global data e.g. you might have a link to the database in the servlet context because there's only one database used by the application.



ServletContext context=getServletContext();  
context.setAttribute("company","IBM");

we set as above right.

Init-param for a servlet done as below right
http://examples.javacodegeeks.com/enterprise-java/servlet/get-all-init-parameters-in-servlet/

10          <servlet>
11              <servlet-name>JCG Snippets Application</servlet-name>
12              <servlet-class>com.javacodegeeks.snippets.enterprise.GetAllInitParametersInServlet</servlet-class>
13              <init-param>
14                  <param-name>myparam1</param-name>
15                  <param-value>myvalue1</param-value>
16              </init-param>
17              <init-param>
18                  <param-name>myparam2</param-name>
19                  <param-value>myvalue2</param-value>
20              </init-param>
21          </servlet>
22       
23          <servlet-mapping>
24              <servlet-name>JCG Snippets Application</servlet-name>
25              <url-pattern>/jcgservlet</url-pattern>
26          </servlet-mapping>
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question