Saturday 5 September 2015

Register -how to add a entry to a database using a jsp form and a servlet


watch the video


newjsp2.jsp

<%--
    Document   : newjsp
    Created on : 2 Sep, 2015, 10:25:39 PM
    Author     : harsh
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <title>Login page</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div>
            <h2>Register</h2>
            <form action="NewServlet1" method="post">
                Email:<input type="email" name="email"><br>
                Name :<input type="text" name="name"><br>
                PASS :<input type="password" name="pass"><br>
                <input type="submit" name="submit" value="submit">
            </form>
        </div>
    </body>
</html>

NewServlet1.java

// a pure processing servlet to authenticate the user
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class NewServlet1 extends HttpServlet
{
   /// private static final String url = "jdbc:mysql://localhost/vusers";

    //    private static final String user = "root";

      //  private static final String pw = "mysql";
           
    public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException
    {
        try
        {
            Class.forName("org.apache.derby.jdbc.ClientDriver");
            Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/users","hall","bed");
            PreparedStatement ps = conn.prepareStatement("INSERT INTO USERNAME VALUES (?,?,?)");
            // get data of form
                        String email=req.getParameter("email");
            String username = req.getParameter("name");
            String password = req.getParameter("pass");
            // set the values for parameters
            ps.setString(1,username);
            ps.setString(2,password);
                        ps.setString(3, email);
            ps.executeUpdate();//database is updated
                //HttpSession session = req.getSession(true);
                                //session.setAttribute("Welcome",username);
                res.sendRedirect("newjsp.jsp");  // Wel is
               
       
               
        }
        catch (Exception e)
        {
                    res.sendRedirect("newjsp1.jsp");
            System.out.println(e.getMessage());
        }
    }
}
       


       

1 comment:

  1. Nice Post... I would like to see more of it. Kip it up the Good work

    ReplyDelete