CRUD in Servlet
CRUD in Servlet
A CRUD (Create, Read, Update and Delete) application is the most important application for
any project development. In Servlet, we can easily create CRUD application.
File: index.html
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <meta charset="ISO-8859-1">
5. <title>Insert title here</title>
6. </head>
7. <body>
8.
9. <h1>Add New Employee</h1>
10. <form action="SaveServlet" method="post">
11. <table>
12. <tr><td>Name:</td><td><input type="text" name="name"/></td></tr>
13. <tr><td>Password:</td><td><input type="password" name="password"/></td></tr>
14. <tr><td>Email:</td><td><input type="email" name="email"/></td></tr>
15. <tr><td>Country:</td><td>
16. <select name="country" style="width:150px">
17. <option>India</option>
18. <option>USA</option>
19. <option>UK</option>
20. <option>Other</option>
21. </select>
22. </td></tr>
23. <tr><td colspan="2"><input type="submit" value="Save Employee"/></td></tr>
24. </table>
25. </form>
26.
27. <br/>
28. <a href="ViewServlet">view employees</a>
29.
30. </body>
31. </html>
File: Emp.java
File: EmpDao.java
1. import java.util.*;
2. import java.sql.*;
3.
4. public class EmpDao {
5.
6. public static Connection getConnection(){
7. Connection con=null;
8. try{
9. Class.forName("oracle.jdbc.driver.OracleDriver");
10. con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system"
,"oracle");
11. }catch(Exception e){System.out.println(e);}
12. return con;
13. }
14. public static int save(Emp e){
15. int status=0;
16. try{
17. Connection con=EmpDao.getConnection();
18. PreparedStatement ps=con.prepareStatement(
19. "insert into user905(name,password,email,country) values (?,?,?,?)");
20. ps.setString(1,e.getName());
21. ps.setString(2,e.getPassword());
22. ps.setString(3,e.getEmail());
23. ps.setString(4,e.getCountry());
24.
25. status=ps.executeUpdate();
26.
27. con.close();
28. }catch(Exception ex){ex.printStackTrace();}
29.
30. return status;
31. }
32. public static int update(Emp e){
33. int status=0;
34. try{
35. Connection con=EmpDao.getConnection();
36. PreparedStatement ps=con.prepareStatement(
37. "update user905 set name=?,password=?,email=?,country=? where id=?"
);
38. ps.setString(1,e.getName());
39. ps.setString(2,e.getPassword());
40. ps.setString(3,e.getEmail());
41. ps.setString(4,e.getCountry());
42. ps.setInt(5,e.getId());
43.
44. status=ps.executeUpdate();
45.
46. con.close();
47. }catch(Exception ex){ex.printStackTrace();}
48.
49. return status;
50. }
51. public static int delete(int id){
52. int status=0;
53. try{
54. Connection con=EmpDao.getConnection();
55. PreparedStatement ps=con.prepareStatement("delete from user905 where id=?");
56. ps.setInt(1,id);
57. status=ps.executeUpdate();
58.
59. con.close();
60. }catch(Exception e){e.printStackTrace();}
61.
62. return status;
63. }
64. public static Emp getEmployeeById(int id){
65. Emp e=new Emp();
66.
67. try{
68. Connection con=EmpDao.getConnection();
69. PreparedStatement ps=con.prepareStatement("select * from user905 where id=?")
;
70. ps.setInt(1,id);
71. ResultSet rs=ps.executeQuery();
72. if(rs.next()){
73. e.setId(rs.getInt(1));
74. e.setName(rs.getString(2));
75. e.setPassword(rs.getString(3));
76. e.setEmail(rs.getString(4));
77. e.setCountry(rs.getString(5));
78. }
79. con.close();
80. }catch(Exception ex){ex.printStackTrace();}
81.
82. return e;
83. }
84. public static List<Emp> getAllEmployees(){
85. List<Emp> list=new ArrayList<Emp>();
86.
87. try{
88. Connection con=EmpDao.getConnection();
89. PreparedStatement ps=con.prepareStatement("select * from user905");
90. ResultSet rs=ps.executeQuery();
91. while(rs.next()){
92. Emp e=new Emp();
93. e.setId(rs.getInt(1));
94. e.setName(rs.getString(2));
95. e.setPassword(rs.getString(3));
96. e.setEmail(rs.getString(4));
97. e.setCountry(rs.getString(5));
98. list.add(e);
99. }
100. con.close();
101. }catch(Exception e){e.printStackTrace();}
102.
103. return list;
104. }
105. }
File: SaveServlet.java
1. import java.io.IOException;
2. import java.io.PrintWriter;
3.
4. import javax.servlet.ServletException;
5. import javax.servlet.annotation.WebServlet;
6. import javax.servlet.http.HttpServlet;
7. import javax.servlet.http.HttpServletRequest;
8. import javax.servlet.http.HttpServletResponse;
9. @WebServlet("/SaveServlet")
10. public class SaveServlet extends HttpServlet {
11. protected void doPost(HttpServletRequest request, HttpServletResponse response)
12. throws ServletException, IOException {
13. response.setContentType("text/html");
14. PrintWriter out=response.getWriter();
15.
16. String name=request.getParameter("name");
17. String password=request.getParameter("password");
18. String email=request.getParameter("email");
19. String country=request.getParameter("country");
20.
21. Emp e=new Emp();
22. e.setName(name);
23. e.setPassword(password);
24. e.setEmail(email);
25. e.setCountry(country);
26.
27. int status=EmpDao.save(e);
28. if(status>0){
29. out.print("<p>Record saved successfully!</p>");
30. request.getRequestDispatcher("index.html").include(request, response);
31. }else{
32. out.println("Sorry! unable to save record");
33. }
34.
35. out.close();
36. }
37.
38. }
File: EditServlet.java
1. import java.io.IOException;
2. import java.io.PrintWriter;
3.
4. import javax.servlet.ServletException;
5. import javax.servlet.annotation.WebServlet;
6. import javax.servlet.http.HttpServlet;
7. import javax.servlet.http.HttpServletRequest;
8. import javax.servlet.http.HttpServletResponse;
9. @WebServlet("/EditServlet")
10. public class EditServlet extends HttpServlet {
11. protected void doGet(HttpServletRequest request, HttpServletResponse response)
12. throws ServletException, IOException {
13. response.setContentType("text/html");
14. PrintWriter out=response.getWriter();
15. out.println("<h1>Update Employee</h1>");
16. String sid=request.getParameter("id");
17. int id=Integer.parseInt(sid);
18.
19. Emp e=EmpDao.getEmployeeById(id);
20.
21. out.print("<form action='EditServlet2' method='post'>");
22. out.print("<table>");
23. out.print("<tr><td></td><td><input type='hidden' name='id' value='"+e.getId()+"'
/></td></tr>");
24. out.print("<tr><td>Name:</td><td><input type='text' name='name' value='"+e.ge
tName()+"'/></td></tr>");
25. out.print("<tr><td>Password:</td><td><input type='password' name='password' v
alue='"+e.getPassword()+"'/>
26. </td></tr>");
27. out.print("<tr><td>Email:</td><td><input type='email' name='email' value='"+e.g
etEmail()+"'/></td></tr>");
28. out.print("<tr><td>Country:</td><td>");
29. out.print("<select name='country' style='width:150px'>");
30. out.print("<option>India</option>");
31. out.print("<option>USA</option>");
32. out.print("<option>UK</option>");
33. out.print("<option>Other</option>");
34. out.print("</select>");
35. out.print("</td></tr>");
36. out.print("<tr><td colspan='2'><input type='submit' value='Edit & Save '/></td></t
r>");
37. out.print("</table>");
38. out.print("</form>");
39.
40. out.close();
41. }
42. }
File: EditServlet2.java
1. import java.io.IOException;
2. import java.io.PrintWriter;
3.
4. import javax.servlet.ServletException;
5. import javax.servlet.annotation.WebServlet;
6. import javax.servlet.http.HttpServlet;
7. import javax.servlet.http.HttpServletRequest;
8. import javax.servlet.http.HttpServletResponse;
9. @WebServlet("/EditServlet2")
10. public class EditServlet2 extends HttpServlet {
11. protected void doPost(HttpServletRequest request, HttpServletResponse response)
12. throws ServletException, IOException {
13. response.setContentType("text/html");
14. PrintWriter out=response.getWriter();
15.
16. String sid=request.getParameter("id");
17. int id=Integer.parseInt(sid);
18. String name=request.getParameter("name");
19. String password=request.getParameter("password");
20. String email=request.getParameter("email");
21. String country=request.getParameter("country");
22.
23. Emp e=new Emp();
24. e.setId(id);
25. e.setName(name);
26. e.setPassword(password);
27. e.setEmail(email);
28. e.setCountry(country);
29.
30. int status=EmpDao.update(e);
31. if(status>0){
32. response.sendRedirect("ViewServlet");
33. }else{
34. out.println("Sorry! unable to update record");
35. }
36.
37. out.close();
38. }
39.
40. }
File: DeleteServlet.java
1. import java.io.IOException;
2. import javax.servlet.ServletException;
3. import javax.servlet.annotation.WebServlet;
4. import javax.servlet.http.HttpServlet;
5. import javax.servlet.http.HttpServletRequest;
6. import javax.servlet.http.HttpServletResponse;
7. @WebServlet("/DeleteServlet")
8. public class DeleteServlet extends HttpServlet {
9. protected void doGet(HttpServletRequest request, HttpServletResponse response)
10. throws ServletException, IOException {
11. String sid=request.getParameter("id");
12. int id=Integer.parseInt(sid);
13. EmpDao.delete(id);
14. response.sendRedirect("ViewServlet");
15. }
16. }
File: ViewServlet.java
1. import java.io.IOException;
2. import java.io.PrintWriter;
3. import java.util.List;
4.
5. import javax.servlet.ServletException;
6. import javax.servlet.annotation.WebServlet;
7. import javax.servlet.http.HttpServlet;
8. import javax.servlet.http.HttpServletRequest;
9. import javax.servlet.http.HttpServletResponse;
10. @WebServlet("/ViewServlet")
11. public class ViewServlet extends HttpServlet {
12. protected void doGet(HttpServletRequest request, HttpServletResponse response)
13. throws ServletException, IOException {
14. response.setContentType("text/html");
15. PrintWriter out=response.getWriter();
16. out.println("<a href='index.html'>Add New Employee</a>");
17. out.println("<h1>Employees List</h1>");
18.
19. List<Emp> list=EmpDao.getAllEmployees();
20.
21. out.print("<table border='1' width='100%'");
22. out.print("<tr><th>Id</th><th>Name</th><th>Password</th><th>Email</th><t
h>Country</th>
23. <th>Edit</th><th>Delete</th></tr>");
24. for(Emp e:list){
25. out.print("<tr><td>"+e.getId()+"</td><td>"+e.getName()+"</td><td>"+e.getPas
sword()+"</td>
26. <td>"+e.getEmail()+"</td><td>"+e.getCountry()+"</td><td><a href='EditS
ervlet?id="+e.getId()+"'>edit</a></td>
27. <td><a href='DeleteServlet?id="+e.getId()+"'>delete</a></td></tr>");
28. }
29. out.print("</table>");
30.
31. out.close();
32. }
33. }
Output
First page will look like this, fill the form and submit it.
You will get a message "Record successfully saved!".
Click on the View Employees link to see the total employees list.
Click on the update link, to change the data.
After changing information, submit button. You will see that information is changed.
Now, click on the delete link to delete the record.