Create a registration servlet in Java using JDBC. Accept the details such as Username, Password, Email, and Country from the user using HTML Form and store the registration details in the database.

 index.html

<html>

    <head>

        <title>Registration Page</title>

    </head> 

    <body> 

        <form action="RegisterServlet" >

            <H1>Welcome to Registration page</H1>    

            Enter UserName   :      <input type="text" name="txtUid"><br> <br>

            Enter Password   :     <input type="password" name="txtPass"><br><br>  

            Enter Email      :<input type="text" name="txtEmail" ><br><br>     

            Enter Country    : <input type="text" name="txtCon" ><br><br> 

            <input type="reset" value="Reset" >

            <input type="submit" value="REGISTER" > 

        </form> 

    </body> 

</html>

RegisterServlet.java

package mypack;

import java.io.*; 

import java.sql.*;

import javax.servlet.*;  

import javax.servlet.http.*; 

public class RegisterServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)                throws ServletException, IOException {               response.setContentType("text/html;charset=UTF-8");           

PrintWriter out = response.getWriter();  

        out.println("<html><head><title>Servlet Register</title></head>");  

        String id = request.getParameter("txtUid"); 

        String ps = request.getParameter("txtPass");  

        String em = request.getParameter("txtEmail");          String co = request.getParameter("txtCon");            try{   

            Class.forName("com.mysql.jdbc.Driver");   

            Connection con

=DriverManager.getConnection("jdbc:derby://localhost:1527/mysql","root","root");                 PreparedStatement pst = con.prepareStatement("insert into register values(?,?,?,?)");              pst.setString(1,id);  

            pst.setString(2,ps);                   pst.setString(3,em);                 pst.setString(4,co);                int row = pst.executeUpdate();              out.println("<body bgcolor=blue >"); 

            out.println("<h1>"+row+ " Inserted Succesfullyyyyy");  

        }

        catch(Exception e)

        {

            out.println("<body bgcolor=red >");                  out.println(e); 

        } 

    }

}


Post a Comment

0 Comments