小博客系统

Java实践项目——简易博客系统

1,需求分析
根据用户故事梳理需求

①用户可以注册成为合法用户,并保持登录状态
②用户可以直接进行登录
③用户可以发表文章
④用户可以查看系统中已有的文章列表
⑤用户可以选择已有的文章来查看内容

2,设计方法
根据用户故事,设计资源和资源之间的跳转关系(方法+URL)


GET/ register.html 可以返回表单
POST/ register 接受输入,进行注册


GET/ login.html 可以返回表单
POST/ login 接受输入,进行登录


GET/ publish.html 可以返回表单
POST/ publish 接受输入,进行文章录入


GET/ list.html 发起ajax请求,从服务端返回文章列表的数据(JSON格式),通过js把数据动态更新到页面上
GET/api/atricle-list.json 给出文章的API格式


GET/atricle/<文章id>

3,数据库
使用MySQL作为业务数据的存储,使用E-R图得到表结构
首先建立库表,用户表和文章表

   create database blog charset utf8mb4;
   use blog;

   create table users (
     id int primary key auto_increment,
     username varchar(40) not null unique,
     password varchar(100) not null
   );

   create table articles (
     id int primary key auto_increment,
     user_id int not null,
     title varchar(200) not null,
     content text not null,
     published_at datetime not null
   );

连接数据库

package com.yangjingxin.util;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

public class DBUtil {
    public static final DataSource dataSource = initDataSource();

    private static DataSource initDataSource() {
        MysqlDataSource mysqlDataSource = new MysqlDataSource();
        mysqlDataSource.setServerName("127.0.0.1");
        mysqlDataSource.setPort(3306);
        mysqlDataSource.setUser("root");
        mysqlDataSource.setPassword("");
        mysqlDataSource.setDatabaseName("blog");
        mysqlDataSource.setCharacterEncoding("utf8");
        mysqlDataSource.setUseSSL(false);

        return mysqlDataSource;
    }

    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }
}

3,功能实现
注册

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        User user;
        try {
            user = User.register(username, password);
        } catch (SQLException e) {
            throw new ServletException(e);
        }

        if (user == null) {
            resp.sendRedirect("register.html");
            return;
        }

        // 注册完成后视为登录
        HttpSession session = req.getSession();
        session.setAttribute("user", user);

        resp.sendRedirect("/");
    }
}

登录

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        User user;
        try {
            user = User.login(username, password);
        } catch (SQLException e) {
            throw new ServletException(e);
        }

        if (user == null) {
            resp.sendRedirect("login.html");
            return;
        }

        HttpSession session = req.getSession();
        session.setAttribute("user", user);

        resp.sendRedirect("/");
    }
}

在这里插入图片描述
用户进行注册后
在这里插入图片描述用户可发表文章并进行查看
在这里插入图片描述----------------------------------------------------------------------------------
文章列表


@WebServlet("/api/article-list.json")
public class ArticleListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Article> articleList = null;
        try {
            articleList = Article.list();
        } catch (SQLException e) {
            throw new ServletException(e);
        }
        String jsonText = translateToJSON(articleList);
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json");
        resp.getWriter().println(jsonText);
    }
    private String translateToJSON(List<Article> articleList) {
        JSONArray array = new JSONArray();
        for (Article article : articleList) {
            JSONObject articleObject = new JSONObject();
            JSONObject authorObject = new JSONObject();
            authorObject.put("id", article.authorId);
            authorObject.put("username", article.authorUsername);
            articleObject.put("id", article.id);
            articleObject.put("author", authorObject);
            articleObject.put("title", article.title);
            articleObject.put("published-at", article.publishedAt);
            array.add(articleObject);
        }
        return array.toJSONString();
    }
}

在这里插入图片描述----------------------------------------------------------------------------------
查看文章

在这里插入图片描述
项目中遇到的问题:
1,html编写前端页面不熟悉,出错较多。
2,tomcat的部署,如果连接不上需要检查8080端口是否被占用。
3,每个页面调用后台的接口都需要用到servlet,servlet部分还需要更熟练。

项目总结:
对做项目有了一个框架思想,整理URL资源,分清楚POST和GET方法;可以先将所有类写出来,知道每个类的用处,再写代码。也对用html编写页面有了更深入的了解。更加理解了http协议,学会了tomcat的使用。文字化的知识更加明了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值