本篇文章接着完成博客项目的编写,之前的代码编写过程可以查看
5.接口的设计
1)获取注册页面
请求:GET /register.html
响应:注册页面,页面上有两个输入框(输入用户名密码) 一个提交按钮
2)实现注册功能(注册页面中提交的form 表单数据,需要服务器处理)
请求:POST /register
响应:返回一个提示页面,告诉用户注册成功还是失败,并且能跳转到登录页面
3)实现登录页面
请求:GET /login.html
响应:返回一个登陆页面,包含两个输入框和一个登录按钮
4)实现登录功能(登录页面提交的数据,需要服务器来处理)
请求:POST /login
响应:返回一个提示页面,告诉用户登录成功还是失败
5)获取文章列表
请求:GET /article
响应:返回文章的列表页(包含一些文章的标题,点击标题,进入到详情页)
6)获取文章详细内容
请求:GET /article?articleId
响应:返回文章的详情页(包含文章的内容)
7)新增/发布文章
请求:POST /article
title=xxx&content=xxxxx
响应:返回一个页面告诉用户是发布成功还是失败
8)删除文章
请求:GET /deleteArticle?articleId=1
响应:返回一个提示页面,告诉用户删除成功还是失败
6.开始实现接口
1.RegisterServlet
package API;
import model.User;
import model.UserDao;
import view.HtmlGernerator;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
/**
* @program: blogDemo
* @description
* @author: wenwen
* @create: 2021-08-15 20:22
**/
public class RegisterServlet extends HttpServlet {
//浏览器是通过 POST 方法提交注册信息给服务器的
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html; charset=utf-8");
//1.获取到前端提交的数据(用户名,密码),校验是否合法
String name = req.getParameter("name");
String password = req.getParameter("password");
if(name == null || "".equals(name)
|| password == null || "".equals(password)){
//用户提交的数据有误
//返回一个错误页面(一段字符串,html 构成得到)
String html = HtmlGernerator.getMessagePage("用户名或者密码为空","register.html");
resp.getWriter().write(html);
return;
}
//2.拿着用户名到数据库中查一下,查看当前用户名是否已经存在,如果存在,认为注册失败
UserDao userDao = new UserDao();
User existUser = null;
try {
existUser = userDao.selectByName(name);
} catch (SQLException e) {
e.printStackTrace();
}
if(existUser != null){
//说明用户名已经存在,就提示用户注册失败,用户名重复
String html = HtmlGernerator.getMessagePage("用户名重复,请换个名字","register.html");
resp.getWriter().write(html);
return;
}
//3.根据前端提交的数据构造User 对象,并插入到数据库中
User user = new User();
user.setName(name);
user.setPassword(password);
try {
userDao.add(user);
} catch (SQLException e) {
e.printStackTrace();
}
//4.返回一个结果页面,提示当前注册成功
String html = HtmlGernerator.getMessagePage("注册成功!!点击跳转到登录页面", "login.html");
resp.getWriter().write(html);
}
}
2.LoginServlet
package API;
import model.User;
import model.UserDao;
import view.HtmlGernerator;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
/**
* @program: blogDemo
* @description
* @author: wenwen
* @create: 2021-08-16 08:34
**/
public class LoginServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html; charset=utf-8");
//1.获取到用户名和密码,并进行简单校验
String name = req.getParameter("name");
String password = req.getParameter("password");
if(name == null || "".equals(name)
|| password == null ||"".equals(password)){
String html = HtmlGernerator.getMessagePage("用户名或者密码为空", "login.html");
resp.getWriter().write(html);
return;
}
//2.在数据库中查找,看用户是否存在
//3.对比密码是否匹配
UserDao userDao = new UserDao();
User user = null;
try {
user = userDao.selectByName(name);
} catch (SQLException e) {
e.printStackTrace();
}
if(user == null || !password.equals(user.getPassword())){
//用户名不存在,提示错误
String html = HtmlGernerator.getMessagePage("用户名或者密码错误", "login.html");
resp.getWriter().write(html);
return;
}
//4.匹配成功则认为登录成功,创建一个Session
HttpSession httpSession = req.getSession(true);
httpSession.setAttribute("user", user);
//5.返回一个登录成功的提示页面
String html = HtmlGernerator.getMessagePage("登陆成功", "article");
resp.getWriter().write(html);
}
}
3.addServlet
package API;
import model.Article;
import model.ArticleDao;
import model.User;
import view.HtmlGernerator;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
/**
* @program: blogDemo
* @description
* @author: wenwen
* @create: 2021-08-16 17:16
**/
public class addServlet extends HttpServlet {
//实现新增文章的逻辑
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html; charset=utf-8");
//1.判定用户的登录状态,如果用户尚未登录,提示用户登录
HttpSession httpSession = req.getSession(false);
if(httpSession == null){
String html = HtmlGernerator.getMessagePage("您尚未登录,请进行登录", "login.html");
resp.getWriter().write(html);
return;
}
User user = (User)httpSession.getAttribute("user");
//2.从请求中读取浏览器提交的数据(title content),并进行简单的校验
String title = req.getParameter("title");
String content = req.getParameter("content");
if (title == null || "".equals(title) ||
content == null || "".equals(content)) {
String html = HtmlGernerator.getMessagePage("提交的标题或者正文为空", "AddArticle.html");
resp.getWriter().write(html);
return;
}
//3.把数据插入到数据库中
ArticleDao articleDao = new ArticleDao();
Article article = new Article();
article.setTitle(title);
article.setContent(content);
article.setUserId(user.getUserId());
try {
articleDao.add(article);
} catch (SQLException e) {
e.printStackTrace();
}
//4.返回一个插入成功的页面
String html = HtmlGernerator.getMessagePage("发布成功!!", "article");
resp.getWriter().write(html);
return;
}
}
4.ArticleServlet
package API;
import model.Article;
import model.ArticleDao;
import model.User;
import model.UserDao;
import view.HtmlGernerator;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
/**
* @program: blogDemo
* @description
* @author: wenwen
* @create: 2021-08-16 09:23
**/
public class ArticleServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html; charset=utf-8");
//1.验证用户是否已经登录了,如果尚未登陆,就提示用户需要先登录
HttpSession httpSession = req.getSession(false);
if(httpSession == null){
//当前未登录
String html = HtmlGernerator.getMessagePage("请先登录", "login.html");
resp.getWriter().write(html);
return;
}
User user = (User)httpSession.getAttribute("user");
//2.判断请求中是否存在 articleId,
String articleIdStr = req.getParameter("articleId");
if(articleIdStr == null) {
//a)如果没有这个参数,就执行获取文章参数列表操作
try {
getAllArticle(user, resp);
} catch (SQLException e) {
e.printStackTrace();
}
}else{
//b)如果有这个参数就执行获取文章详情操作
try {
int articleId = Integer.parseInt(articleIdStr);
getOneArticle(articleId,user,resp);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private void getAllArticle(User user, HttpServletResponse resp) throws SQLException, IOException {
//1.查找数据库
ArticleDao articleDao = new ArticleDao();
List<Article> articles = articleDao.selectAll();
//2.构造页面
String html = HtmlGernerator.getArticleListPage(articles,user);
resp.getWriter().write(html);
}
private void getOneArticle(int articleId,User user,HttpServletResponse resp) throws IOException, SQLException {
//1.查找数据库
ArticleDao articleDao = new ArticleDao();
Article article = articleDao.selectById(articleId);
if(article == null){
//文章未找到
String html = HtmlGernerator.getMessagePage("文章不存在", "article");
resp.getWriter().write(html);
return;
}
//2.根据作者id 找到作者信息,得到作者姓名
UserDao userDao = new UserDao();
User author = userDao.selectById(article.getUserId());
//3.构造页面
String html = HtmlGernerator.getArticleDetailPage(article,user,author);
resp.getWriter().write(html);
}
}
5.AddArticle.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>发布文章</title>
</head>
<body>
<form method="POST" action="addServlet">
<input type="text" name = "title" placeholder="请输入标题">
<br>
<input type="textarea" name = "content">
<br>
<input type="submit" value="发布文章">
</form>
</body>
</html>
6.login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<form method="POST" action="login">
<input type="text" name = "name" placeholder="请输入用户名">
<br/>
<input type="password" name = "password" placeholder="请输入密码">
<br/>
<input type="submit" value="登录">
<br/>
</form>
</body>
</html>
7.register.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<form method="POST" action="register">
<input type="text" name = "name" placeholder="请输入用户名">
<br/>
<input type="password" name = "password" placeholder="请输入密码">
<br/>
<input type="submit" value="注册">
<br/>
</form>
</body>
</html>