Practical 1C: 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.


MySql queries


create database LoginDB;

use LoginDB;

create table user(username varchar(20) PRIMARY KEY, password varchar(20), email varchar(20), country varchar(20));

insert into user values ('admin','admin','admin@admin.com','India');

select * from user;



  •    Add jar file <<Netbean Folder>>\ide\modules\ext\mysql-connector-java-5.1.23-bin.jar




index.html

<html><head><title>Registration Page</title></head>
<body>

<form action="RegisterServlet" >

<H1>Welcome to Registration page</H1>

Enter User Name

Enter Password

Enter Email

Enter Country

<input type="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();
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:mysql://localhost:3306/logindb");
PreparedStatement pst = con.prepareStatement("insert into user values(?,?,?,?)");
pst.setString(1,id);
pst.setString(2,ps);
pst.setString(3,em);
pst.setString(4,co);
int row = pst.executeUpdate();
out.println("<h1>"+row+ " Inserted Successful");
}
catch(Exception e)
{
out.println(e);
}

}

}



Comments