欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

基于jsp+servlet实现的简单博客系统实例(附源码)

程序员文章站 2022-10-31 12:59:18
本文实例讲述了基于jsp+servlet实现的简单博客系统。分享给大家供大家参考。具体如下: 没有用框架写的 小博客, 代码大量重复。 个人感觉重复代码对于新手还是有好...

本文实例讲述了基于jsp+servlet实现的简单博客系统。分享给大家供大家参考。具体如下:

没有用框架写的 小博客, 代码大量重复。

个人感觉重复代码对于新手还是有好处的,我也是新手,见谅。

完整实例代码点击此处。

1. servlet

/*
 * to change this template, choose tools | templates
 * and open the template in the editor.
 */
package servlet;
import blog.blogbean;
import blog.blogrepositoryjdbc;
import blog.pagerutil;
import java.io.ioexception;
import java.io.printwriter;
import java.net.urldecoder;
import java.util.list;
import javax.enterprise.context.requestscoped;
import javax.enterprise.context.sessionscoped;
import javax.inject.inject;
import javax.inject.named;
import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
@webservlet(name = "pagelistservlet", urlpatterns = {"/page/*", ""})
public class listallservlet extends httpservlet {
  @inject
  private blogrepositoryjdbc blogdao;
  @inject
  private pagerutil pagerutil;
  protected void processrequest(httpservletrequest request, httpservletresponse response)
      throws servletexception, ioexception {
    int pg;
    string s = request.getrequesturi();
    string searchcontent = request.getparameter("search");
    if (s.matches(request.getcontextpath() + "/page/" + "\\d")) {
      string pgnum = s.replaceall(request.getcontextpath() + "/page/", "");
      pg = integer.parseint(pgnum);
    } else if (s.matches(request.getcontextpath() + "/")) {
      pg = 1;
    } else {
      getservletcontext().getrequestdispatcher("/errorpage.jsp").forward(request, response);
      return;
    }
    list<blogbean> bloglist;
    boolean isnullrequest = (searchcontent == null || "".equals(searchcontent));
    if (isnullrequest) {
      bloglist = blogdao.getbloglist();
    } else {
      bloglist = blogdao.listblogbykey(searchcontent);
    }
    if (!pagerutil.isvalidpg(bloglist, pg)) {
      getservletcontext().getrequestdispatcher("/errorpage.jsp").forward(request, response);
      return;
    }
    pagerutil.fillvalue(bloglist, pg);
    string pageurlbase = getservletcontext().getcontextpath();
    string pageurlparam = isnullrequest ? "" : "?search=" + searchcontent;
    request.setattribute("pageurlbase", pageurlbase);
    request.setattribute("pageurlparam", pageurlparam);
    request.setattribute("bloglist", bloglist);
    getservletcontext().getrequestdispatcher("/blog.jsp").forward(request, response);
  }
  @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";
  }// </editor-fold>
}

2. dao

package blog;
import java.sql.connection;
import java.sql.preparedstatement;
import java.sql.resultset;
import java.sql.sqlexception;
import java.sql.statement;
import java.util.arraylist;
import java.util.arrays;
import java.util.date;
import java.util.hashmap;
import java.util.hashset;
import java.util.list;
import java.util.map;
import java.util.set;
import java.util.logging.level;
import java.util.logging.logger;
import javax.annotation.postconstruct;
import javax.annotation.resource;
import javax.enterprise.context.applicationscoped;
import javax.inject.inject;
import javax.inject.named;
import javax.sql.datasource;
@applicationscoped
@named
public class blogrepositoryjdbc implements blogrepository {
  @resource(lookup = "jdbc/sample")
  private datasource ds;
  private arraylist<blogbean> bloglist;
  private set<string> tagset;
  private set<string> categoryset;
  private map<string,integer> datemap;
  @postconstruct
  private void init() {
    refreshcache();
  }
  @override
  public int addblog(blogbean blogi) {
    string sql = "insert into posts values (?,?,?,?,?)";
    connection conn = null;
    preparedstatement ps = null;
    int out = 0;
    try {
      conn = ds.getconnection();
      ps = conn.preparestatement(sql);
      ps.setstring(1, blogi.getblogtitle());
      ps.setstring(2, blogi.getcategory());
      ps.setstring(3, blogi.gettag());
      ps.setdate(4, new java.sql.date(blogi.getpostdate().gettime()));
      ps.setstring(5, blogi.getcontent());
      out = ps.executeupdate();
    } catch (sqlexception ex) {
      logger.getlogger(blogrepositoryjdbc.class.getname()).log(level.severe, null, ex);
    } finally {
      closeeverything(conn, ps, null);
    }
    refreshcache();
    return out;
  }
  @override
  public int deleteblog(string blogname) {
    string sql = "delete from posts where head = ?";
    connection conn = null;
    preparedstatement ps = null;
    int out = 0;
    try {
      conn = ds.getconnection();
      ps = conn.preparestatement(sql);
      ps.setstring(1, blogname);
      out = ps.executeupdate();
    } catch (sqlexception ex) {
      logger.getlogger(blogrepositoryjdbc.class.getname()).log(level.severe, null, ex);
    } finally {
      closeeverything(conn, ps, null);
    }
    refreshcache();
    return out;
  }
  @override
  public int updateblog(blogbean blog, string oldhead) {
    string sql = "update posts set head=?,category=?,tag=?,postdate=?,content=? where head=?";
    preparedstatement ps = null;
    connection conn = null;
    int out = 0;
    try {
      conn = ds.getconnection();
      ps = conn.preparestatement(sql);
      ps.setstring(1, blog.getblogtitle());
      ps.setstring(2, blog.getcategory());
      ps.setstring(3, blog.gettag());
      ps.setdate(4, new java.sql.date(blog.getpostdate().gettime()));
      ps.setstring(5, blog.getcontent());
      ps.setstring(6, oldhead);
      out = ps.executeupdate();
    } catch (sqlexception ex) {
      logger.getlogger(blogrepositoryjdbc.class.getname()).log(level.severe, null, ex);
    } finally {
      closeeverything(conn, ps, null);
    }
    refreshcache();
    return out;
  }
  @override
  public list<blogbean> listallblog() {
    bloglist = new arraylist<blogbean>();
    blogbean blog;
    string sql = "select * from posts order by postdate desc";
    connection conn = null;
    statement ss = null;
    resultset rs = null;
    int out = 0;
    try {
      conn = ds.getconnection();
      ss = conn.createstatement();
      rs = ss.executequery(sql);
      while (rs.next()) {
        blog = new blogbean();
        blog.setblogtitle(rs.getstring(1));
        blog.setcategory(rs.getstring(2));
        blog.settag(rs.getstring(3));
        blog.setpostdate(rs.getdate(4));
        blog.setcontent(rs.getstring(5));
        bloglist.add(blog);
      }
    } catch (sqlexception ex) {
      logger.getlogger(blogrepositoryjdbc.class.getname()).log(level.severe, null, ex);
    } finally {
      closeeverything(conn, ss, rs);
    }
    return bloglist;
  }
  @override
  public list listblogbykey(string keyword) {
    list<blogbean> list = new arraylist<blogbean>();
    blogbean blog;
    string sql = "select * from posts where head like ? order by postdate desc";
    connection conn = null;
    preparedstatement ps = null;
    resultset rs = null;
    int out = 0;
    try {
      conn = ds.getconnection();
      ps = conn.preparestatement(sql);
      ps.setstring(1, "%" + keyword + "%");
      rs = ps.executequery();
      while (rs.next()) {
        blog = new blogbean();
        blog.setblogtitle(rs.getstring(1));
        blog.setcategory(rs.getstring(2));
        blog.settag(rs.getstring(3));
        blog.setpostdate(rs.getdate(4));
        blog.setcontent(rs.getstring(5));
        list.add(blog);
      }
    } catch (sqlexception ex) {
      logger.getlogger(blogrepositoryjdbc.class.getname()).log(level.severe, null, ex);
    } finally {
      closeeverything(conn, ps, rs);
    }
    return list;
  }
  @override
  public list listblogbycontent(string keyword) {
    list<blogbean> list = new arraylist<blogbean>();
    blogbean blog;
    string sql = "select * from posts where content like ? order by postdate desc";
    preparedstatement ps = null;
    resultset rs = null;
    connection conn = null;
    int out = 0;
    try {
      conn = ds.getconnection();
      ps = conn.preparestatement(sql);
      ps.setstring(1, "%" + keyword + "%");
      rs = ps.executequery();
      while (rs.next()) {
        blog = new blogbean();
        blog.setblogtitle(rs.getstring(1));
        blog.setcategory(rs.getstring(2));
        blog.settag(rs.getstring(3));
        blog.setpostdate(rs.getdate(4));
        blog.setcontent(rs.getstring(5));
        list.add(blog);
      }
    } catch (sqlexception ex) {
      logger.getlogger(blogrepositoryjdbc.class.getname()).log(level.severe, null, ex);
    } finally {
      closeeverything(conn, ps, rs);
    }
    return list;
  }
  public set listalltags() {
    tagset = new hashset<string>();
    string sql = "select tag from posts ";
    connection conn = null;
    statement ss = null;
    resultset rs = null;
    try {
      conn = ds.getconnection();
      ss = conn.createstatement();
      rs = ss.executequery(sql);
      while (rs.next()) {
        string tags = rs.getstring(1);
        string[] arraytags = tags.split(",");
        tagset.addall(arrays.aslist(arraytags));
      }
    } catch (sqlexception ex) {
      logger.getlogger(blogrepositoryjdbc.class.getname()).log(level.severe, null, ex);
    } finally {
      closeeverything(conn, ss, rs);
    }
    return tagset;
  }
  public list listblogbytag(string keyword) {
    list<blogbean> list = new arraylist<blogbean>();
    blogbean blog;
    string sql = "select * from posts where tag like ? order by postdate desc";
    connection conn = null;
    preparedstatement ps = null;
    resultset rs = null;
    int out = 0;
    try {
      conn = ds.getconnection();
      ps = conn.preparestatement(sql);
      ps.setstring(1, "%" + keyword + "%");
      rs = ps.executequery();
      while (rs.next()) {
        blog = new blogbean();
        blog.setblogtitle(rs.getstring(1));
        blog.setcategory(rs.getstring(2));
        blog.settag(rs.getstring(3));
        blog.setpostdate(rs.getdate(4));
        blog.setcontent(rs.getstring(5));
        list.add(blog);
      }
    } catch (sqlexception ex) {
      logger.getlogger(blogrepositoryjdbc.class.getname()).log(level.severe, null, ex);
    } finally {
      closeeverything(conn, ps, rs);
    }
    return list;
  }
    public list listblogbydate(string keyword) {
    list<blogbean> list = new arraylist<blogbean>();
    blogbean blog;
    string sql = "select * from posts where postdate = ? order by postdate desc";
    connection conn = null;
    preparedstatement ps = null;
    resultset rs = null;
    int out = 0;
    try {
      conn = ds.getconnection();
      ps = conn.preparestatement(sql);
      ps.setstring(1, keyword );
      rs = ps.executequery();
      while (rs.next()) {
        blog = new blogbean();
        blog.setblogtitle(rs.getstring(1));
        blog.setcategory(rs.getstring(2));
        blog.settag(rs.getstring(3));
        blog.setpostdate(rs.getdate(4));
        blog.setcontent(rs.getstring(5));
        list.add(blog);
      }
    } catch (sqlexception ex) {
      logger.getlogger(blogrepositoryjdbc.class.getname()).log(level.severe, null, ex);
    } finally {
      closeeverything(conn, ps, rs);
    }
    return list;
  }
  public set listallcategory() {
    categoryset = new hashset<string>();
    string sql = "select category from posts";
    connection conn = null;
    statement ss = null;
    resultset rs = null;
    try {
      conn = ds.getconnection();
      ss = conn.createstatement();
      rs = ss.executequery(sql);
      while (rs.next()) {
        string category = rs.getstring(1);
        categoryset.add(category);
      }
    } catch (sqlexception ex) {
      logger.getlogger(blogrepositoryjdbc.class.getname()).log(level.severe, null, ex);
    } finally {
      closeeverything(conn, ss, rs);
    }
    return categoryset;
  }
  public list listblogbycategory(string keyword) {
    list<blogbean> list = new arraylist<blogbean>();
    blogbean blog;
    string sql = "select * from posts where category like ?";
    connection conn = null;
    preparedstatement ps = null;
    resultset rs = null;
    int out = 0;
    try {
      conn = ds.getconnection();
      ps = conn.preparestatement(sql);
      ps.setstring(1, "%" + keyword + "%");
      rs = ps.executequery();
      while (rs.next()) {
        blog = new blogbean();
        blog.setblogtitle(rs.getstring(1));
        blog.setcategory(rs.getstring(2));
        blog.settag(rs.getstring(3));
        blog.setpostdate(rs.getdate(4));
        blog.setcontent(rs.getstring(5));
        list.add(blog);
      }
    } catch (sqlexception ex) {
      logger.getlogger(blogrepositoryjdbc.class.getname()).log(level.severe, null, ex);
    } finally {
      closeeverything(conn, ps, rs);
    }
    return list;
  }
  private void filldatemap(){
    string sql = "select postdate,count(postdate) from posts group by postdate";
     connection conn = null;
    statement ss = null;
    resultset rs = null;
    datemap = new hashmap<string,integer>();
    try {
      conn = ds.getconnection();
      ss = conn.createstatement();
       rs=ss.executequery(sql);  
      while (rs.next()) {
        datemap.put(rs.getstring(1), rs.getint(2));
      }
    } catch (sqlexception ex) {
      logger.getlogger(blogrepositoryjdbc.class.getname()).log(level.severe, null, ex);
    } finally {
      closeeverything(conn, ss, rs);
    }
  }
  public void test(){
        for(map.entry<string,integer> dd: datemap.entryset()){
           dd.getkey();
           dd.getvalue();
        }
  }
  public void refreshcache() {
    listallblog();
    listalltags();
    listallcategory();
    filldatemap();
  }
  public arraylist<blogbean> getbloglist() {
    return bloglist;
  }
  public set<string> gettagset() {
    return tagset;
  }
  public set<string> getcategoryset() {
    return categoryset;
  }
  public map<string, integer> getdatemap() {
    return datemap;
  }
  private void closeeverything(connection conn, statement ps, resultset rs) {
    if (rs != null) {
      try {
        rs.close();
      } catch (sqlexception ex) {
      }
    }
    if (ps != null) {
      try {
        ps.close();
      } catch (sqlexception ex) {
      }
    }
    if (conn != null) {
      try {
        conn.close();
      } catch (sqlexception ex) {
      }
    }
  }
}

3. 运行效果截图:

基于jsp+servlet实现的简单博客系统实例(附源码)

希望本文所述对大家的jsp程序设计有所帮助。