没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
基于基于jsp+servlet实现的简单博客系统实例实现的简单博客系统实例(附源码附源码)
主要介绍了基于jsp+servlet实现的简单博客系统,以完整实例形式分析了基于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 {
剩余6页未读,继续阅读
资源评论
weixin_38646230
- 粉丝: 6
- 资源: 876
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 注塑技术员试题及答案.doc
- 自学考试房地产开发和经营重点.doc
- 江苏镇江市2018年中考语文试题答案和解析.doc
- 精神病学试题与答案.doc
- 教育行动研究报告的写作.doc
- 老年人常见疾病的护理知识.doc
- 考试后激励学生的话.doc
- 廉洁文化主题教育课教学案.doc
- 贫困家庭申请书范文(精选多篇).doc
- 培训机构教学计划.doc
- 全新版大学英语综合教程3contentquestions答案.doc
- 全科医师转岗培训理论考试题和正确答案.doc
- 全国居民健康素养知识问卷80题及答案.doc
- 服装公司薪酬福利管理手册.docx
- 服装薪酬体系-KPI绩效考核指标.xls
- 各岗位KPI绩效考核指标——服装生产企业.xls
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功