本示例是使用JavaWeb技术实现一个简单的文章管理系统(新闻管理系统)其中主要功能如下:
- 用户和管理员登录
- 用户发布新文章、文章详情查看、文章修改、文章删除与恢复
- 用户查看他人对自己授权的文章及其文章信息
- 用户将自己的文章对他人进行授权
- 管理员对普通用户新发布的文章进行审核和删除
- 管理员查看普通用户发布的所有文章及其详情
- 管理员发布新文章
使用的主要技术有:
JavaEE、JDBC、AJAX、JSP、JavaBean
本项目开发环境为:
- Intellij IDEA 2016.3
- Tomcat 8
- JDK 1.8
- MySQL 5.5
项目工程下载地址:文章管理系统http://download.csdn.net/detail/qq_24369113/9821508
下载完工程文件,按照下面的数据库结构截图建立好数据库的表,然后在 NewsRealeseDao.java中配置好相关的数据库URL、用户名和密码应该就可以直接运行起来了。
简单界面展示
登录界面
用户主要浏览界面
发布文章界面
管理员界面
为了能够让下载的工程文件直接运行这里再放上数据库的结构图:
简单代码介绍
为了节省篇幅这里主要介绍JSP中负责和Servlet或后台DAO有关的代码。
登录界面:
主要的就是form标签里的action属性,表示将表单里的内容提交给后台的checkLogin_user这个Servlet进行处理,其中input标签里的name属性标记其中的值,可以在Servlet中使用request.getparameter()方法得到标签中填入的值。
(还有一点需要注意的是action里的方法需要先在web.xml文件中进行注册,这样tomcat服务器才能正确的找到对应的类进行后续的处理,实际上所有的Servlet类都需要在web.xml中进行注册,所以后面就不再赘述这个问题了)
<form method="post" action="checkLogin_user">
<div class="panel">
<div class="panel-head"><strong>用户登录</strong></div>
<div class="panel-body" style="padding:30px;">
<div class="form-group">
<div class="field field-icon-right">
<input type="text" class="input" name="user" placeholder="Username"/>
<span class="icon icon-user"></span>
</div>
</div>
<div class="form-group">
<div class="field field-icon-right">
<input type="password" class="input" name="pass" placeholder="Password"/>
<span class="icon icon-key"></span>
</div>
</div>
</div>
<div class="panel-foot text-center">
<button class="button button-block bg-main text-big">登录</button>
</div>
</div>
</form>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
处理登录的Servlet:
处理JSP发送过来的数据,调用后台程序进行处理,并返回结果。
package servlet;
import dao.NewsRealeseDao;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class checkLogin_user extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
//这里的"user"和"pass"必须要和JSP里的对应的标签的name属性相同
String user=request.getParameter("user");
String pass=request.getParameter("pass");
NewsRealeseDao newsRealeseDao=new NewsRealeseDao();
try {
boolean checked=newsRealeseDao.ischecked(user,pass,"user");
//调用后台的Dao方法利用user表进行身份验证
if(checked)
{
HttpSession session=request.getSession();
session.setAttribute("username",user);//设置用户的姓名
response.sendRedirect("content_user.jsp");
}
else
{
response.sendRedirect("login_user.jsp");
}
}
catch (Exception ex)
{
Logger.getLogger(checkLogin.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description"+"public String getServletInfo() ";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
数据库访问类newsRealeseDao:
访问数据库并提供相关的方法。
import com.lut.beans.NewsRealese;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
public class NewsRealeseDao {
public static String driver = "com.mysql.jdbc.Driver";//定义驱动
public static String url = "jdbc:mysql://localhost:3306/myNews?useUnicode=true&characterEncoding=utf-8";//定义URL
public static String databseUser = "root";//定义用户名
public static String password = "root";//定义密码
private ArrayList getNews(Statement stat, String sql)//处理具体的新闻查询请求,返回所有结果
{
ArrayList newsRealese = new ArrayList();
try {
ResultSet rs = stat.executeQuery(sql);
while (rs.next()) { //实例化VO
NewsRealese news = new NewsRealese();
news.setNewsId(rs.getString("newsid"));
news.setContent(rs.getString("content"));
news.setHead(rs.getString("head"));
news.setIssueuser(rs.getString("issueuser"));
news.setPublish_time(rs.getString("publish_time"));
news.setNewstype(rs.getString("newstype"));
newsRealese.add(news);
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
return newsRealese;
}
}
public ArrayList UserQueryAllNews(String username, String table) throws Exception {//用户查看自己所有的文章
Connection conn = null;
ArrayList newsRealese = new ArrayList();
try {
//获取连接
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);
//运行SQL语句
String sql = "select * from " + table + " where issueuser='" + username + "' order by publish_time desc";
Statement stat = conn.createStatement();
newsRealese = getNews(stat, sql);
if (newsRealese.size() == 0) {
System.out.println("查询不到任何信息============");
return null;
}
stat.close();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//关闭连接
if (conn != null) {
conn.close();
}
} catch (Exception ex) {
}
return newsRealese;
}
}
public ArrayList UserQueryOthersNews(String username) throws Exception {//查看自己可见的其他人的所有的文章
Connection conn = null;
ArrayList rt = new ArrayList();
try {
//获取连接
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);
//运行SQL语句
String sql = "select * from news where newsId IN (select newsId from authority where username=?) order by publish_time desc";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, username);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
NewsRealese news = new NewsRealese();
news.setNewsId(rs.getString("newsid"));
news.setContent(rs.getString("content"));
news.setHead(rs.getString("head"));
news.setIssueuser(rs.getString("issueuser"));
news.setPublish_time(rs.getString("publish_time"));
news.setNewstype(rs.getString("newstype"));
rt.add(news);
}
if (rt.size() == 0) {
System.out.println("查询不到任何信息============Dao.UserQueryOthersNews");
return null;
}
conn.close();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//关闭连接
if (conn != null) {
conn.close();
}
} catch (Exception ex) {
}
return rt;
}
}
public ArrayList AdministorQueryAllNews(String username) throws Exception {//管理员查看所有的文章
Connection conn = null;
ArrayList newsRealese = new ArrayList();
try {
//获取连接
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);
//运行SQL语句
String sql = "select * from news order by publish_time desc";//获取
Statement stat = conn.createStatement();
newsRealese = getNews(stat, sql);
if (newsRealese.size() == 0) {
System.out.println("查询不到任何信息============");
return null;
}
stat.close();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//关闭连接
if (conn != null) {
conn.close();
}
} catch (Exception ex) {
}
return newsRealese;
}
}
//查询一个消息
public ArrayList AdministorQueryCheckPending() throws Exception {//查询所有的待审核的文章
Connection conn = null;
ArrayList newsRealese = new ArrayList();
try {
//获取连接
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);//不安全
//运行SQL语句
Statement stat = conn.createStatement();
String sql = "select * from check_pending order by publish_time desc";//获取newsid,使用?代替字符串,以免会发生错误
Statement st = conn.createStatement();
newsRealese = getNews(st, sql);
stat.close();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//关闭连接
if (conn != null) {
conn.close();
conn = null;
}
} catch (Exception ex) {
}
return newsRealese;
}
}
//用户删除数据
public String deleteOneNews(String newsid, String table) throws Exception {
Connection conn = null;
ArrayList newsRealese = new ArrayList();
try {
//获取连接
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);//不安全
//运行SQL语句
String sql_move = "insert into dustbin select * from news where newsId='" + newsid + "'";
String sql_delete = "DELETE FROM " + table + " WHERE newsId='" + newsid + "'";//获取newsid,使用?代替字符串,以免会发生错误
Statement ps = conn.createStatement();
int rs_move = ps.executeUpdate(sql_move);
if (rs_move != 0) {
int rs = ps.executeUpdate(sql_delete);
if (rs == 0)
System.out.println("删除失败==================NewsrealeaseDao");
} else {
System.out.println("插入到dustbin出错=============NewsrealeaseDao");
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//关闭连接
if (conn != null) {
conn.close();
}
} catch (Exception ex) {
}
return newsRealese.toString();
}
}
public String AdmindeleteCheck_pendingNews(String newsid) throws Exception {
Connection conn = null;
int rs = 0;
try {
//获取连接
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);//不安全
//运行SQL语句
String sql_delete = "DELETE FROM check_pending WHERE newsId='" + newsid + "'";//获取newsid,使用?代替字符串,以免会发生错误
Statement ps = conn.createStatement();
rs = ps.executeUpdate(sql_delete);
if (rs == 0) {
System.out.println("删除失败==================NewsrealeaseDao");
return null;
}
else
return "成功删除"+rs;
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//关闭连接
if (conn != null) {
conn.close();
}
} catch (Exception ex) {
}
}
return null;
}
public int AuthorizeOneNews(ArrayList<String> userlist, String newsid) {
int count = 0;
Connection conn = null;
try {
//获取连接
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);//不安全
//运行SQL语句
Statement stat = conn.createStatement();
for (int i = 0; i < userlist.size(); i++) {
String sql = "insert into authority VALUES(?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, newsid);
ps.setString(2, userlist.get(i));
System.out.println(ps.toString());
count += ps.executeUpdate();
}
System.out.println("成功添加" + count + "行");
stat.close();
conn.close();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//关闭连接
if (conn != null) {
conn.close();
}
} catch (Exception ex) {
}
return count;
}
}
//插入数据
public int insertOneNews(HashMap<String, String> addnews_list, String table) throws Exception {//插入一个新的新闻
Connection conn = null;
try {
//获取连接
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);//不安全
//运行SQL语句
Statement stat = conn.createStatement();
String sql = "insert into " + table + " VALUES(?,?,?,?,?,?)";//获取newsid,使用?代替字符串,以免会发生错误
PreparedStatement ps = conn.prepareStatement(sql);
// ps.setString(1, table);
ps.setString(1, addnews_list.get("newsid"));
ps.setString(2, addnews_list.get("head"));
ps.setString(3, addnews_list.get("content"));
ps.setString(4, addnews_list.get("publish_time"));
ps.setString(5, addnews_list.get("issueuser"));
ps.setString(6, addnews_list.get("newstype"));
System.out.println(addnews_list.get("newstype") + "===============" + ps.toString());
int i = ps.executeUpdate();
System.out.println("成功添加" + i + "行");
stat.close();
conn.close();
return i;
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//关闭连接
if (conn != null) {
conn.close();
conn = null;
}
} catch (Exception ex) {
}
}
return 0;
}
public HashMap<String, String> queryOneNews(String newsid, String table) throws SQLException {
Connection connection = null;
HashMap<String, String> rt = new HashMap<>();
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, databseUser, password);
String sql = "select * from " + table + " where newsId='" + newsid + "'";
Statement ps = connection.createStatement();
System.out.println("将要执行的=====" + sql);
ResultSet rs = ps.executeQuery(sql);
while (rs.next()) {
rt.put("newsid", rs.getString("newsId"));
rt.put("head", rs.getString("head"));
rt.put("content", rs.getString("content"));
rt.put("time", rs.getString("publish_time"));
rt.put("author", rs.getString("issueuser"));
rt.put("newstype", rs.getString("newstype"));
return rt;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
connection.close();
}
return rt;
}
//更新数据
public int updateOneNews(HashMap<String, String> addnews_list) throws Exception {
Connection conn = null;
try {
//获取连接
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);//不安全
//获取newsid,使用?代替字符串,以免会发生错误
String sql = "UPDATE news set head=?,content=?,publish_time=?,issueuser=?,newstype=? where newsId=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(6, addnews_list.get("newsid"));
ps.setString(1, addnews_list.get("head"));
ps.setString(2, addnews_list.get("content"));
ps.setString(3, addnews_list.get("publish_time"));
ps.setString(4, addnews_list.get("issueuser"));
ps.setString(5, addnews_list.get("newstype"));
System.out.println(ps.toString());
int i = ps.executeUpdate();
System.out.println("成功更新" + i + "行");
conn.close();
return i;
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//关闭连接
if (conn != null) {
conn.close();
conn = null;
}
} catch (Exception ex) {
}
}
return 0;
}
public boolean CheckNews(String newsid)//审核通过一片文章
{
String sql_insert = "insert into news select * from check_pending where newsId=?";
String sql_delete = "delete from check_pending where newsId=?";
Connection con = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url, databseUser, password);
PreparedStatement ps = con.prepareStatement(sql_insert);
ps.setString(1, newsid);
int result = ps.executeUpdate();
if (result != 0) {
ps = con.prepareStatement(sql_delete);
ps.setString(1, newsid);
int r = ps.executeUpdate();
if (r != 0)
return true;
else
return false;
} else
return false;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
public boolean ResumeNews(String newsid)//审核通过一片文章
{
String sql_insert = "insert into news select * from dustbin where newsId=?";
String sql_delete = "delete from dustbin where newsId=?";
Connection con = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url, databseUser, password);
PreparedStatement ps = con.prepareStatement(sql_insert);
ps.setString(1, newsid);
int result = ps.executeUpdate();
if (result != 0) {
ps = con.prepareStatement(sql_delete);
ps.setString(1, newsid);
int r = ps.executeUpdate();
if (r != 0)
return true;
else
return false;
} else
return false;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
public boolean ischecked(String user, String pass, String table) throws Exception {//登录验证 table表示从哪个表中查询
Connection conn = null;
//获取连接
Class.forName(driver);
String sql = "select password from " + table + " where username='" + user + "'";
System.out.println(sql);
try {
conn = DriverManager.getConnection(url, databseUser, password);//不安全
System.out.println("建立database连接");
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
while (rs.next()) {
if (rs.getString("password").equals(pass))
return true;
else
return false;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return false;
}
private static void show(ArrayList in) {
for (int i = 0; i < in.size(); i++) {
NewsRealese tem = (NewsRealese) in.get(i);
System.out.println(tem.getHead() + "======" + tem.getContent() + "@@@" + tem.getIssueuser() + "=====" + tem.getPublish_time());
}
}
public static void main(String arg[]) {
HashMap<String, String> addnews_list = new HashMap<>();
addnews_list.put("newsid", "1234567892");
addnews_list.put("head", "第2条测试");
addnews_list.put("content", "这个是第一条测试文章,里面的内容都是随便乱写的~亏大发老师教辅拉世纪东方拉开圣诞节福利卡士大夫拉克丝打开发送大六块腹肌");
addnews_list.put("publish_time", "2017-4-4/12:12:12");
addnews_list.put("issueuser", "木子勇士心");
addnews_list.put("newstype", "1");
NewsRealeseDao nd = new NewsRealeseDao();
ArrayList rs = nd.AdministorQueryCheckPending(); ///查询所有的待审核订单
show(rs);
} catch (Exception e) {
e.printStackTrace();
}
// nd.CheckNews("1234567890"); //审核一个订单
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372
- 373
- 374
- 375
- 376
- 377
- 378
- 379
- 380
- 381
- 382
- 383
- 384
- 385
- 386
- 387
- 388
- 389
- 390
- 391
- 392
- 393
- 394
- 395
- 396
- 397
- 398
- 399
- 400
- 401
- 402
- 403
- 404
- 405
- 406
- 407
- 408
- 409
- 410
- 411
- 412
- 413
- 414
- 415
- 416
- 417
- 418
- 419
- 420
- 421
- 422
- 423
- 424
- 425
- 426
- 427
- 428
- 429
- 430
- 431
- 432
- 433
- 434
- 435
- 436
- 437
- 438
- 439
- 440
- 441
- 442
- 443
- 444
- 445
- 446
- 447
- 448
- 449
- 450
- 451
- 452
- 453
- 454
- 455
- 456
- 457
- 458
- 459
- 460
- 461
- 462
- 463
- 464
- 465
- 466
- 467
- 468
- 469
- 470
- 471
- 472
- 473
- 474
- 475
- 476
- 477
- 478
- 479
- 480
- 481
- 482
- 483
- 484
- 485
- 486
- 487
- 488
- 489
- 490
- 491
- 492
- 493
- 494
- 495
- 496
- 497
- 498
- 499
- 500
- 501
- 502
- 503
- 504
- 505
- 506
- 507
- 508
- 509
- 510
- 511
- 512
- 513
- 514
- 515
- 516
- 517
- 518
- 519
- 520
- 521
- 522
- 523
- 524
- 525
- 526
- 527
- 528
- 529
- 530
- 531
- 532
- 533
- 534
- 535
- 536
- 537
- 538
- 539
- 540
- 541
- 542
- 543
- 544
- 545
- 546
- 547
- 548
- 549
- 550
- 551
- 552
- 553
- 554
- 555
- 556
- 557
- 558
- 559
- 560
- 561
- 562
web.xml 配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>checkLogin</servlet-name>
<servlet-class>servlet.checkLogin</servlet-class>
</servlet>
<servlet>
<servlet-name>checkLogin_user</servlet-name>
<servlet-class>servlet.checkLogin_user</servlet-class>
</servlet>
<servlet>
<servlet-name>checkNews</servlet-name>
<servlet-class>servlet.checkNews</servlet-class>
</servlet>
<servlet>
<servlet-name>InsertOneNews</servlet-name>
<servlet-class>servlet.InsertOneNews</servlet-class>
</servlet>
<servlet>
<servlet-name>DeleteOneNews</servlet-name>
<servlet-class>servlet.DeleteOneNews</servlet-class>
</servlet>
<servlet>
<servlet-name>UpdateOneNews</servlet-name>
<servlet-class>servlet.UpdateOneNews</servlet-class>
</servlet>
<servlet>
<servlet-name>QueryOneNews</servlet-name>
<servlet-class>servlet.QueryOneNews</servlet-class>
</servlet>
<servlet>
<servlet-name>ShowAllNews</servlet-name>
<servlet-class>servlet.ShowAllNews</servlet-class>
</servlet>
<servlet>
<servlet-name>QueryOneNews_user</servlet-name>
<servlet-class>servlet.QueryOneNews_user</servlet-class>
</servlet>
<servlet>
<servlet-name>Authorize</servlet-name>
<servlet-class>servlet.Authorize</servlet-class>
</servlet>
<servlet>
<servlet-name>ResumeNews</servlet-name>
<servlet-class>servlet.ResumeNews</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Authorize</servlet-name>
<url-pattern>/Authorize</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ShowAllNews</servlet-name>
<url-pattern>/ShowAllNews</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>checkLogin</servlet-name>
<url-pattern>/checkLogin</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>InsertOneNews</servlet-name>
<url-pattern>/InsertOneNews</url-pattern>
</servlet-mapping>
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>com.lutsoft.filter.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet-mapping>
<servlet-name>DeleteOneNews</servlet-name>
<url-pattern>/DeleteOneNews</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UpdateOneNews</servlet-name>
<url-pattern>/UpdateOneNews</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>QueryOneNews</servlet-name>
<url-pattern>/QueryOneNews</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>checkNews</servlet-name>
<url-pattern>/checkNews</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>checkLogin_user</servlet-name>
<url-pattern>/checkLogin_user</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>QueryOneNews_user</servlet-name>
<url-pattern>/QueryOneNews_user</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ResumeNews</servlet-name>
<url-pattern>/ResumeNews</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
下面简单介绍一下如何查看每个人的所有文章
实际上就是直接调用NewsRealeseDao.java中的方法来来直接查询数据库中的记录,然后将结果放到一个ArrayList中直接返回,然后在JSP页面上显示出来。
(查看别人已授权的文章方法类似,只是查询的时候增加一个条件而已)
JSP页面:
<table class="table table-hover">
<tr>
<th width="45">选择</th>
<th width="300">标题</th>
<th width="100">时间</th>
<th width="100">类别</th>
<th width="150">操作</th>
</tr>
<% String username = (String) session.getAttribute("username");
NewsRealeseDao newsRealeseDao = new NewsRealeseDao();
ArrayList newsRealese = newsRealeseDao.UserQueryAllNews(username,"news");
%>
<%
for (int i = 0; i < newsRealese.size(); i++) {
NewsRealese tem = (NewsRealese) newsRealese.get(i);
%>
<tr>
<td>
<input type="checkbox" name="id" value="<%=i%>>"/>
</td>
<td><%=tem.getHead()%>
</td>
<td><%=tem.getPublish_time()%>
</td>
<td><%=tem.getNewstype()%>
</td>
<td>
<a class="button border-green button-little" href="#"
onclick="queryInfo('<%=tem.getNewsId()%>','news')">详情</a>
<a class="button border-blue button-little" href="#"
onclick="queryInfo('<%=tem.getNewsId()%>','news')">修改</a>
<a class="button border-red button-little" href="#"
onclick="{if(confirm('确认删除?')){javascrtpt:window.location.href = 'DeleteOneNews?function=user_delete&destination=content_user.jsp&table=news&newsid=<%=tem.getNewsId()%>'}return false;}">删除</a>
</td>
</tr>
<%
}
%>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
接下来介绍一下Ajax实现的异步传输:
上面的JSP页面最后有三种button,每种button里面有一个onclick方法,该方法就是使用JavaScript实现的方法,然后使用Ajax技术将数据传送给后台的Servlet。
query.js是普通用户和管理员公用的所以里面还有一个checkpass方法用于管理员审核文章。其代码如下:
/**
* Created by 32706 on 2017/4/5.
*/
var xmlHttp=false;
function createXMLHttpRequest()
{
if (window.ActiveXObject) //在IE浏览器中创建XMLHttpRequest对象
{
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e){
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(ee){
xmlHttp=false;
}
}
}
else if (window.XMLHttpRequest) //在非IE浏览器中创建XMLHttpRequest对象
{
try{
xmlHttp = new XMLHttpRequest();
}
catch(e){
xmlHttp=false;
}
}
}
function queryInfo( id,table)
{
id=id.toString();
createXMLHttpRequest(); //调用创建XMLHttpRequest对象的方法
xmlHttp.onreadystatechange=callback; //设置回调函数
//向QueryOneNews这个Servlet查询从表table中的编号为id的文章的详细信息
var url="QueryOneNews?table="+table+"&newsid="+id;
alert("查询编号为:"+id+"的文章详情?");
xmlHttp.open("post",url,true); //向服务器端发送请求
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf8");
xmlHttp.send(null);
function callback()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
var data= xmlHttp.responseText;
//分离每个字段的值,得到字段的属性值
var parameters=data.split("||");
var id=parameters[0]
var head=parameters[1];
var author=parameters[2];
var time=parameters[3];
var type=parameters[4]
var content=parameters[5];
//设置html中标签里的值
document.getElementById("check_id").value=id; document.getElementById("check_head").value=head; document.getElementById("check_author").value=author; document.getElementById("check_time").value=time; document.getElementById("check_type").value=type; document.getElementById("check_content").value=content;
}
}
}
}
function checkpass() {//审核通过
createXMLHttpRequest(); //调用创建XMLHttpRequest对象的方法
xmlHttp.onreadystatechange=callback; //设置回调函数
var newsid=document.getElementById("check_id").value;
var url="checkNews?newsid="+newsid;
xmlHttp.open("post",url,true); //向服务器端发送请求
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf8");
xmlHttp.send(null);
function callback()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
alert("审核通过,已完成");
location.reload();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
由于篇幅有限这里就只是介绍一些局部的原理,具体的实现细节可以下载源工程文件进行查看。而且我也是在一边学习一遍写博客,所以不免会出现很多错误,也请大家多多包涵不吝赐教。
这里做一下总结:
一、 前台JSP调用Servlet的三种方法(可能不完整):
1.在form标签里指定action字段,然后按钮type设置为submit,这样就能在点击按钮的时候自动提交form里的所有数据到Servlet。通常用在登录等表单的提交。
示例:
<form method="post" action="checkLogin_user">
- 1
2.在button等标签里的onclick属性里设置需要调用的Servlet,通常适用于简单的页面跳转或者数据提交。实际上也是调用的JavaScript的方法。
示例:
<a class="button border-red button-little" href="#" onclick="javascrtpt:window.location.href = 'DeleteOneNews?function=user_delete&destination=content_user.jsp&table=news&newsid=<%=tem.getNewsId()%>'">删除</a>
- 1
3.使用JavaScript的函数来处理,典型的就是项目中需要用到的Ajax,使用JavaScript来获得页面中的标签的值然后传输给Servelt进行处理,并将结果显示在原先的页面上。
示例:
var xmlHttp=false;
function createXMLHttpRequest()
{
if (window.ActiveXObject) //在IE浏览器中创建XMLHttpRequest对象
{
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e){
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(ee){
xmlHttp=false;
}
}
}
else if (window.XMLHttpRequest) //在非IE浏览器中创建XMLHttpRequest对象
{
try{
xmlHttp = new XMLHttpRequest();
}
catch(e){
xmlHttp=false;
}
}
}
function queryInfo( id,table)
{
id=id.toString();
createXMLHttpRequest(); //调用创建XMLHttpRequest对象的方法
xmlHttp.onreadystatechange=callback; //设置回调函数
var url="QueryOneNews?table="+table+"&newsid="+id;
alert("查询编号为:"+id+"的文章详情?");
xmlHttp.open("post",url,true); //向服务器端发送请求
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf8");
xmlHttp.send(null);
function callback()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
var data= xmlHttp.responseText;
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
4.直接使用“< a >”标签中的href属性指定所要访问的Servlet。不过该方法好像只能实现get方式。
示例:
<a href="Authorization_user?id=<%=tem.getId()%>" class="icon-file">文章授权</a>
- 1
当数据传输给Servlet后,Servlet调用后台的方法就和普通的Java编程没有多大区别了,所以这里就不再赘述了。如有不足请多多包涵~~