java开发简易个⼈博客系统_基于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.URLDecoder;
import java.util.List;
t.RequestScoped;
t.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 = RequestURI();
String searchContent = Parameter("search");
if (s.ContextPath() + "/page/" + "\\d")) {
String pgnum = s.ContextPath() + "/page/", "");
pg = Integer.parseInt(pgnum);
} else if (s.ContextPath() + "/")) {
pg = 1;
} else {
getServletContext().getRequestDispatcher("/errorpage.jsp").forward(request, response); return;
}
List bloglist;
boolean isNullRequest = (searchContent == null || "".equals(searchContent));
if (isNullRequest) {
bloglist = Bloglist();
} 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
java和jsppublic String getServletInfo() {
return "Short description";
}//
}
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;
t.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 bloglist;
private Set tagSet;
private Set categorySet;
private Map 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, BlogTitle());
ps.setString(2, Category());
ps.setString(3, Tag());
ps.setDate(4, new java.sql.PostDate().getTime()));
ps.setString(5, Content());
out = ps.executeUpdate();
} catch (SQLException 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) {
} 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);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。