❮ Previous Next ❯
JAVA Registration Form in JSP using MVC with database


Hii frnds, here in this article I have explained Registration application in Java using Servlet, JSP, and Oracle10g XE database server. This Java registration form follows Model View Controller (MVC) architecture. Let us see and understand how this should be used while developing any application.

Required Files:
1.     register.jsp                 //Welcome file
2.     registerBean.java         //Model Component
3.     registerServlet.java      //Controller Component
4.     registerDao.java
5.     welcome.jsp             //View Component
6.     web.xml                  //Deployment Descriptor File
jSP

Register.jsp

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title> Registration</title> 
<script> 
function validate()
{
   var fullname = document.form.fullname.value;
   var email = document.form.email.value;
    var username = document.form.username.value;
   var password = document.form.password.value;
    var conpassword= document.form.conpassword.value;
if (fullname==null || fullname=="")
{
alert("Full Name can't be blank");
return false;
}
else if (email==null || email=="")
{
alert("Email can't be blank");
return false;
}
else if (username==null || username=="")
{
alert("Username can't be blank");
return false;
}
else if(password.length<6)
{
alert("Password must be at least 6 characters long.");
return false;
}
else if (password!=conpassword)
{
alert("Confirm Password should match with the Password");
return false;
}
}

</script> 
</head> 
<body> 
<center> <h2>Registration Form</h2> </center> 
<form action="RegisterServlet" name="form" method="get"  onsubmit="return validate()"> 
<table align="center"> 
<tr> 
<td>Full Name</td> 
<td> <input type="text" name="fullname" / > </td> 
</tr> 
<tr> 
<td>Email</td> 
<td><input type="email" name="email" / > </td> 
</tr> 
<tr> 
<td>UserName</td> 
<td> <input type="text" name="username" / > </td> 
</tr>
<tr>
<td>Password</td> 
<td><input type="Password" name="password" / > </td> 
</tr> 
<tr>
<td>Confirm Password</td> 
<td><input type="Password" name="conpassword" / > </td> 
</tr> 
<tr>
<td><%=(request.getAttribute("errMessage") == null) ? "" : request.getAttribute("errMessage")%> </td> 
</tr> 
<tr>
<td></td> 
<td><input type="submit" value="Register"> </td> 
</tr> 
</table> 
</form> 
</body> 
</html>


RegisterBean.java

package java8s.bean;

public class RegisterBean
{
private String fullName;
private String email;
private String userName;
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getFullName() {
return fullName;
}
public void setEmail(String email) {
this.email = email;
}
public String getEmail() {
return email;
}
}

RegisterServlet.java

package java8s.controller;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java8s.bean.RegisterBean;
import java8s.dao.RegisterDao;

public class RegisterServlet extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String fullName = request.getParameter("fullname");
String email = request.getParameter("email");
String userName = request.getParameter("username");
String password = request.getParameter("password");
RegisterBean registerBean = new RegisterBean();
registerBean.setFullName(fullName);
registerBean.setEmail(email);
registerBean.setUserName(userName);
registerBean.setPassword(password);
RegisterDao registerDao = new RegisterDao();
try{
String s = registerDao.registerUser(registerBean);
if(s.equals("SUCCESS"))
{
request.getRequestDispatcher("/welcome.jsp").forward(request, response);
}
else
{
request.setAttribute("errMessage", s);
request.getRequestDispatcher("/register.jsp").forward(request, response);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

RegisterDao.java


package java8s.dao;
import java.sql.*;
import java8s.bean.RegisterBean;
public class RegisterDao
{
public String registerUser(RegisterBean registerBean) throws Exception
{ String fullName = registerBean.getFullName();
String email = registerBean.getEmail();
String userName = registerBean.getUserName();
String password = registerBean.getPassword();

int i=0;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
String query = "insert into users12 values(?,?,?,?)";
PreparedStatement ps=con.prepareStatement(query);
ps.setString(1, fullName);
ps.setString(2, email);
ps.setString(3, userName);
ps.setString(4, password);
i= ps.executeUpdate();
if (i> 0)
{
return "SUCCESS";
}
}
catch(SQLException e)
{
e.printStackTrace();
}
return "Something went wrong there..!";
}
}

welcome.jsp

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title> Insert title here</title> 
</head> 
<body> 
<%
out.print("registration is successful");
%>
</body> 
</html>



Web.xml


<web-app>
<display-name>CET</display-name>
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>java8s.controller.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/RegisterServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>register.jsp</welcome-file>
</welcome-file-list>
</web-app>


OUTPUT


❮ Previous Next ❯

Follow Us




  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved