0

i wrote a basic java servlet code which is not working. Iam using oracle12c release 2 and apache tomcat 9.0. The below doesnot show any error but after clicking submit in html pae it loads into an empty page

MY html file is

<html>
<body>
<form method="get" action="srv" align="center">
<table>
<tr><td>
UName</td><td> <input type="text" name="nm"></td></tr>
<tr><td>
Password</td><td><input type="password" name="pd"></td></tr>
<tr><td>
<input type="submit" value="send"></td></tr></table>
</form>
</body>
</html>

MY servlet file is

import java.io.*;
 import javax.servlet.*;
import java.sql.*;
  public class select extends GenericServlet
{
Connection con;
PreparedStatement ps;
public void init() throws ServletException
{
    try

    {
    Class.forName("oracle.jdbc.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","C##
 1","Oracle_12");
    ps=con.prepareStatement("select * from emp where name=?");}
    catch(Exception e)
    {
        e.printStackTrace();
    }
}
public void service(ServletRequest req,ServletResponse res) throws 
  ServletException,IOException
  {
    try
    {
        String name=req.getParameter("nm");
        //String pwd=req.getParameter("pd");
        ps.setString(1,name);
        //ps.setString(2,pwd);
        ResultSet rs=ps.executeQuery();
        res.setContentType("text/html");
        PrintWriter pw=res.getWriter();
        if(rs.next())
        {
            String name1=rs.getString(1);
            String email1=rs.getString(2);
            String pws=rs.getString(3);
            pw.println("<html>"+
            "<body><h1 align=center>Hello</h1><br>"+
            "<table><tr><th>Name</th><th>Password</th><th>Email</th></tr>"+
            "<tr><td>"+name1+"</td><td>"+pws+"</td><td>"+email1+"</td></tr>
 </table></body></html>");
        }
        else
        {
            pw.println("<html><body><h1 align=center>Try again!</body>
   </html>");
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}
public void destroy()
{
    try

    {con.commit();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}
}

My XML file is

  <web-app>
   <servlet>
  <servlet-name>s1</servlet-name>
  <servlet-class>select</servlet-class>
  </servlet>
  <servlet-mapping>
<servlet-name>s1</servlet-name>
<url-pattern>/srv</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
<welcome-file>home.html</welcome-file>
</welcome-file-list>
 </web-app>

EDIT:This is solved by adding the OJDBC.jar file in the servlets lib folder.It has nothing to do with the errors in the program.

Thakur Karthik
  • 3,034
  • 3
  • 15
  • 24

2 Answers2

1

Have you added the oracle jdbc driver in the classpath? It would be helpful if you can provide tomcat logs (or Eclipse console logs if you are running from Eclipse IDE). It works when I ran, so no issues with the code. Check if you have added the OJDBC driver jar to your classpath..

ManoB
  • 31
  • 3
  • I have added my classpath as C:\app\iamka\product\12.2.0\dbhome_1\jdbc\lib\ojdbc8.jar still no use and iam not using eclipse or any id yet – Thakur Karthik Jul 08 '17 at 08:02
  • Could you post the logs in your . $TOMCAT_HOME/logs/catalina.out. – ManoB Jul 08 '17 at 08:30
  • Most likely you have not added the classpath correctly. You need to add setenv.bat file with below line CLASSPATH=C:\app\iamka\product\12.2.0\dbhome_1\jdbc\lib\ojdbc8.jar setenv.bat should be in the bin folder of TOMCAT home – ManoB Jul 08 '17 at 08:33
  • If i add ojdbc8.jar in the lib folder of my servlet then it is working Thanks for your answer though :) – Thakur Karthik Jul 11 '17 at 10:47
0

Add the ojdbc8.jar file in the lib folder your present working servlet project.

Thakur Karthik
  • 3,034
  • 3
  • 15
  • 24