这一篇博客将详细介绍一个基于Servlet的问答网站的实现,有详细的代码。
可能篇幅较长,以代码为主,有兴趣的童鞋看完可以尝试动手搭建一个属于自己的问答社区。
工具:Eclipse,数据库用到了MySQL,这次项目中未使用jsp,全部以Servlet注解的方式连接HTML和Servlet,JDK最好使用1.8,tomcat使用8.0。(注解方式为JDK1.5后的特性,最低要求1.5+,本项目使用JDK1.8)。
在这篇博客中可以学习到:
1,Servlet中关于注解的使用,本项目没有使用到传统的Servlet配置web.xml,全部使用注解的形式。
2,了解Font Awesome这一矢量图标库的使用,他基本提供了项目中所要使用到的所有图标,方便,快捷。
3,了解simditor这一富文本编辑器的使用,网站中直接嵌入富文本编辑器,再也不用为读取出来的文字格式不对发愁了。
4,关于项目中如何加入验证码,数据库查询之前先进行验证码验证。
5,关于MVC框架显示层——Velocity技术的使用。
先看一下大体项目图(由于主要做后台,前台可能略丑,大家可以自行找网站模板)
登录界面:
注册界面:
首页,展示了大家的提问:
解答界面,点击别人的提问后进入解答界面,使用了富文本编辑器。
我的解答界面,展示了我回答的历史:
我的提问界面,展示了我提问的所有问题:
提问界面:进入网站点击我要提问,加入当前页编辑问题:
下面介绍主要代码(代码中加入了详细注释,所以不再做说明)
主页列表Servlet:
@WebServlet( "/list.do" )
public class ListServlet extends HttpServlet {
private static final long serialVersionUID = 810339694607399128L;
@Override
protected void service( HttpServletRequest request , HttpServletResponse response )
throws ServletException, IOException {
String question=request.getParameter("quest");
System.out.println(question);
if(StringHelper.notEmpty(question)){
final String SQL = "SELECT id , title ,content, publish_time , publish_ip , user_id FROM t_topic where title =? " ;
ResultSet rs = JdbcHelper.query( SQL,question );
// 创建一个 List 对象,用来保存一批 Topic 对象
final List topics = new ArrayList<>();
try {
// 每循环一次,光标下移一行,如果该行有数据返回 true
while( rs.next() ){
Topic t = new Topic(); // 创建对象
t.setId( rs.getInt( 1 ) ); // 将 结果集 中的 该行数据 封装到 t 对象的 id 属性中
t.setTitle( rs.getString( 2 ) );
t.setContent(rs.getString(3));
t.setPublishTime( rs.getTimestamp( 4 ));
t.setPublishIp( rs.getString( 5 ) );
User u = new User(); // 创建 一个 User 对象
u.setId( rs.getInt( 6 ) ); // 将 t_topic 表中的 user_id 放入到 User 对象的 id 属性中
t.setUser( u ); // 将 User 对象 设置到 Topic 对象上
/** 将 本次循环 创建的对象(已经封装数据) 添加到 List 集合中 */
topics.add( t );
}
} catch (SQLException e) {
e.printStackTrace();
}
JdbcHelper.release( rs ); // 关闭 结果集,释放相关的资源
/**** 为每个问题寻找提问者 ***********************************/
//for( int i = 0 ; i < topics.size() ; i++ ){
for( int i = 0 , len = topics.size() ; i < len ; i++ ){
Topic t = topics.get( i ) ; // 获得 题目
User u = t.getUser(); // 获得当前题目的User对象 ( 该对象中只有 id 没有其它数据 )
// 根据 用户对象的 id 来查询 用户的信息
String querySQL = "SELECT id , username , password FROM t_user WHERE id = ? " ;
ResultSet userRs = JdbcHelper.query( querySQL , u.getId() );
try {
if( userRs.next() ) { // 如果查询到用户信息
// 注意,这里应该使用 userRs
u.setUsername( userRs.getString( 2 ) ); // 将 username 列的值设置到 用户对象的 username 属性中
u.setPassword( userRs.getString( 3 )); // 将 password 列的值设置到 用户对象的 password 属性中
}
} catch (SQLException e) {
e.printStackTrace();
}
JdbcHelper.release( userRs ); // 关闭 结果集,释放相关的资源
}
ServletContext application = request.getServletContext();
/** 将这些数据保存到 application **/
application.setAttribute( "topics" , topics );
System.out.println( "问题列表: " + topics );
// 去 list.html 页面
response.sendRedirect( request.getContextPath() + "/list.html");
}else{
/**** 查询数据库中的所有问题 ***********************************/
final String SQL = "SELECT id , title ,content ,publish_time , publish_ip , user_id FROM t_topic ORDER BY publish_time DESC" ;
ResultSet rs = JdbcHelper.query( SQL );
// 创建一个 List 对象,用来保存一批 Topic 对象
final List topics = new ArrayList<>();
try {
// 每循环一次,光标下移一行,如果该行有数据返回 true
while( rs.next() ){
Topic t = new Topic(); // 创建对象
t.setId( rs.getInt( 1 ) ); // 将 结果集 中的 该行数据 封装到 t 对象的 id 属性中
t.setTitle( rs.getString( 2 ) );
t.setContent(rs.getString(3));
t.setPublishTime( rs.getTimestamp( 4 ));
t.setPublishIp( rs.getString( 5 ) );
User u = new User(); // 创建 一个 User 对象
u.setId( rs.getInt( 6) ); // 将 t_topic 表中的 user_id 放入到 User 对象的 id 属性中
t.setUser( u ); // 将 User 对象 设置到 Topic 对象上
/** 将 本次循环 创建的对象(已经封装数据) 添加到 List 集合中 */
topics.add( t );
}
} catch (SQLException e) {
e.printStackTrace();
}
JdbcHelper.release( rs ); // 关闭 结果集,释放相关的资源
/**** 为每个问题寻找提问者 ***********************************/
//for( int i = 0 ; i < topics.size() ; i++ ){
for( int i = 0 , len = topics.size() ; i < len ; i++ ){
Topic t = topics.get( i ) ; // 获得 题目
User u = t.getUser(); // 获得当前题目的User对象 ( 该对象中只有 id 没有其它数据 )
// 根据 用户对象的 id 来查询 用户的信息
String querySQL = "SELECT id , username , password FROM t_user WHERE id = ? " ;
ResultSet userRs = JdbcHelper.query( querySQL , u.getId() );
try {
if( userRs.next() ) { // 如果查询到用户信息
// 注意,这里应该使用 userRs
u.setUsername( userRs.getString( 2 ) ); // 将 username 列的值设置到 用户对象的 username 属性中
u.setPassword( userRs.getString( 3 )); // 将 password 列的值设置到 用户对象的 password 属性中
}
} catch (SQLException e) {
e.printStackTrace();
}
JdbcHelper.release( userRs ); // 关闭 结果集,释放相关的资源
}
ServletContext application = request.getServletContext();
/** 将这些数据保存到 application **/
application.setAttribute( "topi