Solved

servlt context

Posted on 2014-04-07
7
218 Views
Last Modified: 2014-04-29
Hi,

I am going through below link


http://www.javatpoint.com/ServletContextEvent


i wonder how to run this example without use of oracle database. Are there are any other examples on servelt context.



please advise
Any links resources ideas highly appreciated. Thanks in advance
0
Comment
Question by:gudii9
7 Comments
 
LVL 35

Assisted Solution

by:girionis
girionis earned 125 total points
ID: 39988072
You can run it with any database. You will have to change the database connection url and username/password

// Here you should enter the url of the database you're using
DriverManager.getConnection(  
"jdbc:oracle:thin:@localhost:1521:xe","system","oracle");

Open in new window


The rest should be the same.
0
 
LVL 20

Accepted Solution

by:
chaitu chaitu earned 125 total points
ID: 39988089
in servlet1

set this value in servletcontext

request.getServletContext().setAttribute("name", "john");

in servlet2 you can access this value like this..

      String name=(String )request.getServletContext().getAttribute("name");
            
            System.out.println("name in servlet........."+name);

but actual of servletcontext is you can pass the values between different contexts.
0
 
LVL 20

Assisted Solution

by:Sathish David Kumar N
Sathish David  Kumar N earned 125 total points
ID: 39988329
Setting values in Context
String s="Sathish"
ServletContext ctx=event.getServletContext();  
ctx.setAttribute("myname", s);  

Open in new window


Retrive values in Context values anywhere in the application
ServletContext ctx=getServletContext();  
String sName=ctx.getAttribute("myname"); 

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 7

Author Comment

by:gudii9
ID: 40015886
public class MyListener implements ServletContextListener{  
 
public void contextInitialized(ServletContextEvent arg0) {  


when does contextInitialized() method is called. Is it is called while setting

ctx.setAttribute("myname", s);  / ctx.setAttribute("mycon", con);  
         

Please advise
0
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 40016521
no.its not like that.contextInitialized invokes on below situation.

Yes , for each start of Tomcat , ServletContextListener's contextInitialized method invokes.

public class FirstListener implements ServletContextListener {

@Override
public void contextDestroyed(ServletContextEvent sce) {
}

public void contextInitialized(ServletContextEvent sce) {
    // Do your startup work here

    System.out.println("Started....");
    
    

}
and this is  web.xml..

 <listener>
    <listener-class>org.myapp.FirstListener</listener-class>
</listener>

Open in new window

0
 
LVL 27

Assisted Solution

by:rrz
rrz earned 125 total points
ID: 40016669
>when does contextInitialized() method is called.  
The Servlet container calls contextInitialized() when it is about to deploy the web app.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet doXXX methods 3 49
Unhandled exception type Exception 18 31
null output 3 25
Glassfish admin console not working 1 15
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 …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

810 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