【JavaEE】前后端综合项目-博客系统(下)
文章目录
- 【JavaEE】前后端综合项目-博客系统(下)
- 1. 博客登录页
- 1.1 用户名密码校验
- 1.1.1 约定前后端交互接口
- 1.1.2 后端代码
- 1.1.3 前端代码
- 1.2 登录信息记忆功能
- 1.2.1 约定前后端交互接口
- 1.2.2 后端代码
- 1.2.3 前端代码
- 1.3 显示用户/作者信息功能
- 1.3.1 约定前后端交互接口
- 1.3.2 后端代码
- 1.3.3 前端代码
- 1.4 退出登录功能
- 1.4.1 前后端交互接口
- 1.4.2 后端代码
- 1.4.3 前端代码
- 2. 博客编辑页
- 2.1 前后端交互接口
- 2.2 后端代码
- 2.3 前端代码
紧接上文:【JavaEE】前后端综合项目-博客系统(上)_s:103的博客-CSDN博客
请求:POST
向谁请求:/login
响应:302(重定向)location:博客列表页
请求的正文格式:application/x-www-from-urlencoded
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf8");
resp.setContentType("text/html;charset=utf8");
String username = req.getParameter("username");
String password = req.getParameter("password");
if(username == null || username.replaceAll(" ", "").equals("")
|| password == null || password.replaceAll(" ", "").equals("")) {
String html = "登录失败
";
resp.getWriter().write(html);
return;
}
UserDao userDao = new UserDao();
User user = userDao.selectUserByName(username);
if(user == null) {
String html = "登录失败
";
resp.getWriter().write(html);
}else {
if(user.getPassword().equals(password)) {
HttpSession httpSession = req.getSession(true);
httpSession.setAttribute("user", user);
resp.sendRedirect("博客列表页.html");
}else {
String html = "登录失败
";
resp.getWriter().write(html);
}
}
}
将html文件的名字改文英文,并且其他关联代码都需要更改!
resp.sendRedirect("blog_list.html");
要求:
请求:GET
向谁请求:/login
响应:成功:200,失败:403
ObjectMapper objectMapper = new ObjectMapper();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession(false);
if(session == null) {
resp.setStatus(403);
return;
}
User user = (User) session.getAttribute("user");
if(user == null) {
resp.setStatus(403);
return;
}
resp.setStatus(200);
resp.setContentType("application/json; charset=utf8");
String ret = objectMapper.writeValueAsString(user);
resp.getWriter().write(ret);
}
其他三个页面
jQuery.ajax({
type:"get",
url:"login",
error: function(body) {
location.href = "blog_login.html";
}
});
登录页
jQuery.ajax({
type: "get",
url: "login",
success: function(body) {
location.href = "blog_list.html";
alert("欢迎" + body.username + "!");
}
});
要求:
对于列表页,复用一下刚才的后端代码即可,因为成功的时候,会返回用户信息user的json字符串
而对于详情页,则需要多一段逻辑:通过博客id,确定用户id
请求:GET
向谁请求:/user&blogId=xxx
响应:这篇博客作者的用户信息(json)
ObjectMapper objectMapper = new ObjectMapper();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf8");
String id = req.getParameter("blogId");
if(id == null) {
resp.setStatus(404);
return;
}
BlogDao blogDao = new BlogDao();
Blog blog = blogDao.selectOne(Integer.parseInt(id));
if(blog == null) {
resp.setStatus(404);
return;
}
UserDao userDao = new UserDao();
User user = userDao.selectUserById(blog.getUserId());
if(user == null) {
resp.setStatus(404);
return;
}
user.setPassword("");//返回密码有点不安全~
resp.setContentType("application/json; charset=utf8");
String ret = objectMapper.writeValueAsString(user);
resp.getWriter().write(ret);
}
jQuery.ajax({
type:"get",
url:"user" + location.search,
success: function(body) {
jQuery(".card h3").text(body.username);
jQuery(".card img").attr("src", body.image);
jQuery(".card a").attr("href", body.git);
},
error: function(body) {
location.href = "blog_list.html";
alert("error");
}
});
为了有区分,我们在数据库里添加几个user
insert into user values(null, '小空多尼', '123456', '马铭胜帅照.png', 'https://gitee.com/bitpg/projects'); insert into user values(null, '小空你几哇', '123456', '微信图片_20230225230636.png', 'https://gitee.com/HGtz2222'); insert into user values(null, '小斯密码塞', '123456', 'JavaEE.png', 'https://gitee.com/gaobo1');
要求:
请求:GET
向谁请求:/logout
响应:重定向到博客登录页
而请求只需要让a标签发出即可
@WebServlet("/logout")
public class logoutServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession(false);
if(session != null) {
session.removeAttribute("user");
//session.invalidate();也可以,但是这个是把整个session失效
}
resp.sendRedirect("blog_login.html");
}
}
无非就是将所编辑的博客打包成blog存进数据库呗,其他都好办,除了博客的内容,是在md编辑器里的一个元素的内容,所以要通过一些特殊方法(官方给出的方法)
请求:POST
向谁请求:/blog
请求正文:application/x-www-from-urlencoded
响应:非空校验后重定向到主页,否则显示提交失败
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf8");
HttpSession session = req.getSession(false);
String content = req.getParameter("content");
String title = req.getParameter("title");
if(session == null ||
title == null || title.replaceAll(" ", "").equals("") ||
content == null || content.replaceAll(" ", "").equals("")
) {
resp.setContentType("text/html;charset=utf8");
resp.getWriter().write("提交失败!
");
return;
}
User user = (User) session.getAttribute("user");
Blog blog = new Blog();
blog.setTitle(title);
blog.setPostTime(new Timestamp(System.currentTimeMillis()));
blog.setContent(content);
blog.setUserId(user.getUserId());
BlogDao blogDao = new BlogDao();
blogDao.insert(blog);
resp.sendRedirect("blog_list.html");
}
补充,导航栏的头像我不更改,那是系统的创作者的头像/logo,所以不更改
文章到此结束!谢谢观看
可以叫我 小马,我可能写的不好或者有错误,但是一起加油鸭!后续推出更多功能,敬请期待!
通过这个项目可以很好的提高我们对前后端交互的认知!
本文代码链接:BlogSystem · 游离态/马拉圈2023年7月 - 码云 - 开源中国 (gitee.com)