Example of Registration form in servlet
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:
- CREATE TABLE "REGISTERUSER"
- ( "NAME" VARCHAR2(4000),
- "PASS" VARCHAR2(4000),
- "EMAIL" VARCHAR2(4000),
- "COUNTRY" VARCHAR2(4000)
- )
- /
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.
- <html>
- <body>
- <form action="servlet/Register" method="post">
-
- Name:<input type="text" name="userName"/><br/><br/>
- Password:<input type="password" name="userPass"/><br/><br/>
- Email Id:<input type="text" name="userEmail"/><br/><br/>
- Country:
- <select name="userCountry">
- <option>India</option>
- <option>Pakistan</option>
- <option>other</option>
- </select>
-
- <br/><br/>
- <input type="submit" value="register"/>
-
- </form>
- </body>
- </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.
- import java.io.*;
- import java.sql.*;
- import javax.servlet.ServletException;
- import javax.servlet.http.*;
-
- public class Register extends HttpServlet {
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
- response.setContentType("text/html");
- PrintWriter out = response.getWriter();
-
- String n=request.getParameter("userName");
- String p=request.getParameter("userPass");
- String e=request.getParameter("userEmail");
- String c=request.getParameter("userCountry");
-
- try{
- Class.forName("oracle.jdbc.driver.OracleDriver");
- Connection con=DriverManager.getConnection(
- "jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
-
- PreparedStatement ps=con.prepareStatement(
- "insert into registeruser values(?,?,?,?)");
-
- ps.setString(1,n);
- ps.setString(2,p);
- ps.setString(3,e);
- ps.setString(4,c);
-
- int i=ps.executeUpdate();
- if(i>0)
- out.print("You are successfully registered...");
-
-
- }catch (Exception e2) {System.out.println(e2);}
-
- out.close();
- }
-
- }
web.xml file
The is the configuration file, providing information about the servlet.
- <web-app>
-
- <servlet>
- <servlet-name>Register</servlet-name>
- <servlet-class>Register</servlet-class>
- </servlet>
-
- <servlet-mapping>
- <servlet-name>Register</servlet-name>
- <url-pattern>/servlet/Register</url-pattern>
- </servlet-mapping>
-
- <welcome-file-list>
- <welcome-file>register.html</welcome-file>
- </welcome-file-list>
-
- </web-app>
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.
No comments:
Post a Comment