☰ See All Chapters |
How to connect servlet to database - Example of registration form in servlet
In this tutorial we learn to connect servlet to MySQL database. This is an example of simple registration form in servlet.
Project Directory structure
Database script
CREATE TABLE CUSTOMER (NAME VARCHAR(30), PASSWORD VARCHAR(30), EMAIL VARCHAR(30), PHONE VARCHAR(10)); |
home.html
<html> <body bgcolor="AAAAA"> <table align="center" width=40% cellspacing="2" cellpadding="2" border="5"> <tr> <td colspan="2" align="center"><b>Welcome</b></td> </tr> <tr> <td align="center"><b> <a href="signin.html">SIGNIN</a></b></td> <td align="center"><b> <a href="signup.html">SIGNUP</a></b></td> </tr> </body> </html> |
signin.html
<html> <body bgcolor="yellow"> <h1>please enter your details</h1> <form action="../signin" method="post"> <table> <tr> <td>Enter your Name</td> <td><input type="text" name="nme"></td> </tr> <tr> <td>Enter your Password</td> <td><input type="password" name="pwd"></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="SIGNIN"></td> </tr> </table> </form> </body> </html> |
signup.html
<html> <body bgcolor="yellow"> <form action="../signup" method="post"> <h1>Please enter your details</h1> <table> <tr> <td>Enter your name</td> <td><input type="text" name="nme"></td> </tr> <tr> <td>Enter your password</td> <td><input type="password" name="pwd"></td> </tr> <tr> <td>Enter your email</td> <td><input type="text" name="email"></td> </tr> <tr> <td>Enter your phone</td> <td><input type="text" name="phone"></td> </tr> <tr> <td><input type="submit" value="SIGNUP"></td> </tr> </table> </form> </body> </html> |
SignupServlet.java
package com.java4coding;
import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException;
import javax.servlet.*; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
public class SignupServlet extends HttpServlet { public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter pw = res.getWriter();
String strNme = req.getParameter("nme"); String strPwd = req.getParameter("pwd"); String strEmail = req.getParameter("email"); String strPhone = req.getParameter("phone");
Connection con = null; PreparedStatement pst = null; try { // Class.forName("com.mysql.jdbc.Driver"); //for mysql versions lesser than 8.0 Class.forName("com.mysql.cj.jdbc.Driver");// for mysql version 8.0, this requires java 1.8 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/study", "root", "root"); pst = con.prepareStatement("INSERT INTO CUSTOMER(NAME, PASSWORD,EMAIL, PHONE) VALUES (?,?,?,?)");
pst.setString(1, strNme); pst.setString(2, strPwd); pst.setString(3, strEmail); pst.setString(4, strPhone);
int i = pst.executeUpdate();
if (i == 1) { pw.println("<html>"); pw.println("<body bgcolor='green'>"); pw.println("Hello Mr." + strNme + " Thank You for becoming a member"); pw.println("</body>"); pw.println("</html>"); } else { pw.println("<html>"); pw.println("<body bgcolor='red'>"); pw.println("Hello Mr." + strNme + " Better luck next time"); pw.println("</body>"); pw.println("</html>"); }
} catch (ClassNotFoundException e) { System.out.println("Exception caught " + e); e.printStackTrace(); } catch (SQLException e) { System.out.println("Exception caught " + e); e.printStackTrace(); } finally { try { if (pst != null) { pst.close(); } if (con != null) { con.close(); } } catch (SQLException e) { e.printStackTrace(); } } } } |
SigninServlet.java
package com.java4coding;
import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.servlet.*; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
public class SigninServlet extends HttpServlet { public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { boolean b = false; res.setContentType("text/html"); PrintWriter pw = res.getWriter(); String strNme = req.getParameter("nme"); String strPwd = req.getParameter("pwd");
Connection con = null; Statement st = null; ResultSet rs = null;
try {
// Class.forName("com.mysql.jdbc.Driver"); //for mysql versions lesser than 8.0 Class.forName("com.mysql.cj.jdbc.Driver");// for mysql version 8.0, this requires java 1.8 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/study", "root", "root"); st = con.createStatement(); rs = st.executeQuery("SELECT NAME,PASSWORD FROM CUSTOMER");
while (rs.next()) { if (strNme.equalsIgnoreCase(rs.getString("NAME")) && strPwd.equalsIgnoreCase(rs.getString("PASSWORD"))) { b = true; break; } }
if (b) { pw.println("<html>"); pw.println("<body bgcolor='green'>"); pw.println("Hello Mr " + strNme + " you are Welcome"); pw.println("</body>"); pw.println("</html>"); } else { pw.println("<html>"); pw.println("<body bgcolor='red'>"); pw.println("Hello Mr " + strNme + " you are invalid user"); pw.println("</body>"); pw.println("</html>"); }
} catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }finally { try { if (rs != null) { rs.close(); } if (st != null) { st.close(); } if (con != null) { con.close(); } } catch (SQLException e) { e.printStackTrace(); }
} } } |
web.xml
<web-app> <servlet> <servlet-name>Signup</servlet-name> <servlet-class>com.java4coding.SignupServlet</servlet-class> </servlet>
<servlet-mapping> <servlet-name>Signup</servlet-name> <url-pattern>/signup</url-pattern> </servlet-mapping>
<servlet> <servlet-name>Signin</servlet-name> <servlet-class>com.java4coding.SigninServlet</servlet-class> </servlet>
<servlet-mapping> <servlet-name>Signin</servlet-name> <url-pattern>/signin</url-pattern> </servlet-mapping>
</web-app |
Output
SIGNUP
When SIGNUP button is clicked
Enter the details and click on SIGNUP button
Verify the database whether data is inserted or not
SIGIN
When SIGIN button is clicked from home screen
Enter the details and click on SIGNIN
If the entered details are wrong
All Chapters