Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

attributes in servlet

Posted on 2014-03-30
4
Medium Priority
?
434 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 28

Accepted Solution

by:
dpearson earned 2000 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 28

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
A solution for Fortify Path Manipulation.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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 …
Suggested Courses

963 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