一 项目开发环境以及主要视图
1 开发环境:
本项目采用了最基本的JSP MVC模式(javabean+jsp+servlet)开发,数据库使用了MySQL,JDBC连接采用了JNDI连接池。除此之外,代码开发用了JDK1.8+tomcat8+eclipse for javaee,编码统一使用了UTF-8
2 主要视图:
(1)首页:
(2)注册
(3)登录
(4)个人信息
(5)会员详细信息
(6)文章列表
(7)文章详情
注:整个项目的主要视图差不多就是这些了,其他的我就不截图了
二 开发流程
1 简介:
用基本的JSP MVC模式开发网站,我总结的思路是这样的:(1)首先封装dao层的数据库操作(如:增删改查);(2)前台视图页面xx.jsp;(3)在web.xml里配置servlet;(4)最后是在action层(servlet)里调用dao层里的数据库操作完成相应的功能(比如:注册就是讲用户提交的数据写入数据库中);(4)根据操作是否成功给用户返回相应的显示页面yy.jsp
(PS:在这里我省略了业务逻辑层biz,结构太复杂对新手来说反而不容易入门。另:自我检讨下,当时写这个代码时候对分层思想不是很熟悉,所以大部分的dao层的具体操作都给写到servlet中去了)
2 以注册这个功能点举例,其他功能点可以仿照这个步骤完成
(1)dao层的数据库连接DbConn.java。返回一个Connection(这里我做的不好,只有一个数据库连接功能,其他的增删改查都写到action层里去了)
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
|
package
com.zifangsky.OnlineFriend.util;
import
java.sql.Connection;
import
java.sql.DriverManager;
import
javax.naming.InitialContext;
import
javax.sql.DataSource;
public
class
DbConn {
/**
* 通过JNDI连接池的方式
* */
public
static
Connection getConnection(){
try
{
InitialContext context =
new
InitialContext();
DataSource dSource = (DataSource) context.lookup(
"java:comp/env/jdbc/onlinefriend"
);
Connection conn = dSource.getConnection();
return
conn;
}
catch
(Exception e) {
e.printStackTrace();
}
return
null
;
}
/**
* 最基本的方式
* */
// private static String driver = "com.mysql.jdbc.Driver";
// private static String url = "jdbc:mysql://127.0.0.1:3306/onlinefriend?useUnicode=true&characterEncoding=utf-8";
// private static String user = "root";
// private static String passwd = "root";
// public static Connection getConnection(){
// try {
// //加载驱动
// Class.forName(driver);
// //连接数据库
// Connection conn = DriverManager.getConnection(url,user,passwd);
//
// return conn;
// } catch (Exception e) {
// e.printStackTrace();
// }
// return null;
// }
}
|
注:如果对JNDI不熟悉,可以使用注释中的那种连接方式
(2)视图页面register.jsp
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
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<
html
>
<
head
>
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=UTF-8"
>
<
base
href="<%=basePath%>">
<%@ include file="head.txt"%>
</
head
>
<
body
>
<
font
size
=
3
><
br
>
<
center
>
<
form
action
=
"helpRegister"
name
=
""
method
=
"post"
>
<
table
>
<
caption
>带<
font
color
=
red
> * </
font
>号项为必填项</
caption
>
<
tr
><
td
align
=
left
>会员名称:</
td
><
td
><
input
type
=
text
name
=
"id"
><
font
color
=
red
> *</
font
></
td
></
tr
>
<
tr
><
td
align
=
left
>设置密码:</
td
><
td
><
input
type
=
password
name
=
"password"
><
font
color
=
red
> *</
font
></
td
></
tr
>
<
tr
><
td
align
=
left
>电子邮箱:</
td
><
td
><
input
type
=
text
name
=
"email"
></
td
></
tr
>
<
tr
><
td
align
=
left
>联系电话:</
td
><
td
><
input
type
=
text
name
=
"phone"
></
td
></
tr
>
</
table
>
<
table
>
<
tr
><
td
align
=
left
>输入您的简历和交友标准:</
td
></
tr
>
<
tr
>
<
td
align
=
left
><
TextArea
name
=
"message"
rows
=
"6"
cols
=
"30"
></
TextArea
></
td
>
</
tr
>
<
tr
><
td
align
=
center
><
input
type
=
submit
name
=
"submit"
value
=
"注册"
></
td
></
tr
>
</
table
>
</
form
>
</
center
>
</
font
>
</
body
>
</
html
>
|
注:1)页头加上了
1
2
3
4
|
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
|
1
|
<
base
href="<%=basePath%>">
|
是为了使项目中的所有链接的路径都是相对于项目路径的,因此即使项目的部署路径改变了也不用重新修改以前的链接路径,现在一般都会把这两句话加上
2)注册页面建议加上一个随机生成的token参数,然后分两处保存,一处是在session中,一处是在表单中,提交表单后通过比较两个值是否一样来防止不法份子用软件来批量提交注册(PS:我这里忘记写了)
表单之前添加:
1
2
3
|
<%
String tokenValue = String.valueOf(System.currentTimeMillis());
%>
|
表单中添加:
1
2
3
4
5
6
|
<
div
>
<
input
type
=
"hidden"
name
=
"token"
value="<%=tokenValue %>">
<%
session.setAttribute("token", tokenValue);
%>
</
div
>
|
3)servlet中这样验证:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public
void
doPost(HttpServletRequest request,HttpServletResponse response)
throws
ServletException,IOException{
request.setCharacterEncoding(
"utf-8"
);
response.setCharacterEncoding(
"utf-8"
);
HttpSession session = request.getSession(
true
);
String token = StringUtil.xssEncode(request.getParameter(
"token"
).trim());
Object token_session = session.getAttribute(
"token"
);
if
(token_session !=
null
&& token_session.equals(token)){
session.removeAttribute(
"token"
);
//重要
continueDoPost(request,response);
}
else
{
response.sendRedirect(
"index.jsp"
);
return
;
}
}
private
void
continueDoPost(HttpServletRequest request, HttpServletResponse response)
throws
ServletException,IOException{
request.setCharacterEncoding(
"utf-8"
);
response.setCharacterEncoding(
"utf-8"
);
HttpSession session = request.getSession(
true
);
//后面的省略
}
|
(3)web.xml添加一个节点:
1
2
3
4
5
6
7
8
9
|
<!-- 注册 -->
<
servlet
>
<
servlet-name
>register</
servlet-name
>
<
servlet-class
>com.zifangsky.OnlineFriend.servlet.member.HandleRegister</
servlet-class
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>register</
servlet-name
>
<
url-pattern
>/helpRegister</
url-pattern
>
</
servlet-mapping
>
|
(4)servlet页面HandleRegister.java,调用了对应的javabean来保存参数值
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
|
package
com.zifangsky.OnlineFriend.servlet.member;
import
java.io.IOException;
import
java.sql.Connection;
import
java.sql.PreparedStatement;
import
java.sql.SQLException;
import
javax.servlet.RequestDispatcher;
import
javax.servlet.ServletConfig;
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
com.zifangsky.OnlineFriend.model.member.Register;
import
com.zifangsky.OnlineFriend.util.DbConn;
import
com.zifangsky.OnlineFriend.util.StringUtil;
public
class
HandleRegister
extends
HttpServlet{
private
String backNews =
""
,pic =
"public.jpg"
;
//pic为图片信息
public
void
init(ServletConfig config)
throws
ServletException{
super
.init(config);
}
public
void
doPost(HttpServletRequest request,HttpServletResponse response)
throws
ServletException,IOException{
Register registerBean =
new
Register();
request.setAttribute(
"register"
, registerBean);
request.setCharacterEncoding(
"utf-8"
);
response.setCharacterEncoding(
"utf-8"
);
//获取注册信息
String id = StringUtil.xssEncode(request.getParameter(
"id"
).trim());
String password = StringUtil.xssEncode(request.getParameter(
"password"
).trim());
String email = StringUtil.xssEncode(request.getParameter(
"email"
).trim());
String phone = StringUtil.xssEncode(request.getParameter(
"phone"
).trim());
String message = StringUtil.xssEncode(request.getParameter(
"message"
));
boolean
isSuccess =
false
;
//判断注册信息是否符合规定
if
(StringUtil.isNotEmpty(id) &&StringUtil.isNotEmpty(password)){
isSuccess =
true
;
//判断id是否符合标准
for
(
int
i=
0
;i<id.length();i++){
char
c = id.charAt(i);
if
(!((c>=
'a'
&&c<=
'z'
) || (c>=
'A'
&&c<=
'Z'
) || (c>=
'0'
&&c<=
'9'
))){
isSuccess =
false
;
break
;
}
}
}
//向 mysql 中注册用户
try
{
if
(isSuccess){
Connection conn = DbConn.getConnection();
PreparedStatement preparedStatement = conn.prepareStatement(
"insert into member(id,password,email,phone,message,pic) values(?,?,?,?,?,?)"
);
preparedStatement.setString(
1
, id);
preparedStatement.setString(
2
, password);
preparedStatement.setString(
3
, email);
preparedStatement.setString(
4
, phone);
preparedStatement.setString(
5
, message);
preparedStatement.setString(
6
, pic);
//执行成功返回行数大于0
int
num = preparedStatement.executeUpdate();
if
(num !=
0
){
backNews =
"注册成功"
;
registerBean.setBackNews(backNews);
registerBean.setId(id);
registerBean.setPassword(password);
registerBean.setEmail(email);
registerBean.setPhone(phone);
registerBean.setMessage(message);
registerBean.setRegisterSuccess(
true
);
}
preparedStatement.close();
conn.close();
}
else
{
backNews =
"信息填写不完整或者名字中有非法字符"
;
registerBean.setBackNews(backNews);
}
}
catch
(SQLException e) {
backNews =
"该ID已被使用,请更换ID"
;
registerBean.setBackNews(backNews);
}
RequestDispatcher dispatcher = request.getRequestDispatcher(
"showRegisterMess.jsp"
);
dispatcher.forward(request, response);
}
public
void
doGet(HttpServletRequest request,HttpServletResponse response)
throws
ServletException,IOException{
doPost(request, response);
}
}
|
(5)注册成功的显示视图showRegisterMess.jsp
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
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ page import="com.zifangsky.OnlineFriend.model.member.Register"%>
<
jsp:useBean
id
=
"register"
type
=
"com.zifangsky.OnlineFriend.model.member.Register"
scope
=
"request"
/>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<
html
>
<
head
>
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=UTF-8"
>
<
base
href="<%=basePath%>">
<%@ include file="head.txt"%>
</
head
>
<
body
>
<
center
>
<
font
size
=
4
color
=
blue
>
<
br
><
jsp:getProperty
name
=
"register"
property
=
"backNews"
/>
</
font
>
<% if(register.isRegisterSuccess()){ %>
<
font
size
=
3
>
<
table
>
<
tr
><
td
align
=
left
>注册的会员名称:</
td
><
td
><
jsp:getProperty
name
=
"register"
property
=
"id"
/></
td
></
tr
>
<
tr
><
td
align
=
left
>注册的电子邮箱:</
td
><
td
><
jsp:getProperty
name
=
"register"
property
=
"email"
/></
td
></
tr
>
<
tr
><
td
align
=
left
>注册的联系电话:</
td
><
td
><
jsp:getProperty
name
=
"register"
property
=
"phone"
/></
td
></
tr
>
</
table
>
<
table
>
<
tr
><
td
align
=
left
>您的简历和交友标准:</
td
></
tr
>
<
tr
>
<
td
align
=
left
><
TextArea
name
=
"message"
rows
=
"6"
cols
=
"30"
><
jsp:getProperty
name
=
"register"
property
=
"message"
/></
TextArea
></
td
>
</
tr
>
</
table
>
</
font
>
<%}
else{%>
<
br
><
a
href
=
"register.jsp"
>点击这里,重新注册</
a
>
<%} %>
</
center
>
</
body
>
</
html
>
|
OK,一个完整的功能点就这样做完了,其他功能点可以自行参考这个步骤完成
本文转自 pangfc 51CTO博客,原文链接:http://blog.51cto.com/983836259/1718007,如需转载请自行联系原作者