增加留言的功能(JSP版)

jsp/add.jsp
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.   pageEncoding="UTF-8"%>   
  3. <%@page import="java.util.*"%>   
  4. <%@page import="net.java2000.notepad.*"%>   
  5. <%@page import="net.java2000.notepad.service.*"%>   
  6. <%@page import="net.java2000.notepad.service.impl.jdbc.*"%>   
  7. <%@page import="net.java2000.tools.*"%>   
  8. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">   
  9. <html>   
  10. <head>   
  11. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">   
  12. <title>Insert title here</title>   
  13. </head>   
  14. <body>   
  15. <%   
  16.   // 检查登录的用户   
  17.   String usernameLogin = (String) session.getAttribute("USERNAME_LOGIN");   
  18.   if (usernameLogin == null) {   
  19. %>   
  20.   
  21. <a href="register.jsp">注册用户</a>   
  22. <%   
  23.   } else {   
  24. %>   
  25. 欢迎<%=usernameLogin%>,   
  26. <a href="logout.jsp">退出登录</a>   
  27. <%   
  28.   }   
  29. %>   
  30. <a href="index.jsp">留言列表</a>   
  31. <%   
  32.   request.setCharacterEncoding("UTF-8");   
  33.   UserService us = new UserServiceJDBCImpl();   
  34.   PostService ps = new PostServiceJDBCImpl();   
  35.   if ("POST".equals(request.getMethod())) {   
  36.     if (usernameLogin == null) {   
  37.       // 检查用户名和密码   
  38.       String username = request.getParameter("username");   
  39.       String password = request.getParameter("password");   
  40.       if (username != null && password != null) {   
  41.         username = username.trim();   
  42.         password = password.trim();   
  43.         if (us.checkLogin(username, password)) {   
  44.           session.setAttribute("USERNAME_LOGIN", username);   
  45.           usernameLogin = username;   
  46.         } else {   
  47.           out.print("用户名/密码错误!");   
  48.         }   
  49.       }   
  50.     }   
  51.     if (usernameLogin != null) {   
  52.       Post p = new Post();   
  53.       p.setUser(us.findByUsername(usernameLogin));   
  54.       p.setTitle(request.getParameter("title"));   
  55.       p.setContent(request.getParameter("content"));   
  56.       if (ps.save(p) != null) {   
  57.         out.print("增加发言成功");   
  58.       } else {   
  59.         out.print("增加发言失败");   
  60.       }   
  61.     }   
  62.   }   
  63. %>   
  64. <form method="POST">   
  65. <table>   
  66.   <caption>增加留言</caption>   
  67.   <%   
  68.     if (usernameLogin == null) {   
  69.   %>   
  70.   <tr>   
  71.     <th>用户名</th>   
  72.     <td><input type="text" name="username" size="20" maxlength="20" /></td>   
  73.   </tr>   
  74.   <tr>   
  75.     <th>密码</th>   
  76.     <td><input type="password" name="password" size="20"  
  77.       maxlength="32" /></td>   
  78.   </tr>   
  79.   <%   
  80.     } else {   
  81.   %>   
  82.   <tr>   
  83.     <th>用户名</th>   
  84.     <td><%=usernameLogin%></td>   
  85.   </tr>   
  86.   <%   
  87.     }   
  88.   %>   
  89.   <tr>   
  90.     <th>标题</th>   
  91.     <td><input type="text" name="title" size="50" maxlength="100" /></td>   
  92.   </tr>   
  93.   <tr>   
  94.     <th>内容</th>   
  95.     <td><textarea name="content" cols="40" rows="10"></textarea></td>   
  96.   </tr>   
  97.   <tr>   
  98.     <td colspan="2"><input type="submit" value="增加留言" /></td>   
  99.   </tr>   
  100. </table>   
  101. </form>   
  102. </body>   
  103. </html>