1

I try to submit form data in html page, which in turn should call servlet and insert into database. Since it shows blank page after submitting, I am not sure where it causes problem. I have a try catch inside servlet class, because I dont get errors, I guess html form doesn't hit servlet class.

Please help me..

code: reg.html

<html>
<body bgcolor=green text=yellow>
<center>
<h1>Registration Form</h1>
<form action="reg">
First Name : <input type=text name=fname><br>
Last Name  : <input type=text name=lname><br>
User Name  : <input type=text name=uname><br>
Password   : <input type=password name=pword><br><br>
<input type=submit><input type=reset>
</form>
</center>
</body>
</html> 

web.xml:

<web-app>
<servlet>
 <servlet-name>abc</servlet-name>
 <servlet-class>RegServlet</servlet-class>
</servlet>
<servlet-mapping>
 <servlet-name>abc</servlet-name>
 <url-pattern>/reg</url-pattern>
</servlet-mapping>
<servlet>
 <servlet-name>abc2</servlet-name>
 <servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
 <servlet-name>abc2</servlet-name>
 <url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>

RegServlet.java

public class RegServlet extends GenericServlet
{
    Connection con;
    public void init()
    {
        try
        {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","manager");

        }
        catch(Exception ee)
        {
            System.err.println(ee);
        }
    }
    public void service(ServletRequest req,ServletResponse res)
    {
        try
        {
            String s1 = req.getParameter("fname");  
            String s2 = req.getParameter("lname");  
            String s3 = req.getParameter("uname");  
            String s4 = req.getParameter("pword");  
            PreparedStatement pstmt=con.prepareStatement("insert into uinfo values(?,?,?,?)");
            pstmt.setString(1,s1);  
            pstmt.setString(2,s2);  
            pstmt.setString(3,s3);  
            pstmt.setString(4,s4);  
            pstmt.executeUpdate();
            PrintWriter pw =res.getWriter();
            pw.println("<html><body bgcolor=wheat text=green>");
            pw.println("You are successfully registered");
            pw.println("<a href='login.html'>Login</a>");
            pw.println("</body></html>");
        }
        catch(Exception ee)
        {
            System.err.println(ee);
        }
    }   
} 
kaetzacoatl
  • 1,419
  • 1
  • 19
  • 27
  • what does that mean ? how to provide request – Rajakesari Subramanian Aug 14 '16 at 18:18
  • Start with putting `System.out.println("Service Start");` in the first line of the `service()` method. Then try submitting the form again. If you see Service Start in your console, you know the HTML and server configuration are correct. If you don't see that, then your code isn't being executed at all and you can concentrate on the HTML or server configuration (like web.xml). – Mark Olsson Aug 14 '16 at 18:42
  • i tried it. it doesn't hit the service method. Also i gave wrong password to database conncetion. it doesnt show any error even though i have try catch. so its not connecting to servlet class. Wondering what is the problem in html and web.xml file now. – Rajakesari Subramanian Aug 14 '16 at 18:59
  • Here are some things to try: take everything out of web.xml and put this before the start of the class: `@WebServet(name="RegServlet", urlPatterns={"reg.html", "reg"})`, that simplifies the servlet mapping. Change `GenericServlet` to `HttpServlet`, HttpServlet has better support for HTTP (as the name suggests). And move everything from the `service()` method to a `doPost()` method. Example: `protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ... }`. – Mark Olsson Aug 14 '16 at 19:08
  • i am getting following error in tomcat stderr file: 15-Aug-2016 02:03:56.967 INFO [http-nio-9090-exec-69] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive C:\Program Files\Apache Software Foundation\Tomcat 8.0\webapps\logindown.war has finished in 333 ms java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver java.lang.NullPointerException – Rajakesari Subramanian Aug 15 '16 at 15:18
  • i added a lib folder under WEB_INF folder and placed odbc6.jar file. it worked. Thank you so much guys for the help. – Rajakesari Subramanian Aug 15 '16 at 15:37

0 Answers0