Beruflich Dokumente
Kultur Dokumente
CRUD APPLICATION
First, create a database and table for User using the following SQL
scripts:
1 CREATE TABLE `users` (
2 `uname` varchar(10) NOT NULL,
3 `password` varchar(10) NOT NULL,
4 `email` varchar(50) default NULL,
5 `registeredon` date default NULL,
6 PRIMARY KEY (`uname`),
UNIQUE KEY `email` (`email`)
7 ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
8
index.jsp
1
2 <%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
3 <html>
4 <head>
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
6 <title>CRUD Example</title>
7 </head>
<body>
8 <jsp:forward page="/UserController?action=listuser" />
9 </body>
10 </html>
11
user.jsp
1 <%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
2 <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
3 <!DOCTYPE html>
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Add new user</title>
7 </head>
8 <body>
9 <form method="POST" action='UserController' name="frmAddUser">
10 <% String action = request.getParameter("action");
11 System.out.println(action);
%>
12 <% if (action.equalsIgnoreCase("edit")) {%>
13 User Name : <input type="text" name="uname"
14 value="<c:out value="${user.uname}" />" readonly="re
15 <%} else {%>
User Name : <input type="text" name="uname"
16 value="<c:out value="${user.uname}" />" /> <br />
17
18
19
20
<%}%>
21 Password : <input
22 type="password" name="pass"
23 value="<c:out value="${user.password}" />" /> <br />
24 Email : <input
type="text" name="email"
25 value="<c:out value="${user.email}" />" /> <br />
26
27 <% if (action.equalsIgnoreCase("edit")) {%>
28 Registration : <input
29 type="text" name="dob"
30 value="<fmt:formatDate pattern="yyyy/MM/dd" value="${user.registere
<%} else {%>
31 Registration : <input
32 type="text" name="dob"
33 value="<fmt:formatDate pattern="yyyy/MM/dd" value="${user.registere
34 <%}%>
35 <input type="submit" value="Submit" />
36 </form>
</body>
37 </html>
38
39
40
41
listuser.jsp
1 <%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
2 <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
3 <!DOCTYPE html>
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Show All Users</title>
7 </head>
8 <body>
9 <table border=1>
10 <thead>
11 <tr>
<th>User Name</th>
12 <th>Email</th>
13 <th>Registration Date</th>
14 <th colspan=2>Action</th>
15 </tr>
</thead>
16 <tbody>
17 <c:forEach items="${users}" var="user">
18 <tr>
19 <td><c:out value="${user.uname}" /></td>
20 <td><c:out value="${user.email}" /></td>
<td><fmt:formatDate pattern="dd MMM,yyyy" value="${user.register
21
22
23
24 <td><a href="UserController?action=edit&userId=<c:out value="${
25 <td><a href="UserController?action=delete&userId=<c:out value="
26 </tr>
</c:forEach>
27 </tbody>
28 </table>
29 <p><a href="UserController?action=insert">Add User</a></p>
30 </body>
31 </html>
32
33
34