Javatpoint Logo

One time connectivity and use in all jsp pages

By: vikasr*** On: Thu Apr 18 01:31:31 IST 2013     Question Reputation27 Answer Reputation11 Quiz Belt Series Points0  38Blank User
how can i use servletcontextevent like in servlet for one time connectivity at the time of deploying wep application and cab access in all other servlet in application by using ServletContext Obect.(As use in Event and listener topic In javatpoint.com)

So Is there any way to use one time connectivity in jsp and can use in all jsp pages????
Up5Down

 
For one time connection you need to use ServletContext Listener

Step 1)Create web.xml
In web.xml give description for Listener

<web-app>
<listener>
<description>ServletContextListener</description>
<listener-class>PMSListener</listener-class>
</listener>
<context-param>
<param-name>driver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</context-param>
<context-param>
<param-name>jdbcURL</param-name>
<param-value>jdbc:mysql://localhost:<port>/<DataBase Name></param-value>
</context-param>
<context-param>
<param-name>userName</param-name>
<param-value>root</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>root</param-value>
</context-param>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>

Step 2) Then write java class
Then access the context parameter from web.xml


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class PMSListener implements ServletContextListener {

Connection con=null;
public void contextInitialized(ServletContextEvent event) {


String driver=event.getServletContext().getInitParameter("driver");
String jdbcURL=event.getServletContext().getInitParameter("jdbcURL");
String userName=event.getServletContext().getInitParameter("userName");
String password=event.getServletContext().getInitParameter("password");
try{
Class.forName(driver);
con=DriverManager.getConnection(jdbcURL, userName,password);
event.getServletContext().setAttribute("conn",con);
//System.out.println("DB Object get's Created");
}
catch(Exception e)
{
e.printStackTrace();
}


}


public void contextDestroyed(ServletContextEvent sce) {
try {
con.close();
} catch (SQLException ex) {
System.out.println("Exception in Litener .....");
}
}
}
Image Created9Down

By: [email protected] On: Thu Apr 18 09:51:03 IST 2013 Question Reputation15 Answer Reputation32 Belt Series Points0 47User Image
Are You Satisfied :2Yes0No
 
you can use properties file to define the name of driver class and Connection url in order to prevent the application to be redeployed on changing these terms.Image Created0Down

By: [email protected] On: Thu Apr 18 10:21:43 IST 2013 Question Reputation0 Answer Reputation147 Belt Series Points0 147User Image
Are You Satisfied :1Yes0No
 
you can make a jsp page in which you have to write connection code and the object of connection can be use in all other jsp pages..Image Created8Down

By: [email protected] On: Thu Apr 18 11:08:43 IST 2013 Question Reputation0 Answer Reputation153 Belt Series Points0 153User Image
Are You Satisfied :0Yes0No
 
can i use object of Connection class direct in other jsp pages or i have to import that page in which connection made.?? [email protected]
thanx all of u guys..
Image Created2Down

By: [email protected] On: Thu Apr 18 12:45:24 IST 2013 Question Reputation27 Answer Reputation11 Belt Series Points0 38User Image
Are You Satisfied :0Yes0No
 
You can make a database file in the IDE.Make beans of all the tables usable in the project or program.Include getters and setters .Then you will have no problem in the connectivity,all you have to do is to include the bean file in all the files.

Hope this would be helpful to you.
Image Created0Down

By: [email protected] On: Thu Apr 18 14:49:16 IST 2013 Question Reputation6 Answer Reputation0 Belt Series Points0 6User Image
Are You Satisfied :2Yes0No
 
If you are going with [email protected] then you have to include that jsp page in all the jsp pages on which you want to do something with data base.
<jsp:include page="connection.jsp"></jsp:include>
Image Created0Down

By: [email protected] On: Thu Apr 18 16:41:44 IST 2013 Question Reputation0 Answer Reputation147 Belt Series Points0 147User Image
Are You Satisfied :1Yes0No
 
I got the anwer..
We can use WebActionListener implemets ServletContextListener
For example..

public class Listener implements ServletContextListener {

@Override
public void contextInitialized(ServletContextEvent sce) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("Jdbc:Odbc:login", "sa", "123456");
ServletContext sc=sce.getServletContext();
sc.setAttribute("Mycon", con);
___________
and after that we can access servletcontext object in servlets and jsp also..so it reduce our time for database connctivity..
because we know that when our web application deploy then then this Listener wil invoke..
nd can access it in jsp pages and servlets...
like
In servlet and jsp page:----------
ServletContext cn=getServletContext();
Connection con=(Connection)cn.getAttribute("Mycon");
PreparedStatement pr=con.prepareStatement("select * from login");
ResultSet rs=pr.executeQuery();
while(rs.next())
{
out.println("<br>"+rs.getString(1)+" "+rs.getString(2));
}
Image Created0Down

By: [email protected] On: Thu Apr 18 18:00:23 IST 2013 Question Reputation27 Answer Reputation11 Belt Series Points0 38User Image
Are You Satisfied :2Yes0No
 
I have a problem when i use servletcontexListener. And access the database in servlet1.It is accessing and close that connection there(con.close()).and after that i access it in servlet2 it is not accessin because connection has CLOSED.and we can make connection again because SERVLETCONTEXTLISTENER work only when web application deploy.so i can not open that connection by using this way.

SO my question is :
1. IS it necessary to close the connection.Can we make open connection.????
2. Is there any way to use that connection again in another resource because connection is closed.and can not be opened.

Image Created0Down

By: [email protected] On: Tue Apr 23 22:41:09 IST 2013 Question Reputation27 Answer Reputation11 Belt Series Points0 38User Image
Are You Satisfied :0Yes1No
 
yes it is necessary to close the connection if you are creating a new connection on each jsp page or in servlet .In your case you are not creating connection again and again, you have put the object of connection in application scope,and you are using that on each page.so you need only to close the connection in Listener class in destroyed().Define the Connection con as null outside the all overridden method so that you can initialize it in initialized() and close it in destroyed ().Image Created0Down

By: [email protected] On: Wed Apr 24 01:45:56 EDT 2013 Question Reputation0 Answer Reputation147 Belt Series Points0 147User Image
Are You Satisfied :1Yes0No
 
thank u so much rishi..Image Created0Down

By: [email protected] On: Wed Apr 24 02:01:29 EDT 2013 Question Reputation27 Answer Reputation11 Belt Series Points0 38User Image
Are You Satisfied :0Yes0No
 
Is there any disadvantage to use connection through ServletContextListener??????Image Created0Down

By: [email protected] On: Wed Apr 24 04:55:22 EDT 2013 Question Reputation27 Answer Reputation11 Belt Series Points0 38User Image
Are You Satisfied :0Yes0No
 
yes, there may be problem of concurrent accessibility of connection object(i.e. only one) when many users are using your application at the same timeImage Created0Down

By: [email protected] On: Wed Apr 24 07:41:34 EDT 2013 Question Reputation0 Answer Reputation147 Belt Series Points0 147User Image
Are You Satisfied :0Yes0No