回复留言的功能(JSP版)
jsp/reply.jsp
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@page import="java.util.*"%>
- <%@page import="net.java2000.notepad.*"%>
- <%@page import="net.java2000.notepad.service.*"%>
- <%@page import="net.java2000.notepad.service.impl.jdbc.*"%>
- <%@page import="net.java2000.tools.*"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Insert title here</title>
- </head>
- <body>
- <%
- // 检查登录的用户
- String usernameLogin = (String) session.getAttribute("USERNAME_LOGIN");
- if (usernameLogin == null) {
- %>
- <a href="register.jsp">注册用户</a>
- <%
- } else {
- %>
- 欢迎<%=usernameLogin%>,
- <a href="logout.jsp">退出登录</a>
- <%
- }
- %>
- <a href="index.jsp">留言列表</a>
- <%
- UserService us = new UserServiceJDBCImpl();
- PostService ps = new PostServiceJDBCImpl();
- request.setCharacterEncoding("UTF-8");
- long id = ParamUtils.getLongParameter(request, "id", -1);
- if (id <= 0) {
- out.print("请指定编号!");
- return;
- }
- Post post = ps.get(id);
- if (post == null) {
- out.print("指定的留言不存在!");
- return;
- }
- if (post.getIdParent() != 0) {
- out.print("指定的留言不是主题!");
- return;
- }
- if ("POST".equals(request.getMethod())) {
- if (usernameLogin == null) {
- // 检查用户名和密码
- String username = request.getParameter("username");
- String password = request.getParameter("password");
- if (username != null && password != null) {
- username = username.trim();
- password = password.trim();
- if (us.checkLogin(username, password)) {
- session.setAttribute("USERNAME_LOGIN", username);
- usernameLogin = username;
- } else {
- out.print("用户名/密码错误!");
- }
- }
- }
- if (usernameLogin != null) {
- Post p = new Post();
- p.setIdParent(post.getId());
- p.setUser(us.findByUsername(usernameLogin));
- p.setTitle(request.getParameter("title"));
- p.setContent(request.getParameter("content"));
- if (ps.save(p) != null) {
- out.print("增加回复成功");
- } else {
- out.print("增加回复失败");
- }
- }
- }
- %>
- <form method="POST"><input type="hidden" name="id"
- value="<%=id%>" />
- <table>
- <caption>回复留言</caption>
- <%
- if (usernameLogin == null) {
- %>
- <tr>
- <th>用户名</th>
- <td><input type="text" name="username" size="20" maxlength="20" /></td>
- </tr>
- <tr>
- <th>密码</th>
- <td><input type="password" name="password" size="20"
- maxlength="32" /></td>
- </tr>
- <%
- } else {
- %>
- <tr>
- <th>用户名</th>
- <td><%=usernameLogin%></td>
- </tr>
- <%
- }
- %>
- <tr>
- <th>标题</th>
- <td><input type="text" name="title" size="50" maxlength="100"
- value="Re:<%=post.getTitle()%>" /></td>
- </tr>
- <tr>
- <th>内容</th>
- <td><textarea name="content" cols="40" rows="10"></textarea></td>
- </tr>
- <tr>
- <td colspan="2"><input type="submit" value="增加回复" /></td>
- </tr>
- </table>
- </form>
- </body>
- </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="java.util.*"%> <%@page import="net.java2000.notepad.*"%> <%@page import="net.java2000.notepad.service.*"%> <%@page import="net.java2000.notepad.service.impl.jdbc.*"%> <%@page import="net.java2000.tools.*"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% // 检查登录的用户 String usernameLogin = (String) session.getAttribute("USERNAME_LOGIN"); if (usernameLogin == null) { %> <a href="register.jsp">注册用户</a> <% } else { %> 欢迎<%=usernameLogin%>, <a href="logout.jsp">退出登录</a> <% } %> <a href="index.jsp">留言列表</a> <% UserService us = new UserServiceJDBCImpl(); PostService ps = new PostServiceJDBCImpl(); request.setCharacterEncoding("UTF-8"); long id = ParamUtils.getLongParameter(request, "id", -1); if (id <= 0) { out.print("请指定编号!"); return; } Post post = ps.get(id); if (post == null) { out.print("指定的留言不存在!"); return; } if (post.getIdParent() != 0) { out.print("指定的留言不是主题!"); return; } if ("POST".equals(request.getMethod())) { if (usernameLogin == null) { // 检查用户名和密码 String username = request.getParameter("username"); String password = request.getParameter("password"); if (username != null && password != null) { username = username.trim(); password = password.trim(); if (us.checkLogin(username, password)) { session.setAttribute("USERNAME_LOGIN", username); usernameLogin = username; } else { out.print("用户名/密码错误!"); } } } if (usernameLogin != null) { Post p = new Post(); p.setIdParent(post.getId()); p.setUser(us.findByUsername(usernameLogin)); p.setTitle(request.getParameter("title")); p.setContent(request.getParameter("content")); if (ps.save(p) != null) { out.print("增加回复成功"); } else { out.print("增加回复失败"); } } } %> <form method="POST"><input type="hidden" name="id" value="<%=id%>" /> <table> <caption>回复留言</caption> <% if (usernameLogin == null) { %> <tr> <th>用户名</th> <td><input type="text" name="username" size="20" maxlength="20" /></td> </tr> <tr> <th>密码</th> <td><input type="password" name="password" size="20" maxlength="32" /></td> </tr> <% } else { %> <tr> <th>用户名</th> <td><%=usernameLogin%></td> </tr> <% } %> <tr> <th>标题</th> <td><input type="text" name="title" size="50" maxlength="100" value="Re:<%=post.getTitle()%>" /></td> </tr> <tr> <th>内容</th> <td><textarea name="content" cols="40" rows="10"></textarea></td> </tr> <tr> <td colspan="2"><input type="submit" value="增加回复" /></td> </tr> </table> </form> </body> </html>
转载于:https://blog.51cto.com/java0note/109975