1.创建maven项目:
1)我们进行引入Servlet的依赖,引入Thymeleaf和MYSQL的依赖
2)在main目录下创建webapp目录,在webapp目录里面创建web.xml文件,在webapp里面创建WEB-INF目录,再从WEB-INF里面创建template目录,把我们之前写的博客系统的前端页面全部拷贝过来
3)使用Smart-Tomact进行打包部署
2.设计数据库
我们要把博客信息和用户的身份信息给储存到数据库里面
1)我们进行创建博客表
先创建数据库----Java200,博客列表:博客ID,博客标题,博客正文(在这里面使用mediumetext表示的数据范围要比varchar要长一些),博客发布时间,一篇博客对应的作者(用一个ID来进行标识)
创建博客表
create table blog(
blogID int primary key auto_increment,//每一篇博客的身份标识
title varchar(1024),//每一篇博客的文章
content mediumtext,//每一篇博客的正文
postTime datetime,//每一篇博客的发布时间
userID int);//每一篇博客的作者
2)我们进行创建用户表
用户的身份标识ID,用户的姓名,用户的密码,况且我们的用户表里面的用户身份标识ID和博客表中的userID也是可以用外键来进行关联的;
create table user(
userID int,
name varchar(20),
password varchar(30));
3)封装数据库操作,JDBC代码,下面我们来写一些MYSQL中的常用方法
我们在Java里面创建一个包叫做MYSQL
1)我们创建第一个类,叫做ConnectionDatabase,里面存放JDBC链接MYSQL的代码,之前都写过
package MYSQL;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class ConnectionDatabase {
private static final String url="jdbc:mysql://127.0.0.1:3306/java200?characterEncoding=utf-8&userSSL=true";
private static final String password="12503487";
private static final String user="root";
public static DataSource datasource=null;
public static DataSource getDataSource()
{
if(datasource==null)
{
synchronized(Object.class)
{
if(datasource==null)
{
datasource=new MysqlDataSource();
((MysqlDataSource)datasource).setURL(url);
((MysqlDataSource)datasource).setUser(user);
((MysqlDataSource)datasource).setPassword(password);
}
}
}
return datasource;
}
public static Connection getConnection() throws SQLException {
return getDataSource().getConnection();
}
public void close(ResultSet resultSet, PreparedStatement statement, Connection connection) throws SQLException {
resultSet.close();
statement.close();
connection.close();
}
}
2. 创建实体类,通过每一个实体类对象,来进行代表表中的一条记录数据;我们分别进行创建Blog这样的类和User这样的类来进行代表;类中的各种属性和数据库中的各种属性是相对应的,并针对里面的属性提供Set和Get方法-----他们都是根据数据库中的内容来进行创建的类;我们在MYSQL这个包中,创建Blog和User类;
3.在进行创建一些类,来进行通过一些JDBC代码来实现数据库的一些操作;
1.我们进行创建第一个类,来实现对Blog对象的一些操作;
package MYSQL;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class OperateBlog {
public void insert(Blog blog) throws SQLException, SQLException {
System.out.println("进行新增一篇博客");
//1与数据库建立连接
Connection connection=null;
connection=ConnectionDatabase.getConnection();
String SQL="insert into blog values(null,?,?,?,?)";
//2创建SQL语句
PreparedStatement statement=connection.prepareStatement(SQL);
statement.setString(1,blog.getTitle());
statement.setString(2,blog.getContent());
statement.setTimestamp(3,blog.getPostTime());
statement.setInt(4,blog.getUserID());
//3执行SQL语句
int ret=statement.executeUpdate();
if(ret==1)
{
System.out.println("新增博客成功");
}else{
System.out.println("新增博客失败");
}
ConnectionDatabase.close(null,statement,connection);
}
public List<Blog> SelectAll() throws SQLException {
System.out.println("开始查找所有博客");
List<Blog> list=new ArrayList<>();
Connection connection=ConnectionDatabase.getConnection();
String SQL="select * from blog";
PreparedStatement statement=connection.prepareStatement(SQL);
ResultSet set=statement.executeQuery();
//遍历里面的结构集合
while(set.next())
{
Blog blog=new Blog();
blog.setBlogID(set.getInt("blogID"));
blog.setTitle(set.getString("title"));
blog.setContent(set.getString("content"));
blog.setPostTime(set.getTimestamp("postTime"));
blog.setUserID(set.getInt("userID"));
list.add(blog);
}
ConnectionDatabase.close(set,statement,connection);
return list;
}
public Blog SelectOne(int blogID) throws SQLException {
System.out.println("开始查找指定博客");
Connection connection=null;
PreparedStatement statement=null;
connection=ConnectionDatabase.getConnection();
String SQL="select * from blog where blogID = ?";
statement=connection.prepareStatement(SQL);
statement.setInt(1,blogID);
ResultSet set=statement.executeQuery();
if(set.next()) {//条件不要写错 if(set!=null)
Blog blog = new Blog();
//遍历结果集和,此处是基于自增主键来进行查询的,要么是1条记录要么是0条记录;
blog.setBlogID(set.getInt("blogID"));
blog.setTitle(set.getString("title"));
blog.setContent(set.getString("content"));
blog.setPostTime(set.getTimestamp("postTime"));
blog.setUserID(set.getInt("userID"));
ConnectionDatabase.close(set, statement, connection);
return blog;
}
return null;
}
public void DeleteBlog(int blogID) throws SQLException {
Connection connection=null;
PreparedStatement statement=null;
connection=ConnectionDatabase.getConnection();
String SQL="delete from blog where blogID = ?";
statement=connection.prepareStatement(SQL);
statement.setInt(1,blogID);
int len=statement.executeUpdate();
if(len==1)
{
System.out.println("删除成功");
}
}
public static void main(String[] args) throws SQLException {
//1测试插入功能
Blog blog=new Blog();
blog.setUserID(1);
blog.setBlogID(1);
blog.setTitle("ABCDEFG");
blog.setContent("I am a boy");
blog.setPostTime(new Timestamp(System.currentTimeMillis()));//必须是java.sql里面的包
//setPostTime里面需要一个Timestamp类型,里面就需要new一个Timestamp对象,Timestamp里面参数是一个时间戳
// statement设置下标的时候是从0下标开始进行设置的
OperateBlog blogdemo=new OperateBlog();
blogdemo.insert(blog);
//2.测试查找所有博客功能
List<Blog> list=blogdemo.SelectAll();
// System.out.println(list);
//3查找指定博客
Blog blog1=blogdemo.SelectOne(1);
System.out.println(blog1);
}
}
2.我们进行创建第二个对象,来进行对user来进行操作
package MYSQL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class OperateUser {
public void insert(User user) throws SQLException {
Connection connection=null;
PreparedStatement statement=null;
connection=ConnectionDatabase.getConnection();
String SQL="insert into user values(?,?,?)";
statement=connection.prepareStatement(SQL);
statement.setInt(1,user.getUserID());
statement.setString(2, user.getName());
statement.setString(3, user.getPassword());
int len=statement.executeUpdate();
if(len==1)
{
System.out.println("插入成功");
}else{
System.out.println("插入失败");
}
}
public User selectByName(String username) throws SQLException {
Connection connection=null;
PreparedStatement statement=null;
connection=ConnectionDatabase.getConnection();
String SQL="select * from user where name = ?";
statement= connection.prepareStatement(SQL);
statement.setString(1,username);
ResultSet set=statement.executeQuery();
if(set.next())
{
User user=new User();
user.setPassword(set.getString("password"));
user.setName(username);
user.setUserID(set.getInt("userID"));
return user;
}
return null;
}
public User selectByUserID(int userID) throws SQLException {
Connection connection=null;
PreparedStatement statement=null;
connection=ConnectionDatabase.getConnection();
String SQL="select * from user where userID = ?";
statement=connection.prepareStatement(SQL);
statement.setInt(1,userID);
ResultSet set=statement.executeQuery();
if(set.next())
{
User user=new User();
user.setUserID(userID);
user.setName(set.getString("name"));
user.setPassword(set.getString("password"));
return user;
}
return null;
}
}
4.我们此时开始写实现页面的操作,我们让服务器基于模板引擎,来把从数据库中查找到的数据,返回到网页上面
1.进行初始化模板引擎
我们要把把初始化代码放到ServletContextListener中,然后把TemplateEngine初始化好了之后放到ServletContext里面;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener//一定要加注释
public class StartThymeleaf implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("开始进行初始化模板引擎");
//1创建TemplateEngine对象和resolver对象
ServletContext context=servletContextEvent.getServletContext();
TemplateEngine engine=new TemplateEngine();
ServletContextTemplateResolver solver=new ServletContextTemplateResolver(context);
solver.setPrefix("/WEB-INF/template/");
solver.setSuffix(".html");
solver.setCharacterEncoding("utf-8");
//2将resolver对象和engine对象进行关联
engine.setTemplateResolver(solver);
//3将我们创建好的engine对象放到ServletContext对象里面
context.setAttribute("engine",engine);
System.out.println("初始化模板引擎的操作结束");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
}
2.实现博客列表页
<!-- 右侧是博客列表页 -->
<div class="right">
<!-- 在右侧,我们表示的是一篇一篇的博客 -->
<div class="blog" th:each="blog : ${blogs}">
<!-- 文章标题 -->
<h2 class="title" th:text="${blog.title}"></h2>
<!-- 发布日期 -->
<div class="date" th:text="${blog.postTime}"></div>
<!-- 文章内容 -->
<div class="text" th:text="${blog.content}"></div>
<!-- 查看全文按钮 -->
<a href="${'blog2.html?blogID'+blog.blogID}" class="detail" target="_blank">查看全文>></a>
</div>
</div>
这里面的内容都是依靠模板渲染的,后端传递过来一个blogs数组或者list,我们就会自动遍历到这里面的每一个blog,取出里面的每一个属性,然后再去进行渲染操作;
OperateBlog operateBlog=new OperateBlog();
//1从数据库拿到所有的博客列表
List<Blog> list= null;
try {
list = operateBlog.SelectAll();
} catch (SQLException e) {
e.printStackTrace();
}
resp.setContentType("text/html;charset=utf-8");
//2拿到模板引擎对象,进行页面渲染操作
ServletContext context=req.getServletContext();
TemplateEngine engine=(TemplateEngine)context.getAttribute("engine");
//3创建WebContext对象,进行设置键值对
WebContext webContext=new WebContext(req,resp,context);
webContext.setVariable("blogs",list);
//4进行渲染
String html= engine.process("blog1",webContext);
resp.getWriter().write(html);
3.实现博客详情页
<!-- 右侧是博客详情页,这个div表示右侧版新的整个背景-->
<div class="right">
<!--这里面不用写th:each-->
<div class="blog">
<!-- 文章标题 -->
<h3 th:text="${blog.title}"></h3>
<!-- 博客发布时间 -->
<div class="date" th:text="${blog.postTime}">2022年12月3日</div>
<P th:text="${blog.content}"></P>
</div>
</div>
我们希望在博客详情页里面,能够传过来一个blog对象,找到对应的属性进行渲染
resp.setContentType("text/html;charset=utf-8");
OperateBlog operateBlog=new OperateBlog();
String blogID=req.getParameter("blogID");
if(blogID==""||blogID.equals(""))
{
String html="<h3>blogID字段缺失</h3>";
resp.getWriter().write(html);
}
Blog blog= null;
try {
blog = operateBlog.SelectOne(Integer.parseInt(blogID));
} catch (SQLException e) {
e.printStackTrace();
}
if(blog==null)
{
String html="<h3>根据博客ID获取到的当前博客不存在</h3>";
resp.getWriter().write(html);
}
ServletContext context=req.getServletContext();
TemplateEngine engine=(TemplateEngine)context.getAttribute("engine");
WebContext webContext=new WebContext(req,resp,context);
webContext.setVariable("blog",blog);
String html=engine.process("blog2",webContext);
resp.getWriter().write(html);
}
4.实现博客登陆界面
实现具体登陆页面的代码:
import MYSQL.OperateUser;
import MYSQL.User;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
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;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.获取请求中的参数
resp.setContentType("text.html;charset=utf-8");
req.setCharacterEncoding("utf-8");
String username=req.getParameter("name");
String password=req.getParameter("password");
System.out.println(username);
System.out.println(password);
if(username==null||password==null||username.equals("")||password.equals(""))
{ String html="<h3>当前请求中的用户名或者密码缺失</h3>";
resp.getWriter().write(html);
return;
}
OperateUser operateUser=new OperateUser();
//2从数据库中查询用户
User user=null;
try {
user=operateUser.selectByName(username);
} catch (SQLException e) {
e.printStackTrace();
}
//3进行校验,不存在直接重定向到登陆界面,下面是登陆失败的几种情况,查不到user或者密码不匹配
if(user==null)
{
String html="<h3>用户名错误或者密码错误</h3>";
resp.getWriter().write(html);
return;
}
if(!user.getPassword().equals(password))
{
String html="<h3>用户名错误或者密码错误</h3>";
resp.getWriter().write(html);
return;
}
//4把user对象寸放大HttpSession里面
HttpSession httpSession=req.getSession(true);
httpSession.setAttribute("user",user);
resp.sendRedirect("blog1.html");
}
}
再进行访问每一个页面之前,进行判断用户是否进行登陆的代码:
//判断当前用户是否登录
HttpSession session=req.getSession(false);
if(session==null||session.equals(""))
{
System.out.println("当前用户未登录");
//未登录状态
resp.sendRedirect("blog3.html");
return;
}
User userx= (User) session.getAttribute("user");
if(userx==null)
{
System.out.println("当前用户未登录");
//未登录状态
resp.sendRedirect("blog3.html");
return;
}
我们先进行修改博客列表页;
此处的所设置的userx是我们在刚才检查登陆状态的时候,从session里面中读的user,这个userx正是当前用户进行登陆的用户的信息;
我们每一次服务器进行重启的时候,服务器内存中的那一个保存会话的hash表也就没了,所以之前的登陆状态也就没有了(因为这个哈希表是保存在内存里面的),当下一次我们的客户端访问服务器的时候Cookie字段的中的sessionID服务器也不会认识,就只能认定是未登录状态;
但是在实际上不是所有的会话信息都在内存里面,Servlet默认的会话是在内存里面的,想要把hash表存放到文件或者数据库里面,就要通过代码进行实现;
修改博客详情页:
前端修改方式是一样的:
后端代码:
此处我们就不可以在webContext里面设置成从session里面读取到的user,而是根据我们之前再去之前在博客列表页传过来的blogID来进行查询用户user,这个user才是我们需要进行模板渲染的user;
上面写法是错误的,怎么可以根据blogID来进行查询到User呢?下面是正确的:
OperateUser operateUser=new OperateUser();
try {
User user=operateUser.selectByUserID(blog.getUserID());
webContext.setVariable("user",user);
} catch (SQLException e) {
e.printStackTrace();
}
5.实现注销功能
1)我们在这里面期望,当我们进行点击注销按钮之后,就可以退出登录状态了,我们在之前判断用户是否进行登录的时候,现根据SessionID来进行查找出HttpSession对象,再取出里面的user;
2)我们必须取到一个user才算登陆成功,取user取不到,或者说取不到session都是登陆失败;
3)当前我们的注销按钮是一个a标签,里面有一个路径,咱们可以写一个Servlet来进行处理这里面的请求,在代码中我们可以根据SessionID来进行找到储存在Session里面的User对象,然后把这个User对象给删了;在HttpSession中提供了一个removeAttribute()这样的方法;然后直接重定向到登陆界面即可;
4)我们在服务器这边在java目录里面创建deleteServlet来进行处理注销请求;
<a href="delete" target="_blank">注销</a>
import MYSQL.User;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/delete")
public class deleteServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//判断当前用户是否登录
HttpSession session=req.getSession(false);
if(session==null||session.equals(""))
{
System.out.println("当前用户未登录");
//未登录状态
resp.sendRedirect("blog3.html");
return;
}
User userx= (User) session.getAttribute("user");
if(userx==null)
{
System.out.println("当前用户未登录");
//未登录状态
resp.sendRedirect("blog3.html");
return;
}
resp.setContentType("text/html;charset=utf-8");
session.removeAttribute("user");
//重定向登陆界面
resp.sendRedirect("blog3.html");
}
}
6.实现博客发布功能
前端代码:
<!-- 这就是博客编辑页的版心 -->
<form action="edit" method="post">
<div class="edit">
<div class="writetitle">
<input type="text" id="title" name="title">
<input type="submit" value="点击发布文章" id="submit">
</div>
<div id="editor">
<textarea name="content" style="display:none"></textarea>
</div>
</form>
</div>
<script>
//首先要初始化编译器
//先创建出一个textinner对象
//editormd相当于是一个函数,生成一个editor对象
//第一个参数是要指定放到哪一个html标签中,元一个html标签关联,参数就是对应的ID;
var editor=editormd("editor", {
//这里的尺寸必须在这里设置,不能依靠CSS进行设置
width:"100%",
// 这是计算makdown正文的高度
height:"1000px",
//编辑器的初始内容
markdown:"#在这里写一下第一篇博客",
//编译器所用的插件所在的路径
path:"editor.md/lib/",
saveHTMLToTextarea:true
});
后端代码:
import MYSQL.Blog;
import MYSQL.OperateBlog;
import MYSQL.User;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
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.sql.Timestamp;
@WebServlet("/edit")
public class editServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//判断当前用户是否登录
HttpSession session=req.getSession(false);
if(session==null||session.equals(""))
{
System.out.println("当前用户未登录");
//未登录状态
resp.sendRedirect("blog3.html");
return;
}
User userx= (User) session.getAttribute("user");
if(userx==null)
{
System.out.println("当前用户未登录");
//未登录状态
resp.sendRedirect("blog3.html");
return;
}
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//2获取请求中的文章标题和文章内容
String content=req.getParameter("content");
String title=req.getParameter("title");
if(content==null||content.equals("")||title==null||title.equals(""))
{
String html="<h3>发布博客失败,文章标题或者文章内容是空</h3>";
resp.getWriter().write(html);
return;
}
//3构建一个blog对象,插入到数据库里面
Blog blog=new Blog();
blog.setContent(content);
//注意当前的文章作者正是当前的登陆者
blog.setUserID(userx.getUserID());
blog.setTitle(title);
blog.setPostTime(new Timestamp(System.currentTimeMillis()));
OperateBlog operateBlog=new OperateBlog();
try {
operateBlog.insert(blog);
} catch (SQLException e) {
e.printStackTrace();
}
//4重定向到博客列表页
resp.sendRedirect("blog1.html");
}
}
7.当前我们发现,博客详情页显示的是博客的原始的Markdown内容,而不是渲染后的markdown,一级标题,二级标题都没有进行显示出来
一个#会被渲染成一级标题,三个#会被渲染成三级标题(左侧写文字,右边显示具体渲染后的结果)
我们此时要针对我们的博客详情页进行调整,同时也需要在这个页面中引入editor.md这个库,然后在这个库里面有一个操作markdownHTML,这个方法的作用是把原始的markDown文本给进行渲染成一个HTML
<script>
//首先要初始化编译器
//先创建出一个textinner对象
//editormd相当于是一个函数,生成一个editor对象
//第一个参数是要指定放到哪一个html标签中,元一个html标签关联,参数就是对应的ID;
var editor=editormd("editor", {
//这里的尺寸必须在这里设置,不能依靠CSS进行设置
width:"100%",
// 这是计算makdown正文的高度
height:"1000px",
//编辑器的初始内容
markdown:"#在这里写一下第一篇博客",
//编译器所用的插件所在的路径
path:"editor.md/lib/",
saveHTMLToTextarea:true
});
</script>
在博客详情页里面,有一个代码:
<P th:text="${blog.content}"></P>这个div里面就包含了markDown的原始报文
markdown的原始内容,就在这个div里面,我们就可以把这个div里面的内容给取出来,然后再通过刚才的markdownToHTML方法(这个方法在script标签里面)进行转换,然后再把转换的结果给些回到刚才的这个div中;
<div id="content"th:text="${blog.content}"></div>
<script>
function rendMo(){
//1先取出div里面的内容
let markDownDiv=document.querySelector("#content");
let markdown=markDownDiv.innerHTML;
console.log(markdown);
//2把div里面的内容设成空
markDownDiv.innerHTML="";
//3进行替换
editormd.markdownToHTML('content',{
markdown:markdown
});//第一个参数表示ID
}
rendMo();
</script>
8.我们完成了上述的调整之后,就发现当前这个代码确实可以运行了也可以进行渲染出来了,但是这里面的背景和之前的半透明效果,显得格格不入;
此处可以给刚才的div设置一个纯透明背景(back-groundcolor里面的rgbc是可以进行设置透明度的;
<div id="content"th:text="${blog.content}" style="background-color:transparent;"></div>
这个style属性表示完全透明,看到的背景就是一个父亲元素的背景;
之前我们的博客列表这里面不太科学,因为新的博客出现在最下边,一般是新的博客在上边,我们就修改了数据库中的查找所有博客的查询语句,加上order by postTime desc属性
9.我们进行实现删除博客的功能
<a target="_blank" th:if="${showdeleteButton}"th:href="${'deleteblog?blogID='+blog.blogID}"></a>
1)我们在前端加上这样的代码,首先我们肯定要进行判断当前进行登录的用户和文章作者的用户是不是相同的,就用th:if来进行判断,如果相同,就把showdeleteButton设成true,并显示在页面上;否则就会设置成false;
2)当我们进行点击a标签的时候,传递过来的参数肯定要带上blogID这样我们才可以在后面的服务器上面根据blogID来进行删除;
我们在blog2Servlet中修改的代码:
webContext.setVariable("showdeleteButton",user.getUserID()==userx.getUserID());
我们用的deleteblogServlet中的代码
import MYSQL.Blog;
import MYSQL.OperateBlog;
import MYSQL.OperateUser;
import MYSQL.User;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
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;
@WebServlet("/blog2.html")
public class Blog2Servlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
//判断当前用户是否登录
HttpSession session=req.getSession(false);
if(session==null||session.equals(""))
{
System.out.println("当前用户未登录");
//未登录状态
resp.sendRedirect("blog3.html");
return;
}
User userx= (User) session.getAttribute("user");
if(userx==null)
{
System.out.println("当前用户未登录");
//未登录状态
resp.sendRedirect("blog3.html");
return;
}
resp.setContentType("text/html;charset=utf-8");
OperateBlog operateBlog=new OperateBlog();
String blogID=req.getParameter("blogID");
if(blogID==""||blogID.equals(""))
{
String html="<h3>blogID字段缺失</h3>";
resp.getWriter().write(html);
}
Blog blog= null;
try {
blog = operateBlog.SelectOne(Integer.parseInt(blogID));
} catch (SQLException e) {
e.printStackTrace();
}
if(blog==null)
{
String html="<h3>根据博客ID获取到的当前博客不存在</h3>";
resp.getWriter().write(html);
}
ServletContext context=req.getServletContext();
TemplateEngine engine=(TemplateEngine)context.getAttribute("engine");
WebContext webContext=new WebContext(req,resp,context);
OperateUser operateUser=new OperateUser();
try {
User user=operateUser.selectByUserID(blog.getUserID());
webContext.setVariable("user",user);
webContext.setVariable("showdeleteButton",user.getUserID()==userx.getUserID());
} catch (SQLException e) {
e.printStackTrace();
}
webContext.setVariable("blog",blog);
String html=engine.process("blog2",webContext);
resp.getWriter().write(html);
}
}