Solved

attributes in servlet

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
custom annotations 9 38
How  Can  I  resolve   this  Error "java.sql.SQLException: Invalid column index" 4 37
tomcat administrtor 12 46
printf performancy 11 30
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

813 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

12 Experts available now in Live!

Get 1:1 Help Now