This post explains the Registration applications in Java using Servlet, JSP, and MySQL database servers. This Java registration form follows Model View Controller (MVC) architecture. Let us understand what is MVC and how this should be used while developing any application.
For Login application in java, refer to the post Login application in Java using MVC
The Model View Controller (MVC) architecture can be best explained with the help of the following diagram.

We are making use of Eclipse Integrated development environment(IDE) for this Java Registration Application. To follow the basic coding standards, I have created my folder structure as shown in the following diagram.
Login Applications in Java:
Any Registration or Login application always begins with views. It can be a page in HTML, JSP, PHP, VB, or any other language. Our Java Registration application begins with Register.jsp and from Register.jsp we will call RegisterServlet.java class.
Register.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Register</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>Java Registration application using MVC and MySQL </h2></center>
<form name="form" action="RegisterServlet" method="post" 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="text" 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"></input><input
type="reset" value="Reset"></input></td>
</tr>
</table>
</form>
</body>
</html>

RegisterServlet.java
package com.mvc.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mvc.bean.RegisterBean;
import com.mvc.dao.RegisterDao;
public class RegisterServlet extends HttpServlet {
public RegisterServlet() {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//Copying all the input parameters in to local variables
String fullName = request.getParameter("fullname");
String email = request.getParameter("email");
String userName = request.getParameter("username");
String password = request.getParameter("password");
RegisterBean registerBean = new RegisterBean();
//Using Java Beans - An easiest way to play with group of related data
registerBean.setFullName(fullName);
registerBean.setEmail(email);
registerBean.setUserName(userName);
registerBean.setPassword(password);
RegisterDao registerDao = new RegisterDao();
//The core Logic of the Registration application is present here. We are going to insert user data in to the database.
String userRegistered = registerDao.registerUser(registerBean);
if(userRegistered.equals("SUCCESS")) //On success, you can display a message to user on Home page
{
request.getRequestDispatcher("/Home.jsp").forward(request, response);
}
else //On Failure, display a meaningful message to the User.
{
request.setAttribute("errMessage", userRegistered);
request.getRequestDispatcher("/Register.jsp").forward(request, response);
}
}
}
To summarize RegisterServlet.java flow :
1. Assign all the inputs(user details) to local variables.
2. Call RegisterBean.java to set all the user details using java setters.
3. Next, go to RegisterDao.java where you are just going to insert user details into the database.
4. Once it is successful, you are displaying a successful message.
RegisterDao.java
Data Access Object – It focuses on business logic with database connections and operations.
Here the RegisterDao.java code makes a connection with the Database layer and inserts user details into the database.
package com.mvc.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.mvc.bean.RegisterBean;
import com.mvc.util.DBConnection;
public class RegisterDao {
public String registerUser(RegisterBean registerBean)
{
String fullName = registerBean.getFullName();
String email = registerBean.getEmail();
String userName = registerBean.getUserName();
String password = registerBean.getPassword();
Connection con = null;
PreparedStatement preparedStatement = null;
try
{
con = DBConnection.createConnection();
String query = "insert into users(SlNo,fullName,Email,userName,password) values (NULL,?,?,?,?)"; //Insert user details into the table 'USERS'
preparedStatement = con.prepareStatement(query); //Making use of prepared statements here to insert bunch of data
preparedStatement.setString(1, fullName);
preparedStatement.setString(2, email);
preparedStatement.setString(3, userName);
preparedStatement.setString(4, password);
int i= preparedStatement.executeUpdate();
if (i!=0) //Just to ensure data has been inserted into the database
return "SUCCESS";
}
catch(SQLException e)
{
e.printStackTrace();
}
return "Oops.. Something went wrong there..!"; // On failure, send a message from here.
}
}


RegisterBean.java
JavaBeans are classes that encapsulate many objects into a single object (the bean). A JavaBean property is a named feature that can be accessed by the user of the object. Here the RegisterBean encapsulates registration properties fullName, email, userName, password. To set or access individual properties, set and get functions have been implemented.
package com.mvc.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;
}
}
DBConnection.java We are making use of MySQL database server in this application. The URL format and driver name going to be different for different database servers.
package com.mvc.util;
import java.sql.Connection;
import java.sql.DriverManager;
public class DBConnection {
public static Connection createConnection()
{
Connection con = null;
String url = "jdbc:mysql://localhost:3306/customers"; //MySQL URL followed by the database name
String username = "root"; //MySQL username
String password = "root123"; //MySQL password
System.out.println("In DBConnection.java class ");
try
{
try
{
Class.forName("com.mysql.jdbc.Driver"); //loading MySQL drivers. This differs for database servers
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
con = DriverManager.getConnection(url, username, password); //attempting to connect to MySQL database
System.out.println("Printing connection object "+con);
}
catch (Exception e)
{
e.printStackTrace();
}
return con;
}
}
web.xml
The web.xml is known as the deployment descriptor. It declares which Servlets exist and which URLs they handle. You need to mention the fully qualified path for Servlet here.
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>RegistrationMVC</display-name> <welcome-file-list> <welcome-file>Register.jsp</welcome-file> </welcome-file-list> <servlet> <description></description> <display-name>RegisterServlet</display-name> <servlet-name>RegisterServlet</servlet-name> <servlet-class>com.mvc.controller.RegisterServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>RegisterServlet</servlet-name> <url-pattern>/RegisterServlet</url-pattern> </servlet-mapping> </web-app>
Home.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Home Page</title> </head> <body> <center><h2>Home Page</h2></center> <b>User Registration Successful</b> <br></br> <b>Please <a href="https://krazytech.com/programs/a-login-application-in-java-using-model-view-controllermvc-design-pattern">log-in</a> to continue.</b> </body> </html>

The code base can be downloaded from here.
I try to run the code but the following is message —Servlet RegisterServlet at /Project2017Maven— I think its something about the web.xml file. You see in my project I have a web.xml file but it is for a different servlet.
Hi Adrian,
Thanks for the visit. Certainly I will help you with this.
Yes. I think it’s something to do with your web.xml. Hope you have only one xml with following settings.
Also, cross check following line in your Register.jsp
You see i have a project and I want a register and login. I have my own register form and I am sending it to
RegisterServlet in the action. I have revamped the files in RegisterBean, RegisterDao and the Dbconnection to be relevent to my project. I have a web.xml file in my project but it is for a different servlet, if I try to add a different .xml file my project will not run.
You can have only one web.xml.
If you want to have multiple servlets, add multiple servlet and servlet-mapping tags.
Is the correct code to add a different servlet to the web.xml file.
RegisterServlet
com.mvc.controller.RegisterServlet
RegisterServlet
/RegisterServlet
Yes.
You can refer to the provided web.xml.
Note : Tags are not visible as it is xml.
I added the apropriate code and the program runs but it gives an message of —-Servlet RegisterServlet at /Project2017Maven—-
now the error appears——
HTTP Status 500 –
type Exception report
message
description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
com.mvc.dao.RegisterDao.registerUser(RegisterDao.java:20)
com.mvc.controller.RegisterServlet.doPost(RegisterServlet.java:34)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.8.5.4 logs.
The project will run but when it runs the RegisterServlet the code:
request.getRequestDispatcher(“/register.jsp”).
runs as it goes to the register file and not the Home.jsp file
Adrian,
If your registerUser() fun returns a success message then the request will be forwarded to /Home.jsp.
String userRegistered = registerDao.registerUser(registerBean);
You can put additional debug lines in DAO, DBConnection and check.
I dont have a Lib in the WEB-INF has
mysql-connector-java-5.0.2.jar
and servlet-api.jar
The registerUser Function may not be able to write to the databse
I think what is wrong is no Lib in the WEB-INF. In the Lib -mysql-connector-java-5.0.2.jar and servlet-api.jar-The files are not in my project and may cause the registerUser() function to fail.
What tool are you using ?
You can add dependency files in the build path as well.
NetBeans IDE 8.2
It would be easy for you if you download eclipse – a freeware.
For NetBeans, you can refer to the following link and try placing your Jars.
oopbook.com/java-classpath-2/classpath-in-netbeans/
The error is-
HTTP Status 500 –
type Exception report
message
description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
com.mvc.dao.RegisterDao.registerUser(RegisterDao.java:30)
com.mvc.controller.RegisterServlet.doPost(RegisterServlet.java:50)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.27 logs.
Its null pointer exception. The problem could be anything, like your bean objects or something else.
First copy whole code as is and try to execute.
Copying all the input parameters in to local variables
I have 7 input parameters in my project
parameter firstname-
String Firstname = request.getParameter(“firstname”);
registerBean.setFirstname(Firstname);
RegisterDao-
String Firstname = registerBean.getFirstname();
I have a table in my db called users
String query; //Insert user details into the table ‘USERS’
query = “insert into users(Firstname,Lastname,Address,Phone,Email,Username,Password) values (NULL,?,?,?,?,?,?,?)”;
preparedStatement.setString(1, Firstname);
RegisterBean-
private String Firstname;
public String getFirstname() {
return Firstname;
}
public void setFirstname(String Firstname) {
this.Firstname = Firstname;
}
Hi,
Your query is wrong.
insert into users(Firstname,Lastname,Address,Phone,Email,Username,Password) values (NULL,?,?,?,?,?,?,?)”;
Remove NULL. The number of columns and question marks should match
My query-Even if I make the table name different it will only go to the register.jsp file no error it wont read the query.
query = “insert into users(Firstname,Lastname,Address,Phone,Email,Username,Password) values (?,?,?,?,?,?,?)”;
Yes Adrian.
When registerUser return something other than SUCCESS it will go to Register.jsp only.
Could you please include extra debug statements in your DBConnection.java and RegisterDAO. Debug step by step.
First check if DBConnection is successful as you had some issues with JARs.
Best thing is use whole code as it is with same DB details. Once it is successful then you can modify this as per your needs.
Hi Adrian,
Were you able to find out your issue? What was the issue? If you could post the steps taken to resolve the problem, it may help someone here.
Hi ,
Am getting HTTP status 500-error instantiating server class
Exception
Javax.servlet.servletexception
Kindly help
Thanks in advance
Hi Sowmya,
Do maintain the proper directory structure to place the source code. You can refer to the image shown.
Do not put the src folder in the WEB-INF directory.
Let me know if your issue is resolved.
I want to know how to write same program using DAO aswell as Service layers… Can u suggest me how to do it ?
Pavitra,
Do you do not want to use Servlet ?
You can implement the same thing in a simple java class as weel.
using Servlet itself… I want to write it in different layers like registerDAO, registerDAOImpl, registerService, registerServiceImpl…
Pavitra,
I am not sure what logic do you want to write in registerService and RegisterServiceImpl.
If you extend the registration functionality to different level then you can achieve this.
Pls tell me how can i set up local database on my pc and can use it with eclipse.
Hi Asd,
You can install light weight database mysql.
Through console you can create database and tables.
Sir what u write in home.jsp not tell in video or in example
Sorry. The last Register.jsp should be renamed to Home.jsp. Everything else is fine. I have made this change already.
Once the registration is successful a success message is displayed on Home.jsp and the user is asked for log-in.
Let me know if this needs more explanation.
bro! I am getting this error.
HTTP Status 500 –
type Exception report
message
description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
com.mvc.dao.RegisterDao.registerUser(RegisterDao.java:20)
com.mvc.controller.RegisterServlet.doPost(RegisterServlet.java:34)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.8.5.4 logs.
Hi Sahil,
At what point did you get this error ?
Please cross verify the RegisterDao.java class and the method calling in your Servlet code.
Hi sir,
Could you please tell me how to retrieve values from database.my requirement is after registering i have to fetch that data from database and i have show that details in edit page.where admin can edit details and update the profile.please help me out
Thanks in advance sir.
Hi Vikas,
Please go through following program to display the data in tabular form.
https://krazytech.com/programs/java-program-to-display-arraylist-elements-in-jsp-using-iterator
you can just use select statement to display the records or even refer to RegisterDao.java class.
To enable edit mode for admin, you can try
Sir,
could you please post code for uploading multiple files using jsp and servlet .iam following MVC pattern.my problem is i have one upload resume field in register page and one more extra documents field for uploading multiple files like passport etc..please help me out i’m struggling fro 2 days.
Hi Vikas,
To upload multiple files using MVC would be difficult.
I think you have to use different input/submit buttons to upload multiple documents. You should upload each one individually.
sir,
my requirement is like resume upload in registration form.Admin can upload multiple documents in that field..could u send me any link related to that kind of requirement using jsp and servlet.
sir,
could you please explain the servlet flow through pictorial way.iam a beginner could you please help me out.
Hi Vikas,
I am here to help you.
If you go through first image, it explains most of the RegisterServlet flow and also if you refer to my comments within servlet code you will understand the flow.
Just to summarize the flow :
1. Assign all the inputs(user details) to local variables
2. Call Bean.java to set all the user details using java setters.
3. Next, go to DAO.java where you are just going to insert user details into the database.
4. Once it is successful, you are displaying successful message.
Even Java Login page is also similar to this. You can learn it in the following post.
https://krazytech.com/programs/a-login-application-in-java-using-model-view-controllermvc-design-pattern
Let me know if you have further doubts.
Thank you so much for helping me, this is one of the best blog for beginners like me.once again thanks a lot.
You are welcome Vikas.
Thanks for your comments.
i try to execute this code but i get “http 500 status error java.lang.NullPointerException ” what i can do to solve this problem … i hope to help me .. and thanks a lot
Hi Omer,
I am here to help you. Could you please tell me on what page you are getting this error.
Where did you try to execute this code ?
Are you able to see Register.jsp page ?
thanks ms.ravi i solved this problem .. the error it was in the mysql driver . it’s should be with web content file .. and thanks again for help :)
Omer, Nice to know that you are successfully able to execute this program.
You can also build Log in page in the similar manner. Here is the link if you are looking for.
https://krazytech.com/programs/java-programs/a-login-application-in-java-using-model-view-controllermvc-design-pattern
can yu tell me ..how yu solve the error
How did you resolve it?
Sir
This is very useful example for java mvc and registration page.
Thanks a lot.
You are always welcome here Anuja..:)
hi…even i’m getting problem in importing the files. i mean i’m getting errors in syntax.actually i’m doing my mini project for clg purpose on onlne music db.i’m getting problem in new registration so can you please help me with this??
Can you tell me what is the error?
Comments are closed.