Suggested read for you:

55 thoughts on “Java Registration Form using Servlet MySQL MVC

    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
  1. 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
  2. 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.

  3. 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
  4. 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.

  5. asd

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

    Reply
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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..