Solved

servlt context

Posted on 2014-04-07
7
204 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

708 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