Context Initialization Parameters
- So far we discussed, Servlet Container supports to store startup and configuration information for a Servlet as initialization parameters (init parameters).
- init parameters are configured specific to the Servlet configured in the deployment descriptor file, means these data can be retrieved only one servlet
- But I want to configure data that has to be retrieved into all or more than one servlet.
- For example my web application contains two servlets , login and registration, and you want the Driver class name and the JDBC URL of both Servlets to establish a connection, for this situation we can implement by using the context initialization parameters (Context init parameters), which are provided under Servlet specification.
Syntax:
<context-param>
<param-name>parameter name</param-name>
<param-value>parameter value</param-value>
</context-param>
Example program:
Steps:
maintain the folder structure:
1. create the folder in any directivve
D:\webtechnologies\servlets\servlets prog\ContextInitializationParam
<! creating Home.html -->
<html>
<head>
<title>Home.html</title>
</head>
<body>
<center>
<h1> Lotus Technologies</h1>
</center>
<table border="1" width="100%" height="100%">
<tr>
<td width="15%" valign="top" align="center">
<br/><a href="Login.html">Login</a><br/>
<br/><a href="Register.html">Register</a><br/>
</td>
<td valign="top" align="center"><br/>
Welcome to Lotus Technologies
</td>
</tr>
</table>
</body>
</html>
step2 : Login.html
<html>
<body>
<center>
<h1>Lotus technologies</h1>
</center>
<table border="1" width="100%" height="100%">
<tr>
<td width="15%" valign="top" align="center">
<br/><a href="Login.html">Login</a><br/>
<br/><a href="Register.html">Register</a><br/>
</td>
<td valign="top" align="center"><br/>
<form action="login">
<table>
<tr>
<td colspan="2" align="center"><b>Login Page</b></td>
</tr>
<tr>
<td colspan="2" align="center"><b> </td>
</tr>
<tr>
<td>User Name</td>
<td><input type="text" name="uname"/>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pass"/>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Login"/>
<input type="reset" value="Cancel"/></td>
</td>
</tr>
</table>
</form>
</tr>
</table>
</body>
</html>
Step3:Register.html
<! creating Register.html-->
<html><head><title>Register.html</title></head>
<body>
<center><h1>Lotus Technologies</h1></center>
<table border="1" width="100%" height="100%">
<tr>
<td width="15%" valign="top" align="center">
<br/><a href="Login.html">Login</a></br>
<br/><a href="Register.html">Register</a></br>
</td>
<td valign="top" align="center"><br/>
<form action="register">
<table>
<tr> <td colspan="2" align="center"><b>Registration Page</b></td>
</tr>
<tr>
<td colspan="2" align="center"><b> </td>
</tr>
<tr>
<td>User Name</td>
<td><input type="text" name="uname"/></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pass"/></td>
</tr>
<tr>
<td>Re-Password</td>
<td><input type="password" name="repass"/></td>
</tr>
<tr>
<td>Address</td>
<td><input type="text" name="addr"/></td>
</tr>
<tr>
<td>Phone Number</td>
<td><input type="text" name="phno"/></td>
</tr>
<tr>
<td>Email ID </td>
<td><input type="text" name="email"/></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Register"/>
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>
Step 4: create a src folder( this is optional) of your folder
Ex: D:\webtechnologies\servlets\servlets prog\ContextInitializationParam\src
now create a servlet programs:
//RegistrationServlet.java
package com.rajendra.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import javax.servlet.GenericServlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class RegistrationServlet extends GenericServlet
{
private Connection con;
private PreparedStatement ps=null;
public void init() throws ServletException {
System.out.println("In init");
try
{
/*This ServletContext object can be used to get configuration information from web.xml file. There is only one ServletContext object per web application. */
ServletContext sc=getServletContext();
//Getting the Driver class name from context parameter
String driverClassName=sc.getInitParameter("driverClassName");
Class.forName(driverClassName);
//getting the jdbc url from context parameter
String url=sc.getInitParameter("url");
//Getting the DB username, password and sqlstatement from servlet initparameter
String dbuser=getInitParameter("dbuser");
String dbpass=getInitParameter("dbpass");
String sqlst=getInitParameter("sqlstatement");
con=DriverManager.getConnection(url,dbuser,dbpass);
ps=con.prepareStatement(sqlst);
}//end of try
catch(Exception e)
{
e.printStackTrace();
throw new ServletException("Initialization failed, unable to get DB connection");
}//end of catch
}//end init
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
{
System.out.println("in service");
res.setContentType("text/html");
PrintWriter out=res.getWriter();
try
{
String uname=req.getParameter("uname");
String pass=req.getParameter("pass");
String repass=req.getParameter("repass");
if(uname==null||uname.equals("")||pass==null||pass.equals("")||!pass.equals(repass))
{
out.println("<html><body><center>");
out.println("<li><i>");
out.println("Given details are not valid to register</i></li><br/> ");
out.println("<li><i>Please try again later</i>");
return;
}//end of if
String addr=req.getParameter("addr");
String phno=req.getParameter("phno");
String email=req.getParameter("email");
ps.setString(1,uname);
ps.setString(2,pass);
ps.setString(3,addr);
ps.setString(4,phno);
ps.setString(5,email);
int count=ps.executeUpdate();
if(count==1||count==Statement.SUCCESS_NO_INFO)
{
out.println("<html><body>");
out.println("<center><h1>Lotus Technologies Ltd.</h1></center>");
out.println("<table border='1' width='100%' height='100%'>");
out.println("<tr>");
out.println("<td width='15%' valign='top' align='center'>");
out.println("<br/><a href='Login.html'>Login</a>");
out.println("<br/><a href='Register.html'>Register</a>");
out.println("</td>");
out.println("<td valign=\"top\" align=\"center\"><br/>");
out.println("<h3>Welcome, "+uname+"</h3><br/>");
out.println("<h2>Enjoy browsing the Site</h2>");
out.println("</td></tr></table>");
out.println("</body></html>");
}
else
{
out.println("<html><body><center>");
out.println("given details are incorrect<br/>");
out.println("</center></body></html>");
}
}//end of try
catch(Exception e)
{
out.println("<html><body><center>");
out.println("<h2>Unable to the process the request try after some time </h2>");
out.println("</center></body></html>");
}
}//service
public void destroy()
{
System.out.println("in destroy ()");
try
{
con.close();
}
catch(Exception e){}
}//destry
}//class
Now compile this program to get class file, copy class file and paste into classes folder of WEB-INF folder go to classes folder
Example:
Ex: D:\webtechnologies\servlets\servlets prog\ContextInitializationParam\WEB-INF
D:\webtechnologies\servlets\servlets prog\ContextInitializationParam\WEB-INF\classes
step 5: create a servlet program of LoginServet.java in src folder
//LoginServet.java
package com.rajendra.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.GenericServlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class LoginServlet extends GenericServlet
{
private Connection con;
private PreparedStatement ps=null;
public void init() throws ServletException
{
System.out.println("In init");
try
{
ServletContext sc=getServletContext();
//getting the Driver class name from context parameter
String driverClassName=sc.getInitParameter("driverClassName");
Class.forName(driverClassName);
//getting the jdbc url from context parameter
String url=sc.getInitParameter("url");
//getting the DB username and pwd and sql statement from servlet init parameter
String dbuser=getInitParameter("dbuser");
String dbpass=getInitParameter("dbpass");
String sqlst=getInitParameter("sqltatement");
con=DriverManager.getConnection(url,dbuser,dbpass);
ps=con.prepareStatement(sqlst);
}//end of try
catch(Exception e)
{
e.printStackTrace();
throw new ServletException("initialization failed, Unable to get db connection");
}
} //end of init()
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
{
System.out.println("In service()");
res.setContentType("text/html");
PrintWriter out=res.getWriter();
try
{
String uname=req.getParameter("uname");
String pass=req.getParameter("pass");
if(uname==null||uname.equals("")||pass==null||pass.equals(""))
{
out.println("<html><body><center>");
out.println("<li><i>");
out.println("user name and password can not be empty </i></li><br/>");
out.println("<li><i>we can not log you into your account at this time. Please try again later</i>");
out.println("<center></body></html>");
return;
}//if
ps.setString(1,uname);
ps.setString(2,pass);
ResultSet rs=ps.executeQuery();
if(rs.next())
{
out.println("<html><body>");
out.println("<center><h1>Lotus Technologies Ltd</h1></center>");
out.println("<table border='1' width='100%' height='100%'>");
out.println("<tr>");
out.println("<td width='15%' valign='top' align='center'>");
out.println("<br/><a href='Login.html'>Login</a><br/>");
out.println("<br/><a href='Register.html'>Register</a>");
out.println("</td>");
out.println("<td valign='top' align='center'><br/>");
out.println("<h3>Welcome, "+uname+"</h3><br/>");
out.println("<h2>:-)</h2>");
out.println("</td></tr></table>");
out.println("</body></html>");
}
else
{
out.println("<html><body><center>");
out.println("Given username and password are incorrect <br/>");
out.println("<li><i>we can not allow you to log u , please try again</i>");
out.println("</center></body></html>");
}
}//end of try
catch(Exception e)
{
out.println("<html> <body><center>");
out.println("<h2>Unable to the process the request try after some time</h2>");
out.println("</center></body></html>");
}//end of catch
}//service
public void destry()
{
System.out.println("in destry");
try
{
con.close();
}
catch(Exception e){}
}
}//class
Now compile this program to get class file, copy class file and paste into classes folder of WEB-INF folder go to classes folder
Example:
Ex: D:\webtechnologies\servlets\servlets prog\ContextInitializationParam\WEB-INF
D:\webtechnologies\servlets\servlets prog\ContextInitializationParam\WEB-INF\classes
Step : create WEB-INF folder
Ex: D:\webtechnologies\servlets\servlets prog\ContextInitializationParam\WEB-INF
create
classes folder and web.xml file
go to classes folder
D:\webtechnologies\servlets\servlets prog\ContextInitializationParam\WEB-INF\classes
Step 7: create web.xml in WEB-INF Folder
Ex; D:\webtechnologies\servlets\servlets prog\ContextInitializationParam\WEB-INF
//web.xml
<web-app>
<context-param>
<param-name>driverClassName</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</context-param>
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/test</param-value>
</context-param>
<servlet>
<servlet-name>ls</servlet-name>
<servlet-class>com.rajendra.servlets.LoginServlet</servlet-class>
<init-param>
<param-name>dbuser</param-name>
<param-value>root</param-value>
</init-param>
<init-param>
<param-name>dbpass</param-name>
<param-value>admit</param-value>
</init-param>
<init-param>
<param-name>sqlstatement</param-name>
<param-value>select * from userdetails where uname=? and pass=?</param-value>
</init-param>
</servlet>
<servlet>
<servlet-name>rs</servlet-name>
<servlet-class>com.rajendra.servlets.RegistrationServlet</servlet-class>
<init-param>
<param-name>dbuser</param-name>
<param-value>root</param-value>
</init-param>
<init-param>
<param-name>dbpass</param-name>
<param-value>admin</param-value>
</init-param>
<init-param>
<param-name>sqlstatement</param-name>
<param-value>insert into userdetails values(?,?,?,?,?)
</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ls</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>rs</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>Home.html</welcome-file>
</welcome-file-list>
</web-app>
No comments:
Post a Comment