Hi.i am beginner. using eclipse. i am sure my deployment structure is right.
web.xml
---------- --------------
<?xml version="1.0"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<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/blackbook
</param-value>
</context-param>
<
servlet>
<servlet-name>ls</servlet-name>
<servlet-class>com.santosh.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>admin</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.santosh.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>
--- ---- --- ---- --- ----- --- --- -----
<!--Home.html-->
--- ---- ---- --- ---- --- ---- ------ --
<html><body>
<center><h1>XYZ Company Ltd.</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 xyz.com
</td>
</tr>
</table>
</body></html>
-------- -------------- --------
<!--Register.html-->
-------- ----------- ------ ----- -------- ---------- ------ --------
<html> <body>
<center><h1>XYZ Company Ltd.</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>
-- ----- ------- -------- --------- -------- ------ ------- ------- ------
LoginServlet.java
------- --------- ------- ---------- ----------- ------ ------ ----------
package com.santosh.servlets;
import javax.servlet.*;
import java.io.*;
import java.sql.*;
/**
* @author Santosh
*/
public class LoginServlet extends GenericServlet {
private Connection con;
private PreparedStatement ps=null;
public void init() throws ServletException {
System.out.println("In init");
try {
ServletContext ctxt=getServletContext();
//Getting the Driver class name from context parameter
String driverClassName=ctxt.getInitParameter("driverClassName");
Class.forName(driverClassName);
//Getting the JDBC URL from context parameter
String url=ctxt.getInitParameter("url");
//Getting the DB Username, password & sqlstatement from servlet init parameter
String dbuser=getInitParameter("dbuser");
String dbpass=getInitParameter("dbpass");
String sqlst=getInitParameter("sqlstatement");
con=DriverManager.getConnection(url, dbuser, dbpass);
ps=con.prepareStatement(sqlst);
}//try
catch(Exception e){
e.printStackTrace();
throw new ServletException("Initialization failed, Unable to get DB connection");
}//catch
}//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>User Name and Password cannot be empty</i></li><br/>");
out.println("<li><i>We cannot 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>XYZ Company 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>Enjoy browsing the Site</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 cannot log you into your account at this time. Please try again later</i>");
out.println("</center></body></html>");
}
}//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>");
}//catch
}//service
public void destroy () {
System.out.println("In destroy");
try {con.close();}
catch(Exception e){}
}//destroy
}//class
--- -------- ------ -------- ---- ------- --------- ------- -------------
RegistrationServlet.java
---- -------- --------- -------- ------- ---- ----- ---- --------- -- --------
package com.santosh.servlets;
import javax.servlet.*;
import java.io.*;
import java.sql.*;
/**
* @author Santosh
*/
public class RegistrationServlet extends GenericServlet {
private Connection con;
private PreparedStatement ps=null;
public void init() throws ServletException {
System.out.println("In init");
try {
ServletContext ctxt=getServletContext();
//Getting the Driver class name from context parameter
String driverClassName=ctxt.getInitParameter("driverClassName");
Class.forName(driverClassName);
//Getting the JDBC URL from context parameter
String url=ctxt.getInitParameter("url");
//Getting the DB Username, password & sqlstatement from servlet init parameter
String dbuser=getInitParameter("dbuser");
String dbpass=getInitParameter("dbpass");
String sqlst=getInitParameter("sqlstatement");
con=DriverManager.getConnection(url, dbuser, dbpass);
ps=con.prepareStatement(sqlst);
}//try
catch(Exception e){
e.printStackTrace();
throw new ServletException("Initialization failed, Unable to get DB connection");
}//catch
}//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>Given details are not valid to register</i></li><br/>");
out.println("<li><i>Please try again later</i>");
out.println("</center></body></html>");
return;
}//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>XYZ Company 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><br/>");
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("<li><i>Please try again later</i>");
out.println("</center></body></html>");
}
}//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>");
}//catch
}//service
public void destroy () {
System.out.println("In destroy");
try {con.close();}
catch(Exception e){}
}//destroy
}//class
------- ---------- ---------- ------- --------- ------ ------ --- ------ -------
I am using mysql database.
database name is blackbook.
table name is userdetails.
my userdetails table is as follows:
uname pass address phno email
santosh kumar hyd 97049 to_sak@yaho
--- --------- --- -------- ----- ----- --- ---- --- ------ ------- --- -- ---- --
i am able to enter to the home.html page.
then i clicked on login to reach login.html page.
there i click username as santosh and password as kumar and then submit. then i get the following exception on browser
HTTP Status 500 -
-- ----- ----- ---- ------------ ---- ------ ----- ------ --- - -- ----- ------ ------- -----
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Initialization failed, Unable to get DB connection
com.santosh.servlets.LoginServlet.init(LoginServlet.java:36)
javax.servlet.GenericServlet.init(GenericServlet.java:212)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)
org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
java.lang.Thread.run(Thread.java:595)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.26 logs.
----
Apache Tomcat/5.5.26
---- ---------- - ------ ---- ------ -------- -- -------- ------- ---- --- ------- --- --my console looks like this
Oct 27, 2008 5:50:13 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The Apache
Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: E:\jdk\bin;.;C:\WINDOWS\system32;C:\WINDOWS;E:/jdk/bin/../jre/bin/client;E:/jdk/bin/../jre/bin;E:\jdk\bin;C:\Program Files\QuickTime\QTSystem\;C:\Program Files\MySQL\MySQL Server 5.0\bin;E:\jdk\bin
Oct 27, 2008 5:50:13 PM org.apache.coyote.http11.Http11BaseProtocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
Oct 27, 2008 5:50:13 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1328 ms
Oct 27, 2008 5:50:14 PM org.apache.catalina.core.StandardService start
INFO: Starting service Catalina
Oct 27, 2008 5:50:14 PM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/5.5.26
Oct 27, 2008 5:50:14 PM org.apache.catalina.core.StandardHost start
INFO: XML validation disabled
Oct 27, 2008 5:50:15 PM org.apache.coyote.http11.Http11BaseProtocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Oct 27, 2008 5:50:15 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Oct 27, 2008 5:50:15 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/78 config=null
Oct 27, 2008 5:50:15 PM org.apache.catalina.storeconfig.StoreLoader load
INFO: Find registry server-registry.xml at classpath resource
Oct 27, 2008 5:50:16 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 2391 ms
In init
java.sql.SQLException: Unable to connect to any hosts due to exception: java.lang.ArrayIndexOutOfBoundsException: 40
at com.mysql.jdbc.Connection.createNewIO(Connection.java:1797)
at com.mysql.jdbc.Connection.<init>(Connection.java:562)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:361)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at com.santosh.servlets.LoginServlet.init(LoginServlet.java:31)
at javax.servlet.GenericServlet.init(GenericServlet.java:212)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1139)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:791)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:127)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Thread.java:595)
Oct 27, 2008 5:52:08 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet ls
javax.servlet.ServletException: Initialization failed, Unable to get DB connection
at com.santosh.servlets.LoginServlet.init(LoginServlet.java:36)
at javax.servlet.GenericServlet.init(GenericServlet.java:212)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1139)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:791)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:127)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Thread.java:595)
---- ------ ----- ------ ---- ------ --------------- --------- -----
someone please run this code and let me know where i am going wrong..
hope i could ask my question very clearly.
eagerly waiting for the solution