Solved

attributes in servlet

Posted on 2014-03-30
4
354 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 26

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 26

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
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 …

863 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now