項目開發(fā)的準備工作已經差不多了,現在需要的是與數據表對應的數據類。
1. 數據類
按照編程習慣首先創(chuàng)建package,項目名稱右鍵選擇New->Java package; package name填寫com.blog

在com.blog下面創(chuàng)建與數據表對應的Blog,Comment,Category以及User這4個類
blog.java
2. Servlet
考慮到如果按照以后jsp的功能來寫servlet的話,文件多了既混亂也不便于管理,所以按照數據類創(chuàng)建
BlogServlet(與blog相關的Servlet,如:添加,刪除,修改,瀏覽,查詢), CommentServlet, CategoryServlet和UserServlet,4個Servlet。


因為默認添加servlet信息到deployment文件,所以打開web.xml,相應的servlet信息已經被添加進去了。
1. 數據類
按照編程習慣首先創(chuàng)建package,項目名稱右鍵選擇New->Java package; package name填寫com.blog

在com.blog下面創(chuàng)建與數據表對應的Blog,Comment,Category以及User這4個類
blog.java
1 /*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5
6 package com.blog;
7
8 import java.util.Date;
9
10 /**
11 *
12 * @author Chucky
13 */
14 public class Blog {
15 private int id;
16 private String title;
17 private String content;
18 private Date date;
19 private int categoryId;
20 private String category;
21
22 public String getCategory() {
23 return category;
24 }
25
26 public void setCategory(String category) {
27 this.category = category;
28 }
29
30 public int getCategoryId() {
31 return categoryId;
32 }
33
34 public void setCategoryId(int categoryId) {
35 this.categoryId = categoryId;
36 }
37
38 public String getContent() {
39 return content;
40 }
41
42 public void setContent(String content) {
43 this.content = content;
44 }
45
46 public Date getDate() {
47 return date;
48 }
49
50 public void setDate(Date date) {
51 this.date = date;
52 }
53
54 public int getId() {
55 return id;
56 }
57
58 public void setId(int id) {
59 this.id = id;
60 }
61
62 public String getTitle() {
63 return title;
64 }
65
66 public void setTitle(String title) {
67 this.title = title;
68 }
69
70 }
71
Category.java2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5
6 package com.blog;
7
8 import java.util.Date;
9
10 /**
11 *
12 * @author Chucky
13 */
14 public class Blog {
15 private int id;
16 private String title;
17 private String content;
18 private Date date;
19 private int categoryId;
20 private String category;
21
22 public String getCategory() {
23 return category;
24 }
25
26 public void setCategory(String category) {
27 this.category = category;
28 }
29
30 public int getCategoryId() {
31 return categoryId;
32 }
33
34 public void setCategoryId(int categoryId) {
35 this.categoryId = categoryId;
36 }
37
38 public String getContent() {
39 return content;
40 }
41
42 public void setContent(String content) {
43 this.content = content;
44 }
45
46 public Date getDate() {
47 return date;
48 }
49
50 public void setDate(Date date) {
51 this.date = date;
52 }
53
54 public int getId() {
55 return id;
56 }
57
58 public void setId(int id) {
59 this.id = id;
60 }
61
62 public String getTitle() {
63 return title;
64 }
65
66 public void setTitle(String title) {
67 this.title = title;
68 }
69
70 }
71
1 /*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5
6 package com.blog;
7
8 /**
9 * This is category class which records id and name
10 * @author Chucky
11 */
12 public class Category {
13 private int id;
14 private String name;
15
16 public int getId() {
17 return id;
18 }
19
20 public void setId(int id) {
21 this.id = id;
22 }
23
24 public String getName() {
25 return name;
26 }
27
28 public void setName(String name) {
29 this.name = name;
30 }
31
32 }
33
Comment.java2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5
6 package com.blog;
7
8 /**
9 * This is category class which records id and name
10 * @author Chucky
11 */
12 public class Category {
13 private int id;
14 private String name;
15
16 public int getId() {
17 return id;
18 }
19
20 public void setId(int id) {
21 this.id = id;
22 }
23
24 public String getName() {
25 return name;
26 }
27
28 public void setName(String name) {
29 this.name = name;
30 }
31
32 }
33
1 /*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5
6 package com.blog;
7
8 import java.util.Date;
9
10 /**
11 * this is comment class which records id,name,content,date and blog_id
12 * @author Chucky
13 */
14 public class Comment {
15 private int id;
16 private String name;
17 private String content;
18 private Date date;
19 private int blog_id;
20
21 public int getBlog_id() {
22 return blog_id;
23 }
24
25 public void setBlog_id(int blog_id) {
26 this.blog_id = blog_id;
27 }
28
29 public String getContent() {
30 return content;
31 }
32
33 public void setContent(String content) {
34 this.content = content;
35 }
36
37 public Date getDate() {
38 return date;
39 }
40
41 public void setDate(Date date) {
42 this.date = date;
43 }
44
45 public int getId() {
46 return id;
47 }
48
49 public void setId(int id) {
50 this.id = id;
51 }
52
53 public String getName() {
54 return name;
55 }
56
57 public void setName(String name) {
58 this.name = name;
59 }
60
61 }
62
User.java2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5
6 package com.blog;
7
8 import java.util.Date;
9
10 /**
11 * this is comment class which records id,name,content,date and blog_id
12 * @author Chucky
13 */
14 public class Comment {
15 private int id;
16 private String name;
17 private String content;
18 private Date date;
19 private int blog_id;
20
21 public int getBlog_id() {
22 return blog_id;
23 }
24
25 public void setBlog_id(int blog_id) {
26 this.blog_id = blog_id;
27 }
28
29 public String getContent() {
30 return content;
31 }
32
33 public void setContent(String content) {
34 this.content = content;
35 }
36
37 public Date getDate() {
38 return date;
39 }
40
41 public void setDate(Date date) {
42 this.date = date;
43 }
44
45 public int getId() {
46 return id;
47 }
48
49 public void setId(int id) {
50 this.id = id;
51 }
52
53 public String getName() {
54 return name;
55 }
56
57 public void setName(String name) {
58 this.name = name;
59 }
60
61 }
62
1 /*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5
6 package com.blog;
7
8 /**
9 *
10 * @author Chucky
11 */
12 public class User {
13 private int id;
14 private String userName;
15 private String password;
16
17 public int getId() {
18 return id;
19 }
20
21 public void setId(int id) {
22 this.id = id;
23 }
24
25 public String getPassword() {
26 return password;
27 }
28
29 public void setPassword(String password) {
30 this.password = password;
31 }
32
33 public String getUserName() {
34 return userName;
35 }
36
37 public void setUserName(String userName) {
38 this.userName = userName;
39 }
40
41 }
42
值得注意的是為了開發(fā)的方便,在Blog.java里與數據表不同,多了一個category屬性。2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5
6 package com.blog;
7
8 /**
9 *
10 * @author Chucky
11 */
12 public class User {
13 private int id;
14 private String userName;
15 private String password;
16
17 public int getId() {
18 return id;
19 }
20
21 public void setId(int id) {
22 this.id = id;
23 }
24
25 public String getPassword() {
26 return password;
27 }
28
29 public void setPassword(String password) {
30 this.password = password;
31 }
32
33 public String getUserName() {
34 return userName;
35 }
36
37 public void setUserName(String userName) {
38 this.userName = userName;
39 }
40
41 }
42
2. Servlet
考慮到如果按照以后jsp的功能來寫servlet的話,文件多了既混亂也不便于管理,所以按照數據類創(chuàng)建
BlogServlet(與blog相關的Servlet,如:添加,刪除,修改,瀏覽,查詢), CommentServlet, CategoryServlet和UserServlet,4個Servlet。


因為默認添加servlet信息到deployment文件,所以打開web.xml,相應的servlet信息已經被添加進去了。