JSP复习笔记——第10章 连接数据库 之 jsp+DAO实现留言管理程序
案例-jsp+DAO实现留言管理程序
----------------Note.java------------------------
Java代码
1.package org.sky.darkness.note.vo ;
2.
3.public class Note {
4. private int id ;
5. private String title ;
6. private String author ;
7. private String content ;
8.
9. public void setId(int id) {
10. this.id = id ;
11. }
12. public void setTitle(String title) {
13. this.title = title ;
14. }
15. public void setAuthor(String author) {
16. this.author = author ;
17. }
18. public void setContent(String content) {
19. this.content = content ;
20. }
21.
22. public int getId() {
23. return this.id ;
24. }
25. public String getTitle() {
26. return this.title ;
27. }
28. public String getAuthor() {
29. return this.author ;
30. }
31. public String getContent() {
32. return this.content ;
33. }
34.};
package org.sky.darkness.note.vo ;
public class Note {
private int id ;
private String title ;
private String author ;
private String content ;
public void setId(int id) {
this.id = id ;
}
public void setTitle(String title) {
this.title = title ;
}
public void setAuthor(String author) {
this.author = author ;
}
public void setContent(String content) {
this.content = content ;
}
public int getId() {
return this.id ;
}
public String getTitle() {
return this.title ;
}
public String getAuthor() {
return this.author ;
}
public String getContent() {
return this.content ;
}
};
----------------------Person.java--------------------------
Java代码
1.package org.sky.darkness.note.vo ;
2.
3.public class Person
4.{
5. private String id ;
6. private String name ;
7. private String password ;
8.
9. public void setId(String id) {
10. this.id = id ;
11. }
12. public void setName(String name) {
13. this.name = name ;
14. }
15. public void setPassword(String password) {
16. this.password = password ;
17. }
18.
19. public String getId() {
20. return this.id ;
21. }
22. public String getName() {
23. return this.name ;
24. }
25. public String getPassword() {
26. return this.password ;
27. }
28.};
package org.sky.darkness.note.vo ;
public class Person
{
private String id ;
private String name ;
private String password ;
public void setId(String id) {
this.id = id ;
}
public void setName(String name) {
this.name = name ;
}
public void setPassword(String password) {
this.password = password ;
}
public String getId() {
return this.id ;
}
public String getName() {
return this.name ;
}
public String getPassword() {
return this.password ;
}
};
----------------NoteDAO.java-------------
Java代码
1.package org.sky.darkness.note.dao ;
2.
3.import java.util.* ;
4.import org.sky.darkness.note.vo.* ;
5.
6.public interface NoteDAO
7.{
8. // 增加操作
9. public void insert(Note note) throws Exception ;
10. // 修改操作
11. public void update(Note note) throws Exception ;
12. // 删除操作
13. public void delete(int id) throws Exception ;
14. // 按ID查询,主要为更新使用
15. public Note queryById(int id) throws Exception ;
16. // 查询全部
17. public List queryAll() throws Exception ;
18. // 模糊查询
19. public List queryByLike(String cond) throws Exception ;
20.};
package org.sky.darkness.note.dao ;
import java.util.* ;
import org.sky.darkness.note.vo.* ;
public interface NoteDAO
{
// 增加操作
public void insert(Note note) throws Exception ;
// 修改操作
public void update(Note note) throws Exception ;
// 删除操作
public void delete(int id) throws Exception ;
// 按ID查询,主要为更新使用
public Note queryById(int id) throws Exception ;
// 查询全部
public List queryAll() throws Exception ;
// 模糊查询
public List queryByLike(String cond) throws Exception ;
};
----------------------------PersonDAO.java-----------------------------------
Java代码
1.package org.sky.darkness.note.dao ;
2.
3.import org.sky.darkness.note.vo.* ;
4.
5.public interface PersonDAO {
6. // 做登陆验证
7. public boolean login(Person person) throws Exception ;
8.};
package org.sky.darkness.note.dao ;
import org.sky.darkness.note.vo.* ;
public interface PersonDAO {
// 做登陆验证
public boolean login(Person person) throws Exception ;
};
Java代码
1.---------------- PersonDAOImpl.java--------------
2.package org.sky.darkness.note.dao.impl ;
3.
4.import java.sql.* ;
5.import org.sky.darkness.note.vo.* ;
6.import org.sky.darkness.note.dbc.* ;
7.import org.sky.darkness.note.dao.* ;
8.
9.public class PersonDAOImpl implements PersonDAO
10.{
11. /*
12. 功能:
13. ? 判断是否是正确的用户名或密码
14. ? 从数据库中取出用户的真实姓名
15. */
16. public boolean login(Person person) throws Exception {
17. boolean flag = false ;
18. String sql = "SELECT name FROM person WHERE id=? and password=?" ;
19. PreparedStatement pstmt = null ;
20. DataBaseConnection dbc = null ;
21. dbc = new DataBaseConnection() ;
22. try {
23. pstmt = dbc.getConnection().prepareStatement(sql) ;
24. pstmt.setString(1,person.getId()) ;
25. pstmt.setString(2,person.getPassword()) ;
26. ResultSet rs = pstmt.executeQuery() ;
27. if (rs.next()) {
28. flag = true ;
29. person.setName(rs.getString(1)) ;
30. }
31. rs.close() ;
32. pstmt.close() ;
33. } catch (Exception e) {
34. throw new Exception("操作出现错误!!!") ;
35. } finally {
36. dbc.close() ;
37. }
38.
39. return flag ;
40. }
41.};
---------------- PersonDAOImpl.java--------------
package org.sky.darkness.note.dao.impl ;
import java.sql.* ;
import org.sky.darkness.note.vo.* ;
import org.sky.darkness.note.dbc.* ;
import org.sky.darkness.note.dao.* ;
public class PersonDAOImpl implements PersonDAO
{
/*
功能:
? 判断是否是正确的用户名或密码
? 从数据库中取出用户的真实姓名
*/
public boolean login(Person person) throws Exception {
boolean flag = false ;
String sql = "SELECT name FROM person WHERE id=? and password=?" ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
dbc = new DataBaseConnection() ;
try {
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(1,person.getId()) ;
pstmt.setString(2,person.getPassword()) ;
ResultSet rs = pstmt.executeQuery() ;
if (rs.next()) {
flag = true ;
person.setName(rs.getString(1)) ;
}
rs.close() ;
pstmt.close() ;
} catch (Exception e) {
throw new Exception("操作出现错误!!!") ;
} finally {
dbc.close() ;
}
return flag ;
}
};
------------- NoteDAOImpl.java-------------------------------------
Java代码
1.package org.sky.darkness.note.dao.impl ;
2.
3.import java.sql.* ;
4.import java.util.* ;
5.import org.sky.darkness.note.vo.* ;
6.import org.sky.darkness.note.dao.* ;
7.import org.sky.darkness.note.dbc.* ;
8.
9.public class NoteDAOImpl implements NoteDAO {
10. // 增加操作
11. public void insert(Note note) throws Exception {
12. String sql = "INSERT INTO note(id,title,author,content) VALUES(note_sequ.nextVal,?,?,?)" ;
13. PreparedStatement pstmt = null ;
14. DataBaseConnection dbc = null ;
15. dbc = new DataBaseConnection() ;
16. try {
17. pstmt = dbc.getConnection().prepareStatement(sql) ;
18. pstmt.setString(1,note.getTitle()) ;
19. pstmt.setString(2,note.getAuthor()) ;
20. pstmt.setString(3,note.getContent()) ;
21. pstmt.executeUpdate() ;
22. pstmt.close() ;
23. } catch (Exception e) {
24. // System.out.println(e) ;
25. throw new Exception("操作中出现错误!!!") ;
26. } finally {
27. dbc.close() ;
28. }
29. }
30. // 修改操作
31. public void update(Note note) throws Exception {
32. String sql = "UPDATE note SET title=?,author=?,content=? WHERE id=?" ;
33. PreparedStatement pstmt = null ;
34. DataBaseConnection dbc = null ;
35. dbc = new DataBaseConnection() ;
36. try {
37. pstmt = dbc.getConnection().prepareStatement(sql) ;
38. pstmt.setString(1,note.getTitle()) ;
39. pstmt.setString(2,note.getAuthor()) ;
40. pstmt.setString(3,note.getContent()) ;
41. pstmt.setInt(4,note.getId()) ;
42. pstmt.executeUpdate() ;
43. pstmt.close() ;
44. } catch (Exception e) {
45. throw new Exception("操作中出现错误!!!") ;
46. } finally {
47. dbc.close() ;
48. }
49. }
50. // 删除操作
51. public void delete(int id) throws Exception {
52. String sql = "DELETE FROM note WHERE id=?" ;
53. PreparedStatement pstmt = null ;
54. DataBaseConnection dbc = null ;
55. dbc = new DataBaseConnection() ;
56. try {
57. pstmt = dbc.getConnection().prepareStatement(sql) ;
58. pstmt.setInt(1,id) ;
59. pstmt.executeUpdate() ;
60. pstmt.close() ;
61. } catch (Exception e) {
62. throw new Exception("操作中出现错误!!!") ;
63. } finally {
64. dbc.close() ;
65. }
66. }
67. // 按ID查询,主要为更新使用
68. public Note queryById(int id) throws Exception {
69. Note note = null ;
70. String sql = "SELECT id,title,author,content FROM note WHERE id=?" ;
71. PreparedStatement pstmt = null ;
72. DataBaseConnection dbc = null ;
73. dbc = new DataBaseConnection() ;
74. try {
75. pstmt = dbc.getConnection().prepareStatement(sql) ;
76. pstmt.setInt(1,id) ;
77. ResultSet rs = pstmt.executeQuery() ;
78. if(rs.next()) {
79. note = new Note() ;
80. note.setId(rs.getInt(1)) ;
81. note.setTitle(rs.getString(2)) ;
82. note.setAuthor(rs.getString(3)) ;
83. note.setContent(rs.getString(4)) ;
84. }
85. rs.close() ;
86. pstmt.close() ;
87. } catch (Exception e) {
88. throw new Exception("操作中出现错误!!!") ;
89. } finally {
90. dbc.close() ;
91. }
92. return note ;
93. }
94. // 查询全部
95. public List queryAll() throws Exception {
96. List all = new ArrayList() ;
97. String sql = "SELECT id,title,author,content FROM note" ;
98. PreparedStatement pstmt = null ;
99. DataBaseConnection dbc = null ;
100. dbc = new DataBaseConnection() ;
101. try {
102. pstmt = dbc.getConnection().prepareStatement(sql) ;
103. ResultSet rs = pstmt.executeQuery() ;
104. while(rs.next()) {
105. Note note = new Note() ;
106. note.setId(rs.getInt(1)) ;
107. note.setTitle(rs.getString(2)) ;
108. note.setAuthor(rs.getString(3)) ;
109. note.setContent(rs.getString(4)) ;
110. all.add(note) ;
111. }
112. rs.close() ;
113. pstmt.close() ;
114. } catch (Exception e) {
115. System.out.println(e) ;
116. throw new Exception("操作中出现错误!!!") ;
117. } finally {
118. dbc.close() ;
119. }
120. return all ;
121. }
122. // 模糊查询
123. public List queryByLike(String cond) throws Exception {
124. List all = new ArrayList() ;
125. String sql = "SELECT id,title,author,content FROM note WHERE title LIKE ? or AUTHOR LIKE ? or CONTENT LIKE ?" ;
126. PreparedStatement pstmt = null ;
127. DataBaseConnection dbc = null ;
128. dbc = new DataBaseConnection() ;
129. try {
130. pstmt = dbc.getConnection().prepareStatement(sql) ;
131. pstmt.setString(1,"%"+cond+"%") ;
132. pstmt.setString(2,"%"+cond+"%") ;
133. pstmt.setString(3,"%"+cond+"%") ;
134. ResultSet rs = pstmt.executeQuery() ;
135. while(rs.next()) {
136. Note note = new Note() ;
137. note.setId(rs.getInt(1)) ;
138. note.setTitle(rs.getString(2)) ;
139. note.setAuthor(rs.getString(3)) ;
140. note.setContent(rs.getString(4)) ;
141. all.add(note) ;
142. }
143. rs.close() ;
144. pstmt.close() ;
145. } catch (Exception e) {
146. System.out.println(e) ;
147. throw new Exception("操作中出现错误!!!") ;
148. } finally {
149. dbc.close() ;
150. }
151. return all ;
152. }
153.};
package org.sky.darkness.note.dao.impl ;
import java.sql.* ;
import java.util.* ;
import org.sky.darkness.note.vo.* ;
import org.sky.darkness.note.dao.* ;
import org.sky.darkness.note.dbc.* ;
public class NoteDAOImpl implements NoteDAO {
// 增加操作
public void insert(Note note) throws Exception {
String sql = "INSERT INTO note(id,title,author,content) VALUES(note_sequ.nextVal,?,?,?)" ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
dbc = new DataBaseConnection() ;
try {
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(1,note.getTitle()) ;
pstmt.setString(2,note.getAuthor()) ;
pstmt.setString(3,note.getContent()) ;
pstmt.executeUpdate() ;
pstmt.close() ;
} catch (Exception e) {
// System.out.println(e) ;
throw new Exception("操作中出现错误!!!") ;
} finally {
dbc.close() ;
}
}
// 修改操作
public void update(Note note) throws Exception {
String sql = "UPDATE note SET title=?,author=?,content=? WHERE id=?" ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
dbc = new DataBaseConnection() ;
try {
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(1,note.getTitle()) ;
pstmt.setString(2,note.getAuthor()) ;
pstmt.setString(3,note.getContent()) ;
pstmt.setInt(4,note.getId()) ;
pstmt.executeUpdate() ;
pstmt.close() ;
} catch (Exception e) {
throw new Exception("操作中出现错误!!!") ;
} finally {
dbc.close() ;
}
}
// 删除操作
public void delete(int id) throws Exception {
String sql = "DELETE FROM note WHERE id=?" ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
dbc = new DataBaseConnection() ;
try {
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setInt(1,id) ;
pstmt.executeUpdate() ;
pstmt.close() ;
} catch (Exception e) {
throw new Exception("操作中出现错误!!!") ;
} finally {
dbc.close() ;
}
}
// 按ID查询,主要为更新使用
public Note queryById(int id) throws Exception {
Note note = null ;
String sql = "SELECT id,title,author,content FROM note WHERE id=?" ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
dbc = new DataBaseConnection() ;
try {
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setInt(1,id) ;
ResultSet rs = pstmt.executeQuery() ;
if(rs.next()) {
note = new Note() ;
note.setId(rs.getInt(1)) ;
note.setTitle(rs.getString(2)) ;
note.setAuthor(rs.getString(3)) ;
note.setContent(rs.getString(4)) ;
}
rs.close() ;
pstmt.close() ;
} catch (Exception e) {
throw new Exception("操作中出现错误!!!") ;
} finally {
dbc.close() ;
}
return note ;
}
// 查询全部
public List queryAll() throws Exception {
List all = new ArrayList() ;
String sql = "SELECT id,title,author,content FROM note" ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
dbc = new DataBaseConnection() ;
try {
pstmt = dbc.getConnection().prepareStatement(sql) ;
ResultSet rs = pstmt.executeQuery() ;
while(rs.next()) {
Note note = new Note() ;
note.setId(rs.getInt(1)) ;
note.setTitle(rs.getString(2)) ;
note.setAuthor(rs.getString(3)) ;
note.setContent(rs.getString(4)) ;
all.add(note) ;
}
rs.close() ;
pstmt.close() ;
} catch (Exception e) {
System.out.println(e) ;
throw new Exception("操作中出现错误!!!") ;
} finally {
dbc.close() ;
}
return all ;
}
// 模糊查询
public List queryByLike(String cond) throws Exception {
List all = new ArrayList() ;
String sql = "SELECT id,title,author,content FROM note WHERE title LIKE ? or AUTHOR LIKE ? or CONTENT LIKE ?" ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
dbc = new DataBaseConnection() ;
try {
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(1,"%"+cond+"%") ;
pstmt.setString(2,"%"+cond+"%") ;
pstmt.setString(3,"%"+cond+"%") ;
ResultSet rs = pstmt.executeQuery() ;
while(rs.next()) {
Note note = new Note() ;
note.setId(rs.getInt(1)) ;
note.setTitle(rs.getString(2)) ;
note.setAuthor(rs.getString(3)) ;
note.setContent(rs.getString(4)) ;
all.add(note) ;
}
rs.close() ;
pstmt.close() ;
} catch (Exception e) {
System.out.println(e) ;
throw new Exception("操作中出现错误!!!") ;
} finally {
dbc.close() ;
}
return all ;
}
};
----------- DAOFactory .java------------------------------------------------
Java代码
1.package org.sky.darkness.note.factory ;
2.
3.import org.sky.darkness.note.dao.* ;
4.import org.sky.darkness.note.dao.impl.* ;
5.
6.public class DAOFactory {
7. public static PersonDAO getPersonDAOInstance() {
8. return new PersonDAOImpl() ;
9. }
10.
11. public static NoteDAO getNoteDAOInstance() {
12. return new NoteDAOImpl() ;
13. }
14.};
package org.sky.darkness.note.factory ;
import org.sky.darkness.note.dao.* ;
import org.sky.darkness.note.dao.impl.* ;
public class DAOFactory {
public static PersonDAO getPersonDAOInstance() {
return new PersonDAOImpl() ;
}
public static NoteDAO getNoteDAOInstance() {
return new NoteDAOImpl() ;
}
};
----------- DataBaseConnection .java-----------------------------------------------
Java代码
1.package org.sky.darkness.note.dbc ;
2.
3.import java.sql.* ;
4.
5.public class DataBaseConnection {
6. private String DBDRIVER = "oracle.jdbc.driver.OracleDriver" ;
7. private String DBURL = "jdbc:oracle:thin:@localhost:1521:sky" ;
8. private String DBUSER = "scott" ;
9. private String DBPASSWORD = "darkness" ;
10. private Connection conn = null ;
11.
12. public DataBaseConnection() {
13. try {
14. Class.forName(DBDRIVER) ;
15. this.conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
16. } catch (Exception e) {
17. }
18. }
19.
20. public Connection getConnection() {
21. return this.conn ;
22. }
23.
24. public void close() {
25. try {
26. this.conn.close() ;
27. }
28. catch (Exception e) {
29. }
30. }
31.};
package org.sky.darkness.note.dbc ;
import java.sql.* ;
public class DataBaseConnection {
private String DBDRIVER= "oracle.jdbc.driver.OracleDriver" ;
private String DBURL= "jdbc:oracle:thin:@localhost:1521:sky" ;
private String DBUSER= "scott" ;
private String DBPASSWORD= "darkness" ;
private Connection conn= null ;
public DataBaseConnection() {
try {
Class.forName(DBDRIVER) ;
this.conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
} catch (Exception e) {
}
}
public Connection getConnection() {
return this.conn ;
}
public void close() {
try {
this.conn.close() ;
}
catch (Exception e) {
}
}
};
------------脚本.sql-------------------------------------------------
Java代码
1.-- 创建表
2.-- 用户表(登陆)、留言表
3.
4.-- 删除表
5.DROP TABLE person ;
6.DROP TABLE note ;
7.
8.-- 删除序列
9.DROP SEQUENCE note_sequ ;
10.
11.-- 创建序列
12.CREATE SEQUENCE note_sequ ;
13.
14.
15.-- 创建person表
16.CREATE TABLE person
17.(
18. id varchar(20) not null primary key ,
19. name varchar(20) ,
20. password varchar(20)
21.) ;
22.
23.-- 创建留言表
24.CREATE TABLE note
25.(
26. id int not null primary key , -- sequence
27. title varchar(20) not null ,
28. author varchar(20) not null ,
29. content varchar(50) not null
30.) ;
31.
32.-- 插入测试数据
33.INSERT INTO person VALUES ('darkness','wind','zzzzzz') ;
34.INSERT INTO person VALUES ('sky','cloud','mmmmmm') ;
35.
36.-- 事务提交
37.commit ;
-- 创建表
-- 用户表(登陆)、留言表
-- 删除表
DROP TABLE person ;
DROP TABLE note ;
-- 删除序列
DROP SEQUENCE note_sequ ;
-- 创建序列
CREATE SEQUENCE note_sequ ;
-- 创建person表
CREATE TABLE person
(
id varchar(20) not null primary key ,
name varchar(20) ,
password varchar(20)
) ;
-- 创建留言表
CREATE TABLE note
(
id int not null primary key , -- sequence
title varchar(20) not null ,
author varchar(20) not null ,
content varchar(50) not null
) ;
-- 插入测试数据
INSERT INTO person VALUES ('darkness','wind','zzzzzz') ;
INSERT INTO person VALUES ('sky','cloud','mmmmmm') ;
-- 事务提交
commit ;
Java代码
1.-------------------------login.jsp--------------------------------------------
2.
3.<%@ page contentType="text/html;charset=gb2312"%>
4.<html>
5.<head>
6. <title>JSP+DAO 留言管理程序——登陆</title>
7.</head>
8.<body>
9.<center>
10. <h1>留言管理范例 —— JSP + DAO实现</h1>
11. <hr>
12. <br>
13. <%
14. // 判断是否有错误信息,如果有则打印
15. // 如果没有此段代码,则显示时会直接打印null
16. if(request.getAttribute("err")!=null)
17. {
18. %>
19. <h2><%=request.getAttribute("err")%></h2>
20. <%
21. }
22. %>
23. <form action="login_conf.jsp" method="post">
24. <table width="80%">
25. <tr>
26. <td colspan="2">用户登陆</td>
27. </tr>
28. <tr>
29. <td>用户名:</td>
30. <td><input type="text" name="id"></td>
31. </tr>
32. <tr>
33. <td>密 码:</td>
34. <td><input type="password" name="password"></td>
35. </tr>
36. <tr>
37. <td colspan="2">
38. <input type="submit" value="登陆">
39. <input type="reset" value="重置">
40. </td>
41. </tr>
42. </table>
43. </form>
44.</center>
45.</body>
46.</html>
-------------------------login.jsp--------------------------------------------
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>JSP+DAO 留言管理程序——登陆</title>
</head>
<body>
<center>
<h1>留言管理范例 —— JSP + DAO实现</h1>
<hr>
<br>
<%
// 判断是否有错误信息,如果有则打印
// 如果没有此段代码,则显示时会直接打印null
if(request.getAttribute("err")!=null)
{
%>
<h2><%=request.getAttribute("err")%></h2>
<%
}
%>
<form action="login_conf.jsp" method="post">
<table width="80%">
<tr>
<td colspan="2">用户登陆</td>
</tr>
<tr>
<td>用户名:</td>
<td><input type="text" name="id"></td>
</tr>
<tr>
<td>密 码:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="登陆">
<input type="reset" value="重置">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
Java代码
1.--------------------------- login_conf.jsp------------------------------------
2.<%@ page contentType="text/html;charset=gb2312"%>
3.<%@ page import="org.sky.darkness.note.factory.*"%>
4.<html>
5.<head>
6. <title>JSP+DAO 留言管理程序——登陆</title>
7.</head>
8.<body>
9.<center>
10. <h1>留言管理范例 —— JSP + DAO实现</h1>
11. <hr>
12. <br>
13. <jsp:useBean id="person" scope="page" property="*"/>
15. <%
16. try
17. {
18. // 跳转
19. if(DAOFactory.getPersonDAOInstance().login(person))
20. {
21. // 设置用户姓名到session范围之中
22. session.setAttribute("uname",person.getName()) ;
23. // 用户合法
24. %>
25. <jsp:forward page="login_success.jsp"/>
26. <%
27. }
28. else
29. {
30. // 用户非法
31. %>
32. <jsp:forward page="login.jsp"/>
33. <%
34. }
35. }
36. catch(Exception e)
37. {}
38. %>
39.</center>
40.</body>
41.</html>
--------------------------- login_conf.jsp------------------------------------
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="org.sky.darkness.note.factory.*"%>
<html>
<head>
<title>JSP+DAO 留言管理程序——登陆</title>
</head>
<body>
<center>
<h1>留言管理范例 —— JSP + DAO实现</h1>
<hr>
<br>
<jsp:useBean id="person" scope="page" property="*"/>
<%
try
{
// 跳转
if(DAOFactory.getPersonDAOInstance().login(person))
{
// 设置用户姓名到session范围之中
session.setAttribute("uname",person.getName()) ;
// 用户合法
%>
<jsp:forward page="login_success.jsp"/>
<%
}
else
{
// 用户非法
%>
<jsp:forward page="login.jsp"/>
<%
}
}
catch(Exception e)
{}
%>
</center>
</body>
</html>
Java代码
1.------------------login_success.jsp------------------------------------------
2.<%@ page contentType="text/html;charset=gb2312"%>
3.<html>
4.<head>
5. <title>JSP+DAO 留言管理程序——登陆</title>
6.</head>
7.<body>
8.<center>
9. <h1>留言管理范例 —— JSP + DAO实现</h1>
10. <hr>
11. <br>
12. <%
13. if(session.getAttribute("uname")!=null)
14. {
15. // 用户已登陆
16. %>
17. <h2>登陆成功</h2>
18. <h2>欢迎<font color="red" size="12">
19. <%=session.getAttribute("uname")%>
20. </font>光临留言程序</h2>
21. <h3><a href="list_notes.jsp">进入留言管理页面</a></h3>
22. <%
23. }
24. else
25. {
26. // 用户未登陆,提示用户登陆,并跳转
27. response.setHeader("refresh","2;URL=login.jsp") ;
28. %>
29. 您还未登陆,请先登陆!!!<br>
30. 两秒后自动跳转到登陆窗口!!!<br>
31. 如果没有跳转,请按<a href="login.jsp">这里</a>!!!<br>
32. <%
33. }
34. %>
35.</center>
36.</body>
37.</html>
------------------login_success.jsp------------------------------------------
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>JSP+DAO 留言管理程序——登陆</title>
</head>
<body>
<center>
<h1>留言管理范例 —— JSP + DAO实现</h1>
<hr>
<br>
<%
if(session.getAttribute("uname")!=null)
{
// 用户已登陆
%>
<h2>登陆成功</h2>
<h2>欢迎<font color="red" size="12">
<%=session.getAttribute("uname")%>
</font>光临留言程序</h2>
<h3><a href="list_notes.jsp">进入留言管理页面</a></h3>
<%
}
else
{
// 用户未登陆,提示用户登陆,并跳转
response.setHeader("refresh","2;URL=login.jsp") ;
%>
您还未登陆,请先登陆!!!<br>
两秒后自动跳转到登陆窗口!!!<br>
如果没有跳转,请按<a href="login.jsp">这里</a>!!!<br>
<%
}
%>
</center>
</body>
</html>
Java代码
1.------------------------insert.jsp------------------------------
2.<%@ page contentType="text/html;charset=gb2312"%>
3.<html>
4.<head>
5. <title>JSP+DAO 留言管理程序——登陆</title>
6.</head>
7.<body>
8.<center>
9. <h1>留言管理范例 —— JSP + DAO实现</h1>
10. <hr>
11. <br>
12. <%
13. if(session.getAttribute("uname")!=null)
14. {
15. // 用户已登陆
16. %>
17. <form action="insert_do.jsp" method="post">
18. <table>
19. <tr>
20. <td colspan="2">添加新留言</td>
21. </tr>
22. <tr>
23. <td>标题:</td>
24. <td><input type="text" name="title"></td>
25. </tr>
26. <tr>
27. <td>作者:</td>
28. <td><input type="text" name="author"></td>
29. </tr>
30. <tr>
31. <td>内容:</td>
32. <td><textarea name="content" cols="30" rows="6"></textarea></td>
33. </tr>
34. <tr>
35. <td colspan="2">
36. <input type="submit" value="添加">
37. <input type="reset" value="重置">
38. </td>
39. </tr>
40. </table>
41. </form>
42. <h3><a href="list_notes.jsp">回到留言列表页</a></h3>
43. <%
44. }
45. else
46. {
47. // 用户未登陆,提示用户登陆,并跳转
48. response.setHeader("refresh","2;URL=login.jsp") ;
49. %>
50. 您还未登陆,请先登陆!!!<br>
51. 两秒后自动跳转到登陆窗口!!!<br>
52. 如果没有跳转,请按<a href="login.jsp">这里</a>!!!<br>
53. <%
54. }
55. %>
56.</center>
57.</body>
58.</html>
------------------------insert.jsp------------------------------
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>JSP+DAO 留言管理程序——登陆</title>
</head>
<body>
<center>
<h1>留言管理范例 —— JSP + DAO实现</h1>
<hr>
<br>
<%
if(session.getAttribute("uname")!=null)
{
// 用户已登陆
%>
<form action="insert_do.jsp" method="post">
<table>
<tr>
<td colspan="2">添加新留言</td>
</tr>
<tr>
<td>标题:</td>
<td><input type="text" name="title"></td>
</tr>
<tr>
<td>作者:</td>
<td><input type="text" name="author"></td>
</tr>
<tr>
<td>内容:</td>
<td><textarea name="content" cols="30" rows="6"></textarea></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="添加">
<input type="reset" value="重置">
</td>
</tr>
</table>
</form>
<h3><a href="list_notes.jsp">回到留言列表页</a></h3>
<%
}
else
{
// 用户未登陆,提示用户登陆,并跳转
response.setHeader("refresh","2;URL=login.jsp") ;
%>
您还未登陆,请先登陆!!!<br>
两秒后自动跳转到登陆窗口!!!<br>
如果没有跳转,请按<a href="login.jsp">这里</a>!!!<br>
<%
}
%>
</center>
</body>
</html>
Java代码
1.--------------------insert_do.jsp---------------------
2.<%@ page contentType="text/html;charset=gb2312"%>
3.<%@ page import="org.sky.darkness.note.factory.*"%>
4.<html>
5.<head>
6. <title>JSP+DAO 留言管理程序——登陆</title>
7.</head>
8.<body>
9.<center>
10. <h1>留言管理范例 —— JSP + DAO实现</h1>
11. <hr>
12. <br>
13. <%
14. // 进行乱码处理
15. request.setCharacterEncoding("GB2312") ;
16. %>
17. <%
18. if(session.getAttribute("uname")!=null)
19. {
20. // 用户已登陆
21. %>
22. <jsp:useBean id="note" scope="page" property="*"/>
24.
25. <%
26. response.setHeader("refresh","2;URL=list_notes.jsp") ;
27. try
28. {
29. DAOFactory.getNoteDAOInstance().insert(note) ;
30. %>
31. 留言添加成功,两秒后跳转到留言列表页!!!<br>
32. 如果没有跳转,请按<a href="list_notes.jsp">这里</a>!!!
33. <%
34. }
35. catch(Exception e)
36. {
37. %>
38. 留言添加失败,两秒后跳转到留言列表页!!!<br>
39. 如果没有跳转,请按<a href="list_notes.jsp">这里</a>!!!
40. <%
41. }
42. %>
43. <%
44. }
45. else
46. {
47. // 用户未登陆,提示用户登陆,并跳转
48. response.setHeader("refresh","2;URL=login.jsp") ;
49. %>
50. 您还未登陆,请先登陆!!!<br>
51. 两秒后自动跳转到登陆窗口!!!<br>
52. 如果没有跳转,请按<a href="login.jsp">这里</a>!!!<br>
53. <%
54. }
55. %>
56.</center>
57.</body>
58.</html>
--------------------insert_do.jsp---------------------
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="org.sky.darkness.note.factory.*"%>
<html>
<head>
<title>JSP+DAO 留言管理程序——登陆</title>
</head>
<body>
<center>
<h1>留言管理范例 —— JSP + DAO实现</h1>
<hr>
<br>
<%
// 进行乱码处理
request.setCharacterEncoding("GB2312") ;
%>
<%
if(session.getAttribute("uname")!=null)
{
// 用户已登陆
%>
<jsp:useBean id="note" scope="page" property="*"/>
<%
response.setHeader("refresh","2;URL=list_notes.jsp") ;
try
{
DAOFactory.getNoteDAOInstance().insert(note) ;
%>
留言添加成功,两秒后跳转到留言列表页!!!<br>
如果没有跳转,请按<a href="list_notes.jsp">这里</a>!!!
<%
}
catch(Exception e)
{
%>
留言添加失败,两秒后跳转到留言列表页!!!<br>
如果没有跳转,请按<a href="list_notes.jsp">这里</a>!!!
<%
}
%>
<%
}
else
{
// 用户未登陆,提示用户登陆,并跳转
response.setHeader("refresh","2;URL=login.jsp") ;
%>
您还未登陆,请先登陆!!!<br>
两秒后自动跳转到登陆窗口!!!<br>
如果没有跳转,请按<a href="login.jsp">这里</a>!!!<br>
<%
}
%>
</center>
</body>
</html>
Java代码
1.-------------------update.jsp-------------------------------------
2.<%@ page contentType="text/html;charset=gb2312"%>
3.<%@ page import="org.sky.darkness.note.factory.*"%>
4.<%@ page import="org.sky.darkness.note.vo.*"%>
5.<html>
6.<head>
7. <title>JSP+DAO 留言管理程序——登陆</title>
8.</head>
9.<body>
10.<center>
11. <h1>留言管理范例 —— JSP + DAO实现</h1>
12. <hr>
13. <br>
14. <%
15. // 进行乱码处理
16. request.setCharacterEncoding("GB2312") ;
17. %>
18. <%
19. if(session.getAttribute("uname")!=null)
20. {
21. // 用户已登陆
22. %>
23. <%
24. // 接收参数
25. int id = 0 ;
26. try
27. {
28. id = Integer.parseInt(request.getParameter("id")) ;
29. }
30. catch(Exception e)
31. {}
32. %>
33. <%
34. Note note = null ;
35. try
36. {
37. note = DAOFactory.getNoteDAOInstance().queryById(id) ;
38. }
39. catch(Exception e)
40. {}
41. %>
42. <%
43. if(note!=null)
44. {
45. id = note.getId() ;
46. String title = note.getTitle() ;
47. String author = note.getAuthor() ;
48. String content = note.getContent() ;
49. %>
50. <form action="update_do.jsp" method="post">
51. <table>
52. <tr>
53. <td colspan="2">添加新留言</td>
54. </tr>
55. <tr>
56. <td>标题:</td>
57. <td><input type="text" name="title" value="<%=title%>"></td>
58. </tr>
59. <tr>
60. <td>作者:</td>
61. <td><input type="text" name="author" value="<%=author%>"></td>
62. </tr>
63. <tr>
64. <td>内容:</td>
65. <td><textarea name="content" cols="30" rows="6"><%=content%></textarea></td>
66. </tr>
67. <tr>
68. <td colspan="2">
69. <input type="hidden" name="id" value="<%=id%>">
70. <input type="submit" value="更新">
71. <input type="reset" value="重置">
72. </td>
73. </tr>
74. </table>
75. </form>
76. <%
77. }
78. else
79. {
80. %>
81. 没有发现,要更新的内容!!<br>
82. 请确认要更新的留言是否存在!!<br>
83. <%
84. }
85. %>
86. <h3><a href="list_notes.jsp">回到留言列表页</a></h3>
87. <%
88. }
89. else
90. {
91. // 用户未登陆,提示用户登陆,并跳转
92. response.setHeader("refresh","2;URL=login.jsp") ;
93. %>
94. 您还未登陆,请先登陆!!!<br>
95. 两秒后自动跳转到登陆窗口!!!<br>
96. 如果没有跳转,请按<a href="login.jsp">这里</a>!!!<br>
97. <%
98. }
99. %>
100.</center>
101.</body>
102.</html>
-------------------update.jsp-------------------------------------
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="org.sky.darkness.note.factory.*"%>
<%@ page import="org.sky.darkness.note.vo.*"%>
<html>
<head>
<title>JSP+DAO 留言管理程序——登陆</title>
</head>
<body>
<center>
<h1>留言管理范例 —— JSP + DAO实现</h1>
<hr>
<br>
<%
// 进行乱码处理
request.setCharacterEncoding("GB2312") ;
%>
<%
if(session.getAttribute("uname")!=null)
{
// 用户已登陆
%>
<%
// 接收参数
int id = 0 ;
try
{
id = Integer.parseInt(request.getParameter("id")) ;
}
catch(Exception e)
{}
%>
<%
Note note = null ;
try
{
note = DAOFactory.getNoteDAOInstance().queryById(id) ;
}
catch(Exception e)
{}
%>
<%
if(note!=null)
{
id = note.getId() ;
String title = note.getTitle() ;
String author = note.getAuthor() ;
String content = note.getContent() ;
%>
<form action="update_do.jsp" method="post">
<table>
<tr>
<td colspan="2">添加新留言</td>
</tr>
<tr>
<td>标题:</td>
<td><input type="text" name="title" value="<%=title%>"></td>
</tr>
<tr>
<td>作者:</td>
<td><input type="text" name="author" value="<%=author%>"></td>
</tr>
<tr>
<td>内容:</td>
<td><textarea name="content" cols="30" rows="6"><%=content%></textarea></td>
</tr>
<tr>
<td colspan="2">
<input type="hidden" name="id" value="<%=id%>">
<input type="submit" value="更新">
<input type="reset" value="重置">
</td>
</tr>
</table>
</form>
<%
}
else
{
%>
没有发现,要更新的内容!!<br>
请确认要更新的留言是否存在!!<br>
<%
}
%>
<h3><a href="list_notes.jsp">回到留言列表页</a></h3>
<%
}
else
{
// 用户未登陆,提示用户登陆,并跳转
response.setHeader("refresh","2;URL=login.jsp") ;
%>
您还未登陆,请先登陆!!!<br>
两秒后自动跳转到登陆窗口!!!<br>
如果没有跳转,请按<a href="login.jsp">这里</a>!!!<br>
<%
}
%>
</center>
</body>
</html>
Java代码
1.-----------------------------update_do.jsp--------------------------------
2.<%@ page contentType="text/html;charset=gb2312"%>
3.<%@ page import="org.sky.darkness.note.factory.*"%>
4.<html>
5.<head>
6. <title>JSP+DAO 留言管理程序——登陆</title>
7.</head>
8.<body>
9.<center>
10. <h1>留言管理范例 —— JSP + DAO实现</h1>
11. <hr>
12. <br>
13. <%
14. // 进行乱码处理
15. request.setCharacterEncoding("GB2312") ;
16. %>
17. <jsp:useBean id="note" scope="page" property="*"/>
19. <%
20. if(session.getAttribute("uname")!=null)
21. {
22. // 用户已登陆
23. %>
24.
25. <%
26. response.setHeader("refresh","2;URL=list_notes.jsp") ;
27. try
28. {
29. DAOFactory.getNoteDAOInstance().update(note) ;
30. %>
31. 留言修改成功,两秒后跳转到留言列表页!!!<br>
32. 如果没有跳转,请按<a href="list_notes.jsp">这里</a>!!!
33. <%
34. }
35. catch(Exception e)
36. {
37. %>
38. 留言修改失败,两秒后跳转到留言列表页!!!<br>
39. 如果没有跳转,请按<a href="list_notes.jsp">这里</a>!!!
40. <%
41. }
42. %>
43. <%
44. }
45. else
46. {
47. // 用户未登陆,提示用户登陆,并跳转
48. response.setHeader("refresh","2;URL=login.jsp") ;
49. %>
50. 您还未登陆,请先登陆!!!<br>
51. 两秒后自动跳转到登陆窗口!!!<br>
52. 如果没有跳转,请按<a href="login.jsp">这里</a>!!!<br>
53. <%
54. }
55. %>
56.</center>
57.</body>
58.</html>
-----------------------------update_do.jsp--------------------------------
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="org.sky.darkness.note.factory.*"%>
<html>
<head>
<title>JSP+DAO 留言管理程序——登陆</title>
</head>
<body>
<center>
<h1>留言管理范例 —— JSP + DAO实现</h1>
<hr>
<br>
<%
// 进行乱码处理
request.setCharacterEncoding("GB2312") ;
%>
<jsp:useBean id="note" scope="page" property="*"/>
<%
if(session.getAttribute("uname")!=null)
{
// 用户已登陆
%>
<%
response.setHeader("refresh","2;URL=list_notes.jsp") ;
try
{
DAOFactory.getNoteDAOInstance().update(note) ;
%>
留言修改成功,两秒后跳转到留言列表页!!!<br>
如果没有跳转,请按<a href="list_notes.jsp">这里</a>!!!
<%
}
catch(Exception e)
{
%>
留言修改失败,两秒后跳转到留言列表页!!!<br>
如果没有跳转,请按<a href="list_notes.jsp">这里</a>!!!
<%
}
%>
<%
}
else
{
// 用户未登陆,提示用户登陆,并跳转
response.setHeader("refresh","2;URL=login.jsp") ;
%>
您还未登陆,请先登陆!!!<br>
两秒后自动跳转到登陆窗口!!!<br>
如果没有跳转,请按<a href="login.jsp">这里</a>!!!<br>
<%
}
%>
</center>
</body>
</html>
Java代码
1.-------------------------list_notes.jsp--------------------------
2.<%@ page contentType="text/html;charset=gb2312"%>
3.<%@ page import="java.util.*"%>
4.<%@ page import="org.sky.darkness.note.factory.*"%>
5.<%@ page import="org.sky.darkness.note.vo.*"%>
6.<html>
7.<head>
8. <title>JSP+DAO 留言管理程序——登陆</title>
9.</head>
10.<body>
11.<center>
12. <h1>留言管理范例 —— JSP + DAO实现</h1>
13. <hr>
14. <br>
15. <%
16. // 编码转换
17. request.setCharacterEncoding("GB2312") ;
18. if(session.getAttribute("uname")!=null)
19. {
20. // 用户已登陆
21. %>
22. <%
23. // 如果有内容,则修改变量i,如果没有,则根据i的值进行无内容提示
24. int i = 0 ;
25. String sql = null;
26. String keyword = request.getParameter("keyword") ;
27. List all = null ;
28. try
29. {
30. if(keyword==null)
31. {
32. all = DAOFactory.getNoteDAOInstance().queryAll() ;
33. }
34. else
35. {
36. // 有查询条件
37. all = DAOFactory.getNoteDAOInstance().queryByLike(keyword) ;
38. }
39. }
40. catch(Exception e)
41. {
42. System.out.println(e) ;
43. }
44. %>
45.<form action="list_notes.jsp" method="POST">
46. 请输入查询内容:<input type="text" name="keyword">
47. <input type="submit" value="查询">
48.</form>
49.</h3><a href="insert.jsp">添加新留言</a></h3>
50.<table width="80%" border="1">
51. <tr>
52. <td>留言ID</td>
53. <td>标题</td>
54. <td>作者</td>
55. <td>内容</td>
56. <td>删除</td>
57. </tr>
58. <%
59. Iterator iter = all.iterator() ;
60. while(iter.hasNext())
61. {
62. Note note = (Note)iter.next() ;
63. i++ ;
64. // 进行循环打印,打印出所有的内容,以表格形式
65. // 从数据库中取出内容
66. int id = note.getId() ;
67. String title = note.getTitle() ;
68. String author = note.getAuthor() ;
69. String content = note.getContent() ;
70.
71. if(keyword!=null)
72. {
73. // 需要将数据返红
74. title = title.replaceAll(keyword,"<font color="red">"+keyword+"</font>") ;
75. author = author.replaceAll(keyword,"<font color="red">"+keyword+"</font>") ;
76. content = content.replaceAll(keyword,"<font color="red">"+keyword+"</font>") ;
77. }
78. %>
79. <tr>
80. <td><%=id%></td>
81. <td><a href="update.jsp?id=<%=id%>"><%=title%></a></td>
82. <td><%=author%></td>
83. <td><%=content%></td>
84. <td><a href="delete_do.jsp?id=<%=id%>">删除</a></td>
85. </tr>
86. <%
87. }
88. // 判断i的值是否改变,如果改变,则表示有内容,反之,无内容
89. if(i==0)
90. {
91. // 进行提示
92. %>
93. <tr>
94. <td colspan="5">没有任何内容!!!</td>
95. </tr>
96. <%
97. }
98. %>
99.</table>
100.
101. <%
102. }
103. else
104. {
105. // 用户未登陆,提示用户登陆,并跳转
106. response.setHeader("refresh","2;URL=login.jsp") ;
107. %>
108. 您还未登陆,请先登陆!!!<br>
109. 两秒后自动跳转到登陆窗口!!!<br>
110. 如果没有跳转,请按<a href="login.jsp">这里</a>!!!<br>
111. <%
112. }
113. %>
114.</center>
115.</body>
116.</html>
-------------------------list_notes.jsp--------------------------
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.util.*"%>
<%@ page import="org.sky.darkness.note.factory.*"%>
<%@ page import="org.sky.darkness.note.vo.*"%>
<html>
<head>
<title>JSP+DAO 留言管理程序——登陆</title>
</head>
<body>
<center>
<h1>留言管理范例 —— JSP + DAO实现</h1>
<hr>
<br>
<%
// 编码转换
request.setCharacterEncoding("GB2312") ;
if(session.getAttribute("uname")!=null)
{
// 用户已登陆
%>
<%
// 如果有内容,则修改变量i,如果没有,则根据i的值进行无内容提示
int i = 0 ;
String sql = null;
String keyword = request.getParameter("keyword") ;
List all = null ;
try
{
if(keyword==null)
{
all = DAOFactory.getNoteDAOInstance().queryAll() ;
}
else
{
// 有查询条件
all = DAOFactory.getNoteDAOInstance().queryByLike(keyword) ;
}
}
catch(Exception e)
{
System.out.println(e) ;
}
%>
<form action="list_notes.jsp" method="POST">
请输入查询内容:<input type="text" name="keyword">
<input type="submit" value="查询">
</form>
</h3><a href="insert.jsp">添加新留言</a></h3>
<table width="80%" border="1">
<tr>
<td>留言ID</td>
<td>标题</td>
<td>作者</td>
<td>内容</td>
<td>删除</td>
</tr>
<%
Iterator iter = all.iterator() ;
while(iter.hasNext())
{
Note note = (Note)iter.next() ;
i++ ;
// 进行循环打印,打印出所有的内容,以表格形式
// 从数据库中取出内容
int id = note.getId() ;
String title = note.getTitle() ;
String author = note.getAuthor() ;
String content = note.getContent() ;
if(keyword!=null)
{
// 需要将数据返红
title = title.replaceAll(keyword,"<font color="red">"+keyword+"</font>") ;
author = author.replaceAll(keyword,"<font color="red">"+keyword+"</font>") ;
content = content.replaceAll(keyword,"<font color="red">"+keyword+"</font>") ;
}
%>
<tr>
<td><%=id%></td>
<td><a href="update.jsp?id=<%=id%>"><%=title%></a></td>
<td><%=author%></td>
<td><%=content%></td>
<td><a href="delete_do.jsp?id=<%=id%>">删除</a></td>
</tr>
<%
}
// 判断i的值是否改变,如果改变,则表示有内容,反之,无内容
if(i==0)
{
// 进行提示
%>
<tr>
<td colspan="5">没有任何内容!!!</td>
</tr>
<%
}
%>
</table>
<%
}
else
{
// 用户未登陆,提示用户登陆,并跳转
response.setHeader("refresh","2;URL=login.jsp") ;
%>
您还未登陆,请先登陆!!!<br>
两秒后自动跳转到登陆窗口!!!<br>
如果没有跳转,请按<a href="login.jsp">这里</a>!!!<br>
<%
}
%>
</center>
</body>
</html>
Java代码
1.------------------------delete_do.jsp-----------------------------------------
2.<%@ page contentType="text/html;charset=gb2312"%>
3.<%@ page import="org.sky.darkness.note.factory.*"%>
4.
5.<html>
6.<head>
7. <title>JSP+DAO 留言管理程序——登陆</title>
8.</head>
9.<body>
10.<center>
11. <h1>留言管理范例 —— JSP + DAO实现</h1>
12. <hr>
13. <br>
14. <%
15. if(session.getAttribute("uname")!=null)
16. {
17. // 用户已登陆
18. %>
19.
20. <%
21. // 接收参数
22. int id = 0 ;
23. try
24. {
25. id = Integer.parseInt(request.getParameter("id")) ;
26. }
27. catch(Exception e)
28. {}
29. %>
30. <%
31. response.setHeader("refresh","2;URL=list_notes.jsp") ;
32. try
33. {
34. DAOFactory.getNoteDAOInstance().delete(id) ;
35. %>
36. 留言删除成功,两秒后跳转到留言列表页!!!<br>
37. 如果没有跳转,请按<a href="list_notes.jsp">这里</a>!!!
38. <%
39. }
40. catch(Exception e)
41. {
42. %>
43. 留言删除失败,两秒后跳转到留言列表页!!!<br>
44. 如果没有跳转,请按<a href="list_notes.jsp">这里</a>!!!
45. <%
46. }
47. %>
48. <%
49. }
50. else
51. {
52. // 用户未登陆,提示用户登陆,并跳转
53. response.setHeader("refresh","2;URL=login.jsp") ;
54. %>
55. 您还未登陆,请先登陆!!!<br>
56. 两秒后自动跳转到登陆窗口!!!<br>
57. 如果没有跳转,请按<a href="login.jsp">这里</a>!!!<br>
58. <%
59. }
60. %>
61.</center>
62.</body>
63.</html>