java开发简易个人博客系统_基于jsp+servlet实现的简单博客系统实例(附源码)

论坛 期权论坛     
选择匿名的用户   2021-5-30 02:11   99   0
<div style="font-size:16px;">
<p>本文实例讲述了基于jsp&#43;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>&#64;WebServlet(name &#61; &#34;PageListServlet&#34;, urlPatterns &#61; {&#34;/page/*&#34;, &#34;&#34;})</p>
<p>public class ListAllServlet extends HttpServlet {<!-- --></p>
<p>&#64;Inject</p>
<p>private BlogRepositoryJDBC blogdao;</p>
<p>&#64;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 &#61; request.getRequestURI();</p>
<p>String searchContent &#61; request.getParameter(&#34;search&#34;);</p>
<p>if (s.matches(request.getContextPath() &#43; &#34;/page/&#34; &#43; &#34;\\d&#34;)) {<!-- --></p>
<p>String pgnum &#61; s.replaceAll(request.getContextPath() &#43; &#34;/page/&#34;, &#34;&#34;);</p>
<p>pg &#61; Integer.parseInt(pgnum);</p>
<p>} else if (s.matches(request.getContextPath() &#43; &#34;/&#34;)) {<!-- --></p>
<p>pg &#61; 1;</p>
<p>} else {<!-- --></p>
<p>getServletContext().getRequestDispatcher(&#34;/errorpage.jsp&#34;).forward(request, response);</p>
<p>return;</p>
<p>}</p>
<p>List bloglist;</p>
<p>boolean isNullRequest &#61; (searchContent &#61;&#61; null || &#34;&#34;.equals(searchContent));</p>
<p>if (isNullRequest) {<!-- --></p>
<p>bloglist &#61; blogdao.getBloglist();</p>
<p>} else {<!-- --></p>
<p>bloglist &#61; blogdao.listBlogByKey(searchContent);</p>
<p>}</p>
<p>if (!pagerUtil.isValidPg(bloglist, pg)) {<!-- --></p>
<p>getServletContext().getRequestDispatcher(&#34;/errorpage.jsp&#34;).forward(request, response);</p>
<p>return;</p>
<p>}</p>
<p>pagerUtil.fillValue(bloglist, pg);</p>
<p>String pageURLBase &#61; getServletContext().getContextPath();</p>
<p>String pageURLparam &#61; isNullRequest ? &#34;&#34; : &#34;?search&#61;&#34; &#43; searchContent;</p>
<p>request.setAttribute(&#34;pageURLBase&#34;, pageURLBase);</p>
<p>request.setAttribute(&#34;pageURLparam&#34;, pageURLparam);</p>
<p>request.setAttribute(&#34;bloglist&#34;, bloglist);</p>
<p>getServletContext().getRequestDispatcher(&#34;/blog.jsp&#34;).forward(request, response);</p>
<p>}</p>
<p>&#64;Override</p>
<p>protected void doGet(HttpServletRequest request, HttpServletResponse response)</p>
<p>throws ServletException, IOException {<!-- --></p>
<p>processRequest(request, response);</p>
<p>}</p>
<p>&#64;Override</p>
<p>protected void doPost(HttpServletRequest request, HttpServletResponse response)</p>
<p>throws ServletException, IOException {<!-- --></p>
<p>processRequest(request, response);</p>
<p>}</p>
<p>&#64;Override</p>
<p>public String getServletInfo() {<!-- --></p>
<p>return &#34;Short description&#34;;</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>&#64;ApplicationScoped</p>
<p>&#64;Named</p>
<p>public class BlogRepositoryJDBC implements BlogRepository {<!-- --></p>
<p>&#64;Resource(lookup &#61; &#34;jdbc/sample&#34;)</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>&#64;PostConstruct</p>
<p>private void init() {<!-- --></p>
<p>refreshCache();</p>
<p>}</p>
<p>&#64;Override</p>
<p>public int addBlog(BlogBean blogi) {<!-- --></p>
<p>String sql &#61; &#34;INSERT INTO POSTS VALUES (?,?,?,?,?)&#34;;</p>
<p>Connection conn &#61; null;</p>
<p>PreparedStatement ps &#61; null;</p>
<
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP