<div style="font-size:16px;">
<p>本文实例讲述了基于jsp+servlet实现的简单博客系统。分享给大家供大家参考。具体如下:</p>
<p>没有用框架写的 小博客, 代码大量重复。</p>
<p>个人感觉重复代码对于新手还是有好处的,我也是新手,见谅。</p>
<p>完整实例代码点击此处本站下载。</p>
<p>1. servlet</p>
<p>/*</p>
<p>* To change this template, choose Tools | Templates</p>
<p>* and open the template in the editor.</p>
<p>*/</p>
<p>package Servlet;</p>
<p>import blog.BlogBean;</p>
<p>import blog.BlogRepositoryJDBC;</p>
<p>import blog.PagerUtil;</p>
<p>import java.io.IOException;</p>
<p>import java.io.PrintWriter;</p>
<p>import java.net.URLDecoder;</p>
<p>import java.util.List;</p>
<p>import javax.enterprise.context.RequestScoped;</p>
<p>import javax.enterprise.context.SessionScoped;</p>
<p>import javax.inject.Inject;</p>
<p>import javax.inject.Named;</p>
<p>import javax.servlet.ServletException;</p>
<p>import javax.servlet.annotation.WebServlet;</p>
<p>import javax.servlet.http.HttpServlet;</p>
<p>import javax.servlet.http.HttpServletRequest;</p>
<p>import javax.servlet.http.HttpServletResponse;</p>
<p>@WebServlet(name = "PageListServlet", urlPatterns = {"/page/*", ""})</p>
<p>public class ListAllServlet extends HttpServlet {<!-- --></p>
<p>@Inject</p>
<p>private BlogRepositoryJDBC blogdao;</p>
<p>@Inject</p>
<p>private PagerUtil pagerUtil;</p>
<p>protected void processRequest(HttpServletRequest request, HttpServletResponse response)</p>
<p>throws ServletException, IOException {<!-- --></p>
<p>int pg;</p>
<p>String s = request.getRequestURI();</p>
<p>String searchContent = request.getParameter("search");</p>
<p>if (s.matches(request.getContextPath() + "/page/" + "\\d")) {<!-- --></p>
<p>String pgnum = s.replaceAll(request.getContextPath() + "/page/", "");</p>
<p>pg = Integer.parseInt(pgnum);</p>
<p>} else if (s.matches(request.getContextPath() + "/")) {<!-- --></p>
<p>pg = 1;</p>
<p>} else {<!-- --></p>
<p>getServletContext().getRequestDispatcher("/errorpage.jsp").forward(request, response);</p>
<p>return;</p>
<p>}</p>
<p>List bloglist;</p>
<p>boolean isNullRequest = (searchContent == null || "".equals(searchContent));</p>
<p>if (isNullRequest) {<!-- --></p>
<p>bloglist = blogdao.getBloglist();</p>
<p>} else {<!-- --></p>
<p>bloglist = blogdao.listBlogByKey(searchContent);</p>
<p>}</p>
<p>if (!pagerUtil.isValidPg(bloglist, pg)) {<!-- --></p>
<p>getServletContext().getRequestDispatcher("/errorpage.jsp").forward(request, response);</p>
<p>return;</p>
<p>}</p>
<p>pagerUtil.fillValue(bloglist, pg);</p>
<p>String pageURLBase = getServletContext().getContextPath();</p>
<p>String pageURLparam = isNullRequest ? "" : "?search=" + searchContent;</p>
<p>request.setAttribute("pageURLBase", pageURLBase);</p>
<p>request.setAttribute("pageURLparam", pageURLparam);</p>
<p>request.setAttribute("bloglist", bloglist);</p>
<p>getServletContext().getRequestDispatcher("/blog.jsp").forward(request, response);</p>
<p>}</p>
<p>@Override</p>
<p>protected void doGet(HttpServletRequest request, HttpServletResponse response)</p>
<p>throws ServletException, IOException {<!-- --></p>
<p>processRequest(request, response);</p>
<p>}</p>
<p>@Override</p>
<p>protected void doPost(HttpServletRequest request, HttpServletResponse response)</p>
<p>throws ServletException, IOException {<!-- --></p>
<p>processRequest(request, response);</p>
<p>}</p>
<p>@Override</p>
<p>public String getServletInfo() {<!-- --></p>
<p>return "Short description";</p>
<p>}// </p>
<p>}</p>
<p>2. dao</p>
<p>package blog;</p>
<p>import java.sql.Connection;</p>
<p>import java.sql.PreparedStatement;</p>
<p>import java.sql.ResultSet;</p>
<p>import java.sql.SQLException;</p>
<p>import java.sql.Statement;</p>
<p>import java.util.ArrayList;</p>
<p>import java.util.Arrays;</p>
<p>import java.util.Date;</p>
<p>import java.util.HashMap;</p>
<p>import java.util.HashSet;</p>
<p>import java.util.List;</p>
<p>import java.util.Map;</p>
<p>import java.util.Set;</p>
<p>import java.util.logging.Level;</p>
<p>import java.util.logging.Logger;</p>
<p>import javax.annotation.PostConstruct;</p>
<p>import javax.annotation.Resource;</p>
<p>import javax.enterprise.context.ApplicationScoped;</p>
<p>import javax.inject.Inject;</p>
<p>import javax.inject.Named;</p>
<p>import javax.sql.DataSource;</p>
<p>@ApplicationScoped</p>
<p>@Named</p>
<p>public class BlogRepositoryJDBC implements BlogRepository {<!-- --></p>
<p>@Resource(lookup = "jdbc/sample")</p>
<p>private DataSource ds;</p>
<p>private ArrayList bloglist;</p>
<p>private Set tagSet;</p>
<p>private Set categorySet;</p>
<p>private Map dateMap;</p>
<p>@PostConstruct</p>
<p>private void init() {<!-- --></p>
<p>refreshCache();</p>
<p>}</p>
<p>@Override</p>
<p>public int addBlog(BlogBean blogi) {<!-- --></p>
<p>String sql = "INSERT INTO POSTS VALUES (?,?,?,?,?)";</p>
<p>Connection conn = null;</p>
<p>PreparedStatement ps = null;</p>
< |
|