request; response);
} else {
User user = new User();
user。setUsername(username);
user。setPassword(password);
userDao。save(user);
response。sendRedirect (request。getContextPath() +
〃/security/registerSuccess。jsp〃);
}
}
获得用户名和密码后,先通过 userDao。checkExists()检测数据库中是否
已经有了同名的用户,如果用户名重复,就跳转到
/security/register。jsp 显示错误信息。如果用户名没有重复,则将此
用户信息添加入库,然后页面重定向到/security/registerSuccess。jsp
显示注册成功信息。
94 / 148
…………………………………………………………Page 95……………………………………………………………
保存信息之后使用 redirect 是个避免重复提交的简易方法,如果使用
forward,浏览器上的url 不会改变,用户刷新页面就会导致重复提交信
息。
2。 用户登录与注销
登录与注销的流程与之前介绍的大体相同。第 4。2 节 “例子:在线列表”
/**
* 登录。
*/
public void login(HttpServletRequest request;HttpServletResponse
response) throws Exception {
String username = request。getParameter(〃username〃);
String password = request。getParameter(〃password〃);
User user = userDao。login(username; password);
if (user != null) {
user。setLastLogin(new Date());
userDao。update(user);
HttpSession session = request。getSession();
session。setAttribute(〃user〃; user);
// 加入在线列表
session。setAttribute(〃onlineUserBindingListener〃; new
OnlineUserBindingListener(username));
response。sendRedirect(request。getContextPath() +
〃/security/loginSuccess。jsp〃);
} else {
request。setAttribute(〃error〃; 〃用户名或密码错误!〃);
request。getRequestDispatcher(〃/security/login。jsp〃)。forward(req
uest; response);
}
}
/**
* 注销。
*/
public void logout(HttpServletRequest
request;HttpServletResponse response) throws Exception {
request。getSession()。invalidate();
95 / 148
…………………………………………………………Page 96……………………………………………………………
response。sendRedirect(request。getContextPath() +
〃/security/logoutSuccess。jsp〃);
}
我们先根据请求中的用户名和密码去数据库搜索用户信息。如果能找到,
说明用户输入无误可以登录,这时更新用户最后登录时间,并将 user 保
存到 session 中,同时使用 listener 操作在线列表。
如果用户名或密码错误,则将请求转发至/security/login。jsp 页面,显
示错误信息。
3。 控制用户访问权限
与用户操作相关的还有 anni。utils。SecurityFilter,我们使用它来控制
用户的访问权限。可以参考之前的讨论:第 7。2 节 “用filter 控制用
户访问权限”。
web。xml 中对 SecurityFilter 的配置如下:
SecurityFilter
anni。utils。SecurityFilter
SecurityFilter
/*
因为filter…mapping太不灵活,我们让SecurityFilter过滤所有的请求,
在代码里判断哪些请求需要保护。
public void doFilter(ServletRequest request;
ServletResponse response;
FilterChain chain)
throws IOException; ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
String url = req。getServletPath();
String method = req。getParameter(〃method〃);
if (〃/create。jsp〃。equals(url) ||
(〃/thread。do〃。equals(url) && 〃post〃。equals(method)) ||
96 / 148
…………………………………………………………Page 97……………………………………………………………
(〃/ment。do〃。equals(url) && 〃post〃。equals(method))) {
HttpSession session = req。getSession();
if (session。getAttribute(〃user〃) == null) {
res。sendRedirect(req。getContextPath() +
〃/security/securityFailure。jsp〃);
return ;
}
}
chain。doFilter(request; response);
}
在此我们只保护三个请求:/create。jsp (进入发布新主题的页面),
/thread。do?method=post (发布新主题),/ment。do?method=post (发
布回复)。这三个操作只有在用户登录之后才能访问,如果用户还没有的
登录就会页面重定向到/security/securityFailure。jsp,显示权限不足
无法访问的提示信息。
10。3。2。 主题回复管理
主题回复管理功能包括:查看所有主题,查看某一主题的详细信息和对应回复,
发表新主题,发表回复。点击主题时还会计算点击数。
1。 查看所有主题信息
进入应用,index。jsp 会立即跳转到/forum。do?method=list,并在
list。jsp 中显示所有主题,包括主题标题,回复数,作者,点击数,最
后回复时间,最后回复人。这些信息按照“最后回复时间”进行逆序排列。
实现代码在 anni。web。ForumServlet 的 list()方法内。
/**
* 显示所有帖子。
*/
private void list(HttpServletRequest request; HttpServletResponse
response) throws Exception {
List list = forumDao。getAll();
request。setAttribute(〃list〃; list);
request。getRequestDispatcher(〃/list。jsp〃)。forward(request;
response);
}
97 / 148
…………………………………………………………Page 98……………………………………………………………
调用 anni。dao
小说推荐
- php程序设计简明教程(DOC格式)
- -Page 1-PHP 程序设计简明教程PHP 讲义 第 1 页 共 90 页-Page 2-目录序 4第一章 PHP 简介 6
- 最新章:第31章
- asp基础实用教程(DOC格式)
- 目 录一、关于ASP二、ASP的新功能三、创建ASP页四、使用脚本语言五、使用变量和常量六、使用集合七、ASP内建对象八、向浏览器发送内容九、包含文件十、访问数据库十一、调试ASP脚本十二、维护ASP应用程序的安全一、关于ASP Active Server Pages(ASP)是服务器端脚本编写环境
- 最新章:第17章
- asp基础入门篇(DOC格式)
- ASP入门与实例-IIS的安装与配置1、运行环境与软件要求Windows 2000 以上(不包括Windows XP Home版)IIS 4.0 以上Microsoft Access 2000Dreamweaver MX2、IIS的安装与配置a.安装IIS若操作系统中还未安装IIS服务器,可打开"控
- 最新章:第23章
- C语言游戏编程从入门到精通(PDF格式)
- -Page 1-Page 2-Page 3-Page 4-Page 5-Page 6-Page 7-Page 8-Page 9-Page 10-Page 11-Page 12-Page 13-Page 14
- 最新章:第4章
- JMS简明教程(PDF格式)
- -Page 1-JMS1.1规范中文版卫建军2007‐11‐22-Page 2
- 最新章:第28章
- C语言实例教程(PDF格式)
- -Page 1-前 言Visual C+是开发运行于Windows 95和Windows NT环境下的Win32应用程序的可视化编程工具中最重要的成员之一,它为软件开发人员提供了完整的编辑、编译和调试工具和建立于Win32 API(ApplicationProgramming Interface)基
- 最新章:第143章
- Word2003使用技巧大全(DOC格式)
- Word使用技巧(二)表格使用大全更新时间:2009-11-16 19:32:15 本文共阅读了257次 作者:xushezheng 文章来源:电教组字体大小:大中小一、快速插入表格 拖动“插入表格”能插入的最大表格跟该图标位置、显示分辨率有关。如使用800×600分辨率时最大为18行×28列 二、
- 最新章:第1章
- Excel word ppt office使用技巧大全(DOC格式)
- -Page 1-Excel_word_ppt_使用技巧大全(完全版)Excelwordppt使用技巧大全(2011新版本)I-Page 2-Excel_word_ppt_使用技巧大全(完全版
- 最新章:第338章
- oracle从入门到精通(PDF格式)
- -Page 1-Oracle 从入门到精通-Page 2-资源来自网络,仅供学习 Oracle 从入门到精通一、SQL 8
- 最新章:第37章