
VIEW:
Registration.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Registration</title>
</head>
<body>
<form method="POST" action="register">
<font color="green">
<h2> Sookshmas Registration Form</h2>
</font>
<table>
<tr>
<td> NAME </td>
<td> <input type="text" name="name"></td>
</tr>
<tr>
<td> PHONE NUMBER </td>
<td> <input type="number" name="phone" > </td>
</tr>
<tr>
<td> Email </td>
<td> <input type="email" name="email"></td>
</tr>
<tr>
<td> Password </td>
<td> <input type="password" name="pw"></td>
</tr>
<tr>
<td> Confirm Password </td>
<td> <input type="password" name="cp"></td>
</tr>
<tr>
<td> </br> <input type="submit" name="submit" value="Register"></td>
</tr>
</table>
</form>
</body>
</html>
Status.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%-- <% java source code %> (JSP scriptlet tag) --%>
<% if(request.getParameter("status1")!=null) {%>
<h1><%= request.getParameter("status1")%></h1>
<%-- <%= "statement" %> --%>
<%-- JSP expression tag is written to the output stream of the response --%>
<%}%>
</body></html>
Controller:
register servlet:
@WebServlet(name = "register", urlPatterns = {"/register"})
public class register extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
Registration reg = new Registration();
try {
if (request.getParameter("submit") != null)
{
String name = request.getParameter("name");
// public String getParameter(String name):is used to obtain the value of a parameter by name
String phone = request.getParameter("phone");
String email = request.getParameter("email");
String pw = request.getParameter("pw");
String cp = request.getParameter("cp");
String status1 = "";
if (pw.equals(cp))
{
String status = reg.Registration(name, phone, email, pw);
if (status.equals("existed")) {
status1 = "existed";
} else if (status.equals("success")) {
status1 = "success";
} else if (status.equals("failure")) {
status1 = "failure";
}
//The RequestDispatcher interface provides the facility of
//dispatching the request to another resource it may be html, servlet or jsp.
RequestDispatcher rd1 = request.getRequestDispatcher("status.jsp?status1=" + status1);
//The getRequestDispatcher() method of ServletRequest interface returns the object of RequestDispatcher
rd1.forward(request, response);
// Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server.
} else {
status1 = "Password doesn't match";
RequestDispatcher rd1 = request.getRequestDispatcher("status.jsp?status1=" + status1);
rd1.forward(request, response);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
MODEL:
public class Registration {
Connection con;
Statement st;
ResultSet rs;
public Registration() {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sookshmas", "root", "root");
} catch (Exception e) {
e.printStackTrace();
}
}
public String Registration(String name, String phone, String email, String pw)
{
PreparedStatement ps;
String status = "";
try {
st = con.createStatement();
rs = st.executeQuery("select * from sookshmas where phone='" + phone + "' or email='" + email + "';");
boolean b = rs.next();
if (b) {
status = "existed";
} else {
ps = (PreparedStatement) con.prepareStatement("insert into sookshmas values(0,?,?,?,?,now())");
ps.setString(1, name);
ps.setString(2, phone);
ps.setString(3, email);
ps.setString(4, pw);
int a = ps.executeUpdate();
if (a > 0) {
status = "success";
} else {
status = "failure";
}
}
} catch (Exception e) {
e.printStackTrace();
}
return status;
}
}
SQL :
CREATE TABLE sookshmas (
slno int(10) NOT NULL AUTO_INCREMENT,
name varchar(45) NOT NULL,
phone varchar(45) NOT NULL unique,
email varchar(45) NOT NULL unique,
password varchar(45) NOT NULL,
date datetime NOT NULL,
PRIMARY KEY (slno)
);
insert into sookshmas
values(0,'karthik','123456','[email protected]','pass',now());