Solved

servlt context

Posted on 2014-04-07
7
227 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

738 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