Suggested articles for you:

101 thoughts on “Java Registration Form using Servlet MySQL MVC

  1. ZAIN Ali

    Hey!!
    Goood to see that you are replying every post.
    can you please tell me how to connect Databases with eclipse.
    which softwares do i need?

    Reply
    1. Ravi Bandakkanavar Post author

      Hi Zain

      Thank you for the comment.

      You can go through the following post to know how to connect to the DB server through Eclipse
      https://docs.oracle.com/cd/E14545_01/help/oracle.eclipse.tools.database.doc/html/gettingStarted/files/gettingStarted.html

      If you want to connect to any other database server simply use DBConnection.java class and provide the host, user details to connect to the database. You would need to change the driver details according to the database server and you would need JAR file as well.

      Reply
  2. Josue

    Hi Ravi,

    I’ve been trying to run this registration example. I have also imported the jar: servlet-api.jar and mysql-connector-java-5.1.20-bin.jar. Everything looks exactly the same but when I run I get the following error:

    java.lang.NullPointerException
    com.mvc.dao.RegisterDao.registerUser(RegisterDao.java:22)
    com.mvc.controller.RegisterServlet.doPost(RegisterServlet.java:34)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

    Reply
    1. Ravi Bandakkanavar Post author

      Hi Josue,

      When are you getting this error?
      Are you able to load Register.php through the local server?

      Hope you have the correct entries in web.xml.

      Reply
  3. siva

    good morning sir,
    sir,what is the use of bean class??? and without this bean class also project will run aa sir?????

    Reply
    1. Ravi Bandakkanavar Post author

      Good Morning Siva.

      JavaBeans are classes that encapsulate many objects into a single object. Bean class is used to hold multiple input data in a single object so that you can simply access user data with the help of object of the bean class. It just contains set and get functions. Initially, you will set all the input data and later on you will make use of the get method wherever you want to access the data.
      Ecplise IDE, automatically generates set and get methods based on the global variables defined in the class.

      Reply
  4. Shambu

    I created same type of Db and updated the same code, i am getting registration page but when i enterdata and submit it it throws an error as below..

    HTTP Status 500 – Error instantiating servlet class com.mvc.controller.RegisterServlet

    type Exception report

    message Error instantiating servlet class com.mvc.controller.RegisterServlet

    description The server encountered an internal error that prevented it from fulfilling this request.

    exception

    javax.servlet.ServletException: Error instantiating servlet class com.mvc.controller.RegisterServlet
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1115)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:745)

    root cause

    java.lang.Error: Unresolved compilation problem:
    The type java.io.ObjectInputStream cannot be resolved. It is indirectly referenced from required .class files

    Reply
      1. Shambu

        Hi Ravi, yes i have put the 2 jar files in lib folder and verified in build path as shown in ur screenshot. I have put mysql-connector-java.jar and servlet-api.jar these 2 jar files. I have a doubt i have mysql version 5.5.57 do i need to update mysql jar file of same version or any mysql jar file would be fine.

        Reply
        1. Ravi Bandakkanavar Post author

          Shambu,

          I think Jar version is fine.
          Have you placed xml file and made sure your servlet name and package name is matching there?

          Do add some debug statements in the beginning of the servlet and before DB connection. We will understand where exactly it is throwing an exception.

          I recommend you to follow the video to understand how it works. It helps you in debugging. https://youtu.be/dyme-pYvSio

        2. Shambu

          Hi Ravi, I deleted all the projects in eclipse and created a new project again. Now after entering the details in register.jsp and click on Register i am getting Oops.. Something went wrong there..! in the same page. I have created MYSQL DB with same as the screenshot. I want to debug it but where exactly i need to put the debug command is it in RegisterDao.java and what is the exact command for System.out.println.

        3. Shambu

          I am seeing error like this in console
          java.sql.SQLException: Column count doesn’t match value count at row 1
          at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)

        4. Ravi Bandakkanavar Post author

          Hi,

          Did you create the table with the given script?
          You can add System.out.println(" write something here"); statement in Servlet, DAO, and DBConnection classes

        5. Shambu

          I solved it, after correcting the SQL command, Thanks Ravi for your help.

        6. Ravi Bandakkanavar Post author

          Hi Shambu,

          It is easy. You will have to include following two tags in the web.xml for the LoginServlet.
          ….

          Let me know if you face any difficulty.

        7. Shambu

          I added below

          LoginServlet
          LoginServlet
          com.mvc.controller.LoginServlet

          LoginServlet
          /LoginServlet

          still getting 404 error resource not available. My register form works fine but when we hit credentials in login form and hit submit gives 404 error. What could be the problem and how to troubleshoot further.

        8. Ravi Bandakkanavar Post author

          Shambu,

          404 error occurs when you are trying to access a URL/page that is incorrect.

          You must be sending your requests to LoginServlet while logging in. You need to check the action element in the Login form. You can also add some debug statements in the LoginServlet to verify if the request is correctly being routed there.

      2. angel

        Hi sir,
        i have done the very same code,and received the below error,can you please help me regarding the same.
        HTTP Status 404 – /one/WEB-INF/Register.jsp

        type Status report

        message /one/WEB-INF/Register.jsp

        description The requested resource is not available.

        Apache Tomcat/7.0.47

        Reply
        1. Ravi Bandakkanavar Post author

          Hi,

          Did you choose the project type to be Dynamic web application?
          When are you getting this error?
          You need to open the Register.jsp via localhost.

  5. siva

    Good evening sir,
    Thank u sir for yesterday answer but i have another one mistake sir,

    SQL query:

    ALTER TABLE wipro4 CHANGE ID ID VARCHAR( 20 ) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL AUTO_INCREMENT

    MySQL said: Documentation

    #1063 – Incorrect column specifier for column ‘ID’

    what to do sir???

    Reply
  6. siva

    Good evening sir, If I am using text while creating table in mysql database in localhost whether we can use both int and varchar?????

    Reply
  7. siva

    Good evening sir, I have one doubt sir, What will happen if we didn’t import the servlet.api.jar file otherwise we can work without importing it ?

    Reply
    1. Ravi Bandakkanavar Post author

      Hi Siva,

      Very Good evening.
      The serlvet-api.jar contains necessary interfaces and classes that are used by the servlet. Few of them are ServletRequest, RequestDispatcher, ServletContext…

      Your servlet code would throw errors and you cannot compile Servlet class without serlvet-api.jar.

      Reply
  8. siva

    thank you soo much sir… the vedio is very useful thank u once again sir…

    Reply
  9. siva

    sir i am copying and just paste but it is coming like this..

    Reply
    1. Ravi Bandakkanavar Post author

      The comment section doesn’t support some tags. You need to put the code within tags.

      404 server code is returned when the page you are trying to invoke is not present.
      I suggest you that take all the code as it is and create database, table by following the scripts given. Place dependent jar files and run the application. You should be able to execute it without issues.
      Please refer to the video – https://youtu.be/dyme-pYvSio

      Reply
  10. siva

    index.jsp

    Insert title here

    Enter the ID
    Enter the NAME
    Enter the USERNAME
    Enter the PASSWORD
    Enter the DESIGNATION

    this only my program sir but it is showing 404 error please help me sir…….

    Reply
  11. siva

    though i changed the location in the server property and changed use tomcat installation it is showing 404 error please help me to overcome this problem sir…

    Reply
  12. siva

    HTTP Status 404 – /tim

    type Status report

    message /tim

    description The requested resource is not available.
    Apache Tomcat/7.0.37

    Reply
  13. siva

    It is really helpful thank u sir stay uploading more and clear our doubts sir please……..

    Reply
      1. Bob

        According to the 404 status, what was the solution? I’m having the same issue..

        Reply
        1. Ravi Bandakkanavar Post author

          Bob,

          404 error generally occurs when the servlet path specified in the action tag of the JSP is wrong or not matching with the name and path of the servlet where it resides.
          Please cross check this.
          Let me know if still not working.

  14. Sakthi

    Hi Ravi,

    I have mailed you regarding one query.

    Questions :

    1. End User is entering the values for the fields in designed form. When click on review button. I need a table which shows the data entered by the end user. once they are sure. click on submit to store the data in db.

    2. How to add a attachment field here in the registration form ?

    3. How to store the data value in database ?

    Please kindly help me on this questions. Thanks in advance !!!

    Reply
    1. Ravi Bandakkanavar Post author

      Hi Sakthi,

      1. Once the user submits his details, you can read all the data and display them in a tabular form on a JSP.
      2. You can look for the code to provide a facility to attach files
      3. storing data in DB is a simple task. Create tables and dump data into columns.

      Refer to the code –
      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();

      you can find this code in RegisterDao.java class.

      Reply
    1. Ravi Bandakkanavar Post author

      Sorry, Vishwa. It was missed.
      Let me add it and notify you.

      Meanwhile, as you are interested in Java, you may like to go through session based login application.

      krazytech.com/programs/session-role-based-java-login-example

      Reply
  15. adrianadmin0088

    No the code is not working. I deleted the username from the DBconnection file and it gave me an error. I think connects to the database. I copied all the code from your website into its own project and no errors in netbeans. Its giving me the 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:25)
    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)
    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.

    Apache Tomcat/8.0.27

    Reply
    1. adrianadmin0088

      I think whats wrong is in netbeans project it doesnt have JDBC Driver for MySQL. The DBconnection file has the jdbc. How do i add the mysql-connector-java.5.1.37.jar file in netbeans IDE 8.2

      Reply
      1. Ravi Bandakkanavar Post author

        Try the following if available.

        1. Go to project properties by right clicking on the project.
        2. Then click on Libraries tab, You will see Compile, Run, Compile Tests, Run Tests tabs.
        3. Click on Compile tab
        4. Click on Add JAR/Folder button at right

        5. Then browse and select the jar files or folder which you want to include. Included jar files or libraries will show on the following box of Compile tab.
        6. Click on OK button.
        7. Finished.

        Reply
    2. Ravi Bandakkanavar Post author

      In the RegisterDao.java, soon after the line
      try
      {
      con = DBConnection.createConnection();

      add the following line
      System.out.println(“Connection – “+con);

      Run your application from the beginning and let me know what you see in the console.

      Reply
  16. Adrian

    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.

    Reply
    1. Ravi Bandakkanavar Post author

      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.

      servlet>
      display -name RegisterServlet
      servlet -name RegisterServlet
      servlet -class com.mvc.controller.RegisterServlet

      Also, cross check following line in your Register.jsp

      input type=”submit” value=”Register”/>

      Reply
      1. Adrian

        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.

        Reply
        1. Adrian

          Is the correct code to add a different servlet to the web.xml file.

          RegisterServlet
          com.mvc.controller.RegisterServlet

          RegisterServlet
          /RegisterServlet

        2. Adrian

          I added the apropriate code and the program runs but it gives an message of —-Servlet RegisterServlet at /Project2017Maven—-

        3. Adrian

          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.

        4. Adrian

          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

        5. Ravi Bandakkanavar Post author

          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.

        6. Adrian

          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

      2. Adrian

        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.

        Reply
        1. Ravi Bandakkanavar Post author

          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/

        2. Adrian

          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.

        3. Ravi Bandakkanavar Post author

          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.

        4. Adrian

          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;
          }

        5. Ravi Bandakkanavar Post author

          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

      3. Adrian

        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 (?,?,?,?,?,?,?)”;

        Reply
        1. Ravi Bandakkanavar Post author

          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.

        2. Ravi Bandakkanavar Post author

          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.

  17. Sowmya

    Hi ,
    Am getting HTTP status 500-error instantiating server class

    Exception
    Javax.servlet.servletexception

    Kindly help
    Thanks in advance

    Reply
    1. Ravi Bandakkanavar Post author

      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.

      Reply
  18. Pavitra

    I want to know how to write same program using DAO aswell as Service layers… Can u suggest me how to do it ?

    Reply
      1. Pavitra

        using Servlet itself… I want to write it in different layers like registerDAO, registerDAOImpl, registerService, registerServiceImpl…

        Reply
        1. Ravi Bandakkanavar Post author

          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.

  19. asd

    Pls tell me how can i set up local database on my pc and can use it with eclipse.

    Reply
  20. sahil

    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.

    Reply
    1. Ravi Bandakkanavar Post author

      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.

      Reply
  21. vikas

    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.

    Reply
  22. vikas

    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.

    Reply
    1. Ravi Bandakkanavar Post author

      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.

      Reply
      1. vikas

        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.

        Reply
  23. vikas

    sir,
    could you please explain the servlet flow through pictorial way.iam a beginner could you please help me out.

    Reply
    1. Ravi Bandakkanavar Post author

      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.
      http://krazytech.com/programs/a-login-application-in-java-using-model-view-controllermvc-design-pattern

      Let me know if you have further doubts.

      Reply
      1. vikas

        Thank you so much for helping me, this is one of the best blog for beginners like me.once again thanks a lot.

        Reply
  24. omer

    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

    Reply
    1. Ravi Bandakkanavar Post author

      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 ?

      Reply
      1. omer

        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 :)

        Reply

Did it help? Comment here..