This example implements a simple article management system (news management system) using Java Web technology. Its main functions are as follows:
- User and Administrator Logon
- Users publish new articles, view details of articles, modify articles, delete and restore articles
- Users view articles authorized by others and their information
- Users authorize their articles to others
- Administrators review and delete new articles published by ordinary users
- Administrators view all articles published by ordinary users and their details
- Administrators Publish New Articles
The main technologies used are:
JavaEE,JDBC,AJAX,JSP,JavaBean
The development environment of this project is as follows:
- Intellij IDEA 2016.3
- Tomcat 8
- JDK 1.8
- MySQL 5.5
Project Engineering Download Address: The article management system http://download.csdn.net/detail/qq_24369113/9821508
After downloading the project file, create the database table according to the following database structure screenshot, and then configure the relevant database URL, user name and password in NewsRealeseDao.java, it should be able to run directly.
Simple interface display
Login interface
User's main browsing interface
Publishing Interface
Administrator Interface
In order to make the downloaded engineering files run directly, the structure diagram of the database is put here.
Simple Code Introduction
In order to save space, this article mainly introduces the code responsible for Servlet or background DAO in JSP.
Login interface:
The main thing is the action attribute in the form tag, which means submitting the contents of the form to the background checkLogin_user, a Servlet for processing. The name attribute in the input tag marks the value in the Servlet, and the value in the tag can be obtained by using the request. get parameter () method in the Servlet.
(It is also important to note that the method in action needs to be registered in the web.xml file first, so that the tomcat server can correctly find the corresponding classes for subsequent processing. In fact, all the Servlet classes need to be registered in web.xml, so this problem will not be repeated later.)
<form method="post" action="checkLogin_user">
<div class="panel">
<div class="panel-head"><strong>User login</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">Sign in</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 handling login:
Processing the data sent by JSP, calling the background program for processing, and returning the results.
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");
//Here "user" and "pass" must have the same name attribute as the corresponding tag in the JSP
String user=request.getParameter("user");
String pass=request.getParameter("pass");
NewsRealeseDao newsRealeseDao=new NewsRealeseDao();
try {
boolean checked=newsRealeseDao.ischecked(user,pass,"user");
//Call the Dao method in the background to authenticate using the user table
if(checked)
{
HttpSession session=request.getSession();
session.setAttribute("username",user);//Set the user's name
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
The database access class newsRealeseDao:
Access the database and provide relevant methods.
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";//Definition driven
public static String url = "jdbc:mysql://localhost:3306/myNews?useUnicode=true&characterEncoding=utf-8";//Define URL
public static String databseUser = "root";//Define user names
public static String password = "root";//Define password
private ArrayList getNews(Statement stat, String sql)//Processing specific news query requests and returning all results
{
ArrayList newsRealese = new ArrayList();
try {
ResultSet rs = stat.executeQuery(sql);
while (rs.next()) { //Instantiation of 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 {//Users view all their articles
Connection conn = null;
ArrayList newsRealese = new ArrayList();
try {
//Get connection
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);
//Running SQL statements
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("No information can be queried============");
return null;
}
stat.close();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//Close connection
if (conn != null) {
conn.close();
}
} catch (Exception ex) {
}
return newsRealese;
}
}
public ArrayList UserQueryOthersNews(String username) throws Exception {//Check out all the articles you can see from others
Connection conn = null;
ArrayList rt = new ArrayList();
try {
//Get connection
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);
//Running SQL statements
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("No information can be queried============Dao.UserQueryOthersNews");
return null;
}
conn.close();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//Close connection
if (conn != null) {
conn.close();
}
} catch (Exception ex) {
}
return rt;
}
}
public ArrayList AdministorQueryAllNews(String username) throws Exception {//Administrators view all articles
Connection conn = null;
ArrayList newsRealese = new ArrayList();
try {
//Get connection
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);
//Running SQL statements
String sql = "select * from news order by publish_time desc";//Obtain
Statement stat = conn.createStatement();
newsRealese = getNews(stat, sql);
if (newsRealese.size() == 0) {
System.out.println("No information can be queried============");
return null;
}
stat.close();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//Close connection
if (conn != null) {
conn.close();
}
} catch (Exception ex) {
}
return newsRealese;
}
}
//Query a message
public ArrayList AdministorQueryCheckPending() throws Exception {//Query all pending articles
Connection conn = null;
ArrayList newsRealese = new ArrayList();
try {
//Get connection
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);//Unsafe
//Running SQL statements
Statement stat = conn.createStatement();
String sql = "select * from check_pending order by publish_time desc";//Get newsid, use it? Replace strings to avoid errors
Statement st = conn.createStatement();
newsRealese = getNews(st, sql);
stat.close();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//Close connection
if (conn != null) {
conn.close();
conn = null;
}
} catch (Exception ex) {
}
return newsRealese;
}
}
//User Delete Data
public String deleteOneNews(String newsid, String table) throws Exception {
Connection conn = null;
ArrayList newsRealese = new ArrayList();
try {
//Get connection
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);//Unsafe
//Running SQL statements
String sql_move = "insert into dustbin select * from news where newsId='" + newsid + "'";
String sql_delete = "DELETE FROM " + table + " WHERE newsId='" + newsid + "'";//Get newsid, use it? Replace strings to avoid errors
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("Delete failed==================NewsrealeaseDao");
} else {
System.out.println("Insert into dustbin error=============NewsrealeaseDao");
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//Close connection
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 {
//Get connection
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);//Unsafe
//Running SQL statements
String sql_delete = "DELETE FROM check_pending WHERE newsId='" + newsid + "'";//Get newsid, use it? Replace strings to avoid errors
Statement ps = conn.createStatement();
rs = ps.executeUpdate(sql_delete);
if (rs == 0) {
System.out.println("Delete failed==================NewsrealeaseDao");
return null;
}
else
return "Deleted successfully"+rs;
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//Close connection
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 {
//Get connection
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);//Unsafe
//Running SQL statements
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("Successful addition" + count + "That's ok");
stat.close();
conn.close();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//Close connection
if (conn != null) {
conn.close();
}
} catch (Exception ex) {
}
return count;
}
}
//insert data
public int insertOneNews(HashMap<String, String> addnews_list, String table) throws Exception {//Insert a new news
Connection conn = null;
try {
//Get connection
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);//Unsafe
//Running SQL statements
Statement stat = conn.createStatement();
String sql = "insert into " + table + " VALUES(?,?,?,?,?,?)";//Get newsid, use it? Replace strings to avoid errors
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("Successful addition" + i + "That's ok");
stat.close();
conn.close();
return i;
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//Close connection
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("To be implemented=====" + 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;
}
//Update data
public int updateOneNews(HashMap<String, String> addnews_list) throws Exception {
Connection conn = null;
try {
//Get connection
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);//Unsafe
//Get newsid, use it? Replace strings to avoid errors
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("Successful update" + i + "That's ok");
conn.close();
return i;
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//Close connection
if (conn != null) {
conn.close();
conn = null;
}
} catch (Exception ex) {
}
}
return 0;
}
public boolean CheckNews(String newsid)//Check and pass an article
{
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)//Check and pass an article
{
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 {//Login validation table represents which table to query from
Connection conn = null;
//Get connection
Class.forName(driver);
String sql = "select password from " + table + " where username='" + user + "'";
System.out.println(sql);
try {
conn = DriverManager.getConnection(url, databseUser, password);//Unsafe
System.out.println("establish database Connect");
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", "Article 2 Testing");
addnews_list.put("content", "This is the first test article. It's all scribbled randomly.~Thanks to Dafa's teacher, La Century Oriental opened the Christmas Welfare Cardiff Lax and opened the six-pack abdominal muscles.");
addnews_list.put("publish_time", "2017-4-4/12:12:12");
addnews_list.put("issueuser", "Wooden Warrior Heart");
addnews_list.put("newstype", "1");
NewsRealeseDao nd = new NewsRealeseDao();
ArrayList rs = nd.AdministorQueryCheckPending(); ///Query all pending orders
show(rs);
} catch (Exception e) {
e.printStackTrace();
}
// nd.CheckNews("1234567890"); //Audit an order
}
}
- 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 configuration file:
<?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
Here's a brief introduction to how to view all of everyone's articles
In fact, it calls the method in NewsRealeseDao.java directly to query the records in the database, then puts the results into an ArrayList and returns them directly, and then displays them on the JSP page.
(Viewing authorized articles by others is similar, except that a condition is added to the query.)
JSP page:
<table class="table table-hover">
<tr>
<th width="45">Choice</th>
<th width="300">Title</th>
<th width="100">time</th>
<th width="100">category</th>
<th width="150">operation</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')">details</a>
<a class="button border-blue button-little" href="#"
onclick="queryInfo('<%=tem.getNewsId()%>','news')">modify</a>
<a class="button border-red button-little" href="#"
onclick="{if(confirm('confirm deletion?')){javascrtpt:window.location.href = 'DeleteOneNews?function=user_delete&destination=content_user.jsp&table=news&newsid=<%=tem.getNewsId()%>'}return false;}">delete</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
Next, we introduce the asynchronous transmission implemented by Ajax:
At the end of the JSP page above, there are three button s, each of which has an onclick method, which is implemented using JavaScript, and then uses Ajax technology to transfer data to the background Servlet.
query.js is common to ordinary users and administrators, so there is also a checkpass method for administrators to review articles. The code is as follows:
/**
* Created by 32706 on 2017/4/5.
*/
var xmlHttp=false;
function createXMLHttpRequest()
{
if (window.ActiveXObject) //Creating XMLHttpRequest Object in IE Browser
{
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e){
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(ee){
xmlHttp=false;
}
}
}
else if (window.XMLHttpRequest) //Creating XMLHttpRequest objects in non-IE browsers
{
try{
xmlHttp = new XMLHttpRequest();
}
catch(e){
xmlHttp=false;
}
}
}
function queryInfo( id,table)
{
id=id.toString();
createXMLHttpRequest(); //Call the method to create the XMLHttpRequest object
xmlHttp.onreadystatechange=callback; //Setting callback function
//Query the Query One News Servlet for details of articles numbered id from the table table
var url="QueryOneNews?table="+table+"&newsid="+id;
alert("The query number is:"+id+"Details of the article?");
xmlHttp.open("post",url,true); //Send a request to the server
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;
//Separate the values of each field to get the attribute values of the field
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];
//Set the value in the label in 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() {//Audit pass
createXMLHttpRequest(); //Call the method to create the XMLHttpRequest object
xmlHttp.onreadystatechange=callback; //Setting callback function
var newsid=document.getElementById("check_id").value;
var url="checkNews?newsid="+newsid;
xmlHttp.open("post",url,true); //Send a request to the server
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("The audit has been approved and completed.");
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
Because of the limited space, here is only some local principles, specific implementation details can be downloaded to view the source engineering files. And I am also learning to write a blog once, so there will inevitably be a lot of mistakes, but also ask you to forgive me.
Here is a summary:
First, there are three ways for the front-end JSP to invoke Servlet (which may not be complete):
1. Specify the action field in the form tag, and then set the button type to submit, so that all data in the form can be automatically submitted to the Servlet when the button is clicked. Usually used for submitting forms such as login.
Example:
<form method="post" action="checkLogin_user">
- 1
2. Setting Servlet s to be called in onclick attributes of button s and other tags is usually suitable for simple page jumps or data submissions. It's actually a JavaScript method that's called.
Example:
<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()%>'">delete</a>
- 1
3. Use JavaScript functions to process, typically Ajax, which is needed in the project. Use JavaScript to get the value of the label in the page and then transfer it to Servelt for processing, and display the results on the original page.
Example:
var xmlHttp=false;
function createXMLHttpRequest()
{
if (window.ActiveXObject) //Creating XMLHttpRequest Object in IE Browser
{
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e){
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(ee){
xmlHttp=false;
}
}
}
else if (window.XMLHttpRequest) //Creating XMLHttpRequest objects in non-IE browsers
{
try{
xmlHttp = new XMLHttpRequest();
}
catch(e){
xmlHttp=false;
}
}
}
function queryInfo( id,table)
{
id=id.toString();
createXMLHttpRequest(); //Call the method to create the XMLHttpRequest object
xmlHttp.onreadystatechange=callback; //Setting callback function
var url="QueryOneNews?table="+table+"&newsid="+id;
alert("The query number is:"+id+"Details of the article?");
xmlHttp.open("post",url,true); //Send a request to the server
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. Use the href attribute in the < a > tag directly to specify the Servlet to be accessed. However, it seems that this method can only implement get mode.
Example:
<a href="Authorization_user?id=<%=tem.getId()%>" class="icon-file">Article authorization</a>
- 1
When the data is transferred to the Servlet, the method that the Servlet calls the background is not much different from the normal Java programming, so I won't repeat it here. If there are any shortcomings, please forgive more ~~