回复留言的功能(JSP版)

jsp/reply.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.   UserService us = new UserServiceJDBCImpl();   
  33.   PostService ps = new PostServiceJDBCImpl();   
  34.   request.setCharacterEncoding("UTF-8");   
  35.   long id = ParamUtils.getLongParameter(request, "id", -1);   
  36.   if (id <= 0) {   
  37.     out.print("请指定编号!");   
  38.     return;   
  39.   }   
  40.   Post post = ps.get(id);   
  41.   if (post == null) {   
  42.     out.print("指定的留言不存在!");   
  43.     return;   
  44.   }   
  45.   if (post.getIdParent() != 0) {   
  46.     out.print("指定的留言不是主题!");   
  47.     return;   
  48.   }   
  49.   
  50.   if ("POST".equals(request.getMethod())) {   
  51.     if (usernameLogin == null) {   
  52.       // 检查用户名和密码   
  53.       String username = request.getParameter("username");   
  54.       String password = request.getParameter("password");   
  55.       if (username != null && password != null) {   
  56.         username = username.trim();   
  57.         password = password.trim();   
  58.         if (us.checkLogin(username, password)) {   
  59.           session.setAttribute("USERNAME_LOGIN", username);   
  60.           usernameLogin = username;   
  61.         } else {   
  62.           out.print("用户名/密码错误!");   
  63.         }   
  64.       }   
  65.     }   
  66.     if (usernameLogin != null) {   
  67.       Post p = new Post();   
  68.       p.setIdParent(post.getId());   
  69.       p.setUser(us.findByUsername(usernameLogin));   
  70.       p.setTitle(request.getParameter("title"));   
  71.       p.setContent(request.getParameter("content"));   
  72.       if (ps.save(p) != null) {   
  73.         out.print("增加回复成功");   
  74.       } else {   
  75.         out.print("增加回复失败");   
  76.       }   
  77.     }   
  78.   }   
  79. %>   
  80. <form method="POST"><input type="hidden" name="id"  
  81.   value="<%=id%>" />   
  82. <table>   
  83.   <caption>回复留言</caption>   
  84.   <%   
  85.     if (usernameLogin == null) {   
  86.   %>   
  87.   <tr>   
  88.     <th>用户名</th>   
  89.     <td><input type="text" name="username" size="20" maxlength="20" /></td>   
  90.   </tr>   
  91.   <tr>   
  92.     <th>密码</th>   
  93.     <td><input type="password" name="password" size="20"  
  94.       maxlength="32" /></td>   
  95.   </tr>   
  96.   <%   
  97.     } else {   
  98.   %>   
  99.   <tr>   
  100.     <th>用户名</th>   
  101.     <td><%=usernameLogin%></td>   
  102.   </tr>   
  103.   <%   
  104.     }   
  105.   %>   
  106.   <tr>   
  107.     <th>标题</th>   
  108.     <td><input type="text" name="title" size="50" maxlength="100"  
  109.       value="Re:<%=post.getTitle()%>" /></td>   
  110.   </tr>   
  111.   <tr>   
  112.     <th>内容</th>   
  113.     <td><textarea name="content" cols="40" rows="10"></textarea></td>   
  114.   </tr>   
  115.   <tr>   
  116.     <td colspan="2"><input type="submit" value="增加回复" /></td>   
  117.   </tr>   
  118. </table>   
  119. </form>   
  120. </body>   
  121. </html>