www.webdeveloper.com
Results 1 to 5 of 5

Thread: Servlet program error

Hybrid View

  1. #1
    Join Date
    Feb 2014
    Posts
    2

    Servlet program error

    Hi , i have done a project in servlet , i didn't get any error in the coding and console page . My problem is that datas are not added to the database. Below are my files. What i'm doing wrong ?

    StudentRegistration.html

    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body bgcolor="#ccffff">
     <center><h1>"Welcome to Student Registration Form"</h1></center>
     <form method="post" action="user">
    		
    	
     <table>
    	 <tr>
    		<td> Name :<input type="text" name="fn" id="fn"></td>
    	 </tr>
    	 <tr>
    		<td> USN :     <input type="text" name="usn" id="usn"></td>
    	 </tr>
    	 <tr>
    		<td> DOB :     <input type="text" name="dob"></td>
    	 </tr>
    	 <tr>
    		<td> E.ID:<input type="text" name="email"></td>
    	 </tr>
    	 <tr>
    		<td> M.NO:<input type="text" name="mob"></td>
    	 </tr>
    	 <tr>
    		<td>domain: <input type="text" name="branch"></td>
    	 </tr>
    	 <tr>
    		<td>10th :     <input type="text" name="sslc"></td>
    	 </tr>
    	 <tr>
    		<td>12th :     <input type="text" name="puc"></td>
    	 </tr>
    	 <tr>
    		<td>degree :    <input type="text" name="deg"></td>
    	 </tr>
    
    	 <tr>
    		<td>BACKLOGS:<input type="text" name="back"></td>
    	 </tr>
    	<tr>
    	<td><center>SUBMIT :<input type="submit"></center></td>
    	 </tr>
     </table>
     </form>
    
    </body>
    </html>
    ------------------------------------------------------------------------------------------------
    StudentRegistrationServlet.java

    package Praveena.Shanthala;

    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.sql.*;
    public class StudentRegistrationServlet extends javax.servlet.http.HttpServlet
    {
    private PreparedStatement pstmt;
    private ResultSet rs =null;
    private Connection con;


    @Override
    public void init() throws ServletException {
    String user="root";
    String password="root";
    String url="jdbc:mysql://localhost";
    // String sql="select NAME from project.testdb where fn=? ,usn=? ,dob=? ,email=?, mob=? ,branch=? ,sslc=?, puc=? ,deg=? and back=?";
    String sql="insert into project.testdb(fName,lName)" + "values(?,?)";
    try {

    Class.forName("com.mysql.jdbc.Driver");
    System.out.println("--------------connection----------------");
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    }
    try {
    con=DriverManager.getConnection(url,user,password);
    System.out.println("----------establish-----------");
    } catch (SQLException e) {
    e.printStackTrace();
    }
    try {
    pstmt=con.prepareStatement(sql);
    System.out.println("--------prepare----------------");
    } catch (SQLException e) {
    e.printStackTrace();
    }


    }
    @Override
    public void destroy() {
    if(con!=null)
    {
    try {
    con.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String NAME=request.getParameter("fn");
    System.out.println("------name----------"+NAME);
    String USN=request.getParameter("usn");
    System.out.println("------name----------"+USN);


    try {
    pstmt.setString(1,"NAME");

    pstmt.setString(2,"USN");



    int rs = pstmt.executeUpdate();
    System.out.println("no of rows affected"+rs);
    if(rs>0)
    {
    response.getWriter().println("sucess"+NAME);
    }


    } catch (SQLException e) {
    e.printStackTrace();
    }

    }
    }
    ---------------------------------------------------------------------------------------

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>
    StudentRegistrationProject</display-name>
    <servlet>

    <servlet-name>StudentRegistrationServlet</servlet-name>
    <servlet-class>
    Praveena.Shanthala.StudentRegistrationServlet</servlet-class>
    </servlet>
    <servlet>
    <servlet-name>PlacementCell</servlet-name>
    <servlet-class>
    Praveena.Shanthala.PlacementCell</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>StudentRegistrationServlet</servlet-name>
    <url-pattern>/login</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
    <servlet-name>PlacementCell</servlet-name>
    <url-pattern>/user</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
    <welcome-file>Welcome.html</welcome-file>
    <welcome-file>StudentRegistration.html</welcome-file>
    <welcome-file>PlacementCell.html</welcome-file>
    <welcome-file>Error.html</welcome-file>

    </welcome-file-list>
    </web-app>

  2. #2
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    Do you get any errors?
    If so post them here pls..
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  3. #3
    Join Date
    Feb 2014
    Posts
    2
    Hi , i have done a project in servlet , i didn't get any error in the coding and console page . My problem is that datas are not added to the database. Below are my files. What i'm doing wrong ?

    StudentRegistration.html

    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body bgcolor="#ccffff">
    <center><h1>"Welcome to Student Registration Form"</h1></center>
    <form method="post" action="user">


    <table>
    <tr>
    <td> Name :<input type="text" name="fn" id="fn"></td>
    </tr>
    <tr>
    <td> USN : <input type="text" name="usn" id="usn"></td>
    </tr>
    <tr>
    <td> DOB : <input type="text" name="dob"></td>
    </tr>
    <tr>
    <td> E.ID:<input type="text" name="email"></td>
    </tr>
    <tr>
    <td> M.NO:<input type="text" name="mob"></td>
    </tr>
    <tr>
    <td>domain: <input type="text" name="branch"></td>
    </tr>
    <tr>
    <td>10th : <input type="text" name="sslc"></td>
    </tr>
    <tr>
    <td>12th : <input type="text" name="puc"></td>
    </tr>
    <tr>
    <td>degree : <input type="text" name="deg"></td>
    </tr>

    <tr>
    <td>BACKLOGS:<input type="text" name="back"></td>
    </tr>
    <tr>
    <td><center>SUBMIT :<input type="submit"></center></td>
    </tr>
    </table>
    </form>

    </body>
    </html>
    ------------------------------------------------------------------------------------------------
    StudentRegistrationServlet.java

    package Praveena.Shanthala;

    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.sql.*;
    public class StudentRegistrationServlet extends javax.servlet.http.HttpServlet
    {
    private PreparedStatement pstmt;
    private ResultSet rs =null;
    private Connection con;


    @Override
    public void init() throws ServletException {
    String user="root";
    String password="root";
    String url="jdbc:mysql://localhost";
    // String sql="select NAME from project.testdb where fn=? ,usn=? ,dob=? ,email=?, mob=? ,branch=? ,sslc=?, puc=? ,deg=? and back=?";
    String sql="insert into project.testdb(fName,lName)" + "values(?,?)";
    try {

    Class.forName("com.mysql.jdbc.Driver");
    System.out.println("--------------connection----------------");
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    }
    try {
    con=DriverManager.getConnection(url,user,password);
    System.out.println("----------establish-----------");
    } catch (SQLException e) {
    e.printStackTrace();
    }
    try {
    pstmt=con.prepareStatement(sql);
    System.out.println("--------prepare----------------");
    } catch (SQLException e) {
    e.printStackTrace();
    }


    }
    @Override
    public void destroy() {
    if(con!=null)
    {
    try {
    con.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String NAME=request.getParameter("fn");
    System.out.println("------name----------"+NAME);
    String USN=request.getParameter("usn");
    System.out.println("------name----------"+USN);


    try {
    pstmt.setString(1,"NAME");

    pstmt.setString(2,"USN");



    int rs = pstmt.executeUpdate();
    System.out.println("no of rows affected"+rs);
    if(rs>0)
    {
    response.getWriter().println("sucess"+NAME);
    }


    } catch (SQLException e) {
    e.printStackTrace();
    }

    }
    }
    ---------------------------------------------------------------------------------------

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>
    StudentRegistrationProject</display-name>
    <servlet>

    <servlet-name>StudentRegistrationServlet</servlet-name>
    <servlet-class>
    Praveena.Shanthala.StudentRegistrationServlet</servlet-class>
    </servlet>
    <servlet>
    <servlet-name>PlacementCell</servlet-name>
    <servlet-class>
    Praveena.Shanthala.PlacementCell</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>StudentRegistrationServlet</servlet-name>
    <url-pattern>/login</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
    <servlet-name>PlacementCell</servlet-name>
    <url-pattern>/user</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
    <welcome-file>Welcome.html</welcome-file>
    <welcome-file>StudentRegistration.html</welcome-file>
    <welcome-file>PlacementCell.html</welcome-file>
    <welcome-file>Error.html</welcome-file>

    </welcome-file-list>
    </web-app>

  4. #4
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    you have comment statements, do you see them printing out as well, specifically, do you get following line printed out?
    System.out.println("no of rows affected"+rs);
    if so, what is the rs value...which database is being used, is it MS SQL? if so try to run sql profiler and see if a connection is made at all..
    I will take a look later on today, in detail on your code, meanwhile post all the printout statement output here.
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  5. #5
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    Ok first thing that I have noticed is following...in your html page for action attribute you are sending request to user. Where as the code you posted is of the class StudentRegistrationServlet.java.
    You need to post code here of the following class
    Praveena.Shanthala.PlacementCell

    Or you should change the action attribute and point it to login so that the code executes which you have posted. Let me know what u get after making changes as suggested. There may be other issues, u must post errors that u are getting.
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles