Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Registration Example


Here, you will learn that how to create simple registration form in servlet. We are using oracle10g database. So you need to create a table first as given below:

  1. CREATE TABLE  “REGISTERUSER”
  2.    (    “NAME” VARCHAR2(4000),
  3.     “PASS” VARCHAR2(4000),
  4.     “EMAIL” VARCHAR2(4000),
  5.     “COUNTRY” VARCHAR2(4000)
  6.    )
  7. /

To create the registration page in servlet, we can separate the database logic from the servlet. But here, we are mixing the database logic in the servlet only for simplicity of the program. We will develop this page in JSP following DAO, DTO and Singleton design pattern later.

 

Example of Registration form in servlet

In this example, we have created the three pages.

  • register.html
  • Register.java
  • web.xml

register.html

In this page, we have getting input from the user using text fields and combobox. The information entered by the user is forwarded to Register servlet, which is responsible to store the data into the database.

  1. <html>
  2. <body>
  3. <form action=“servlet/Register” method=“post”>
  4. Name:<input type=“text” name=“userName”/><br/><br/>
  5. Password:<input type=“password” name=“userPass”/><br/><br/>
  6. Email Id:<input type=“text” name=“userEmail”/><br/><br/>
  7. Country:
  8. <select name=“userCountry”>
  9. <option>India</option>
  10. <option>Pakistan</option>
  11. <option>other</option>
  12. </select>
  13. <br/><br/>
  14. <input type=“submit” value=“register”/>
  15. </form>
  16. </body>
  17. </html>

Register.java

This servlet class receives all the data entered by user and stores it into the database. Here, we are performing the database logic. But you may separate it, which will be better for the web application.

  1. import java.io.*;
  2. import java.sql.*;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.*;
  5. public class Register extends HttpServlet {
  6. public void doPost(HttpServletRequest request, HttpServletResponse response)
  7.             throws ServletException, IOException {
  8. response.setContentType(“text/html”);
  9. PrintWriter out = response.getWriter();
  10. String n=request.getParameter(“userName”);
  11. String p=request.getParameter(“userPass”);
  12. String e=request.getParameter(“userEmail”);
  13. String c=request.getParameter(“userCountry”);
  14. try{
  15. Class.forName(“oracle.jdbc.driver.OracleDriver”);
  16. Connection con=DriverManager.getConnection(
  17. “jdbc:oracle:thin:@localhost:1521:xe”,“system”,“oracle”);
  18. PreparedStatement ps=con.prepareStatement(
  19. “insert into registeruser values(?,?,?,?)”);
  20. ps.setString(1,n);
  21. ps.setString(2,p);
  22. ps.setString(3,e);
  23. ps.setString(4,c);
  24. int i=ps.executeUpdate();
  25. if(i>0)
  26. out.print(“You are successfully registered…”);
  27. }catch (Exception e2) {System.out.println(e2);}
  28. out.close();
  29. }
  30. }

web.xml file

The is the configuration file, providing information about the servlet.

  1. <web-app>
  2. <servlet>
  3. <servlet-name>Register</servlet-name>
  4. <servlet-class>Register</servlet-class>
  5. </servlet>
  6. <servlet-mapping>
  7. <servlet-name>Register</servlet-name>
  8. <url-pattern>/servlet/Register</url-pattern>
  9. </servlet-mapping>
  10. <welcome-file-list>
  11.     <welcome-file>register.html</welcome-file>
  12. </welcome-file-list>
  13. </web-app>

download this example
download the war file
download this example (developed using Myeclipse IDE)
download this example (developed using Eclipse IDE)
download this example (developed using Netbeans IDE)

To connect java application with the Oracle database ojdbc14.jar file is required to be loaded. Put this jar file in WEB-INF/lib folder.

Comments

comments