Solved

servlt context

Posted on 2014-04-07
7
233 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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.
Suggested Courses

630 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