Solved

attributes in servlet

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

744 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

13 Experts available now in Live!

Get 1:1 Help Now