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

x
?
Solved

servlt context

Posted on 2014-04-07
7
Medium Priority
?
252 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 500 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 500 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 500 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
Technology Partners: 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 28

Assisted Solution

by:rrz
rrz earned 500 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

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!

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
A solution for Fortify Path Manipulation.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
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

926 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