文章发布
This commit is contained in:
@@ -137,4 +137,28 @@ public class articleDao {
|
||||
}
|
||||
return entity;
|
||||
}
|
||||
wen
|
||||
public boolean insertArticle(int userid,String articleContent,String articleTitle,String articleIco){
|
||||
Connection conn = null;
|
||||
PreparedStatement preparedStatement = null;
|
||||
String sql = "INSERT INTO article(articleId,userId,articleContent,publishTime,isValid,readCount,articleTitle,articleIco)\n" +
|
||||
" VALUES(NULL,?,?,?,1,0,?,?);";
|
||||
Date time= new java.sql.Date(new java.util.Date().getTime());
|
||||
boolean bool = false;
|
||||
try{
|
||||
conn = JdbcUtils.getConnection();
|
||||
preparedStatement = conn.prepareStatement(sql);
|
||||
preparedStatement.setInt(1,userid);
|
||||
preparedStatement.setString(2, articleContent);
|
||||
preparedStatement.setDate(3, time);
|
||||
preparedStatement.setString(4,articleTitle);
|
||||
preparedStatement.setString(5, articleIco);
|
||||
|
||||
bool = preparedStatement.execute();
|
||||
JdbcUtils.closeConnection(preparedStatement, conn);
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return bool;
|
||||
}
|
||||
}
|
@@ -81,4 +81,9 @@ public class articleService {
|
||||
return JSONObject.toJSONString(dataMap);
|
||||
}
|
||||
|
||||
public boolean insertArticle(int userid,String articleContent,String articleTitle,String articleIco){
|
||||
boolean bool = articleDao.insertArticle(userid, articleContent, articleTitle, articleIco);
|
||||
return bool;
|
||||
}
|
||||
|
||||
}
|
71
src/com/hellogithub/servlet/article/ArticlePublish.java
Normal file
71
src/com/hellogithub/servlet/article/ArticlePublish.java
Normal file
@@ -0,0 +1,71 @@
|
||||
package com.hellogithub.servlet.article;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.hellogithub.service.UserService;
|
||||
import com.hellogithub.service.articleService;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.annotation.WebServlet;
|
||||
import jakarta.servlet.http.HttpServlet;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.servlet.http.HttpSession;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.HashMap;
|
||||
|
||||
@WebServlet("/articlepublish")
|
||||
public class ArticlePublish extends HttpServlet {
|
||||
protected articleService articleservice = new articleService();
|
||||
private UserService userservice = new UserService();
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
doPost(req,resp);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
resp.setCharacterEncoding("UTF-8");
|
||||
// 设置响应头允许ajax跨域访问
|
||||
String curOrigin = req.getHeader("Origin");
|
||||
resp.setHeader("Access-Control-Allow-Origin", curOrigin == null ? "true" : curOrigin);
|
||||
resp.setHeader("Access-Control-Allow-Credentials", "true");
|
||||
resp.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, HEAD");
|
||||
resp.setHeader("Access-Control-Max-Age", "3600");
|
||||
resp.setHeader("Access-Control-Allow-Headers", "access-control-allow-origin, authority, content-type, version-info, X-Requested-With");
|
||||
resp.setContentType("application/json;charset=UTF-8");
|
||||
|
||||
PrintWriter writer = resp.getWriter();
|
||||
HttpSession session = req.getSession();
|
||||
if(session == null){
|
||||
setResultError("用户未登录", writer);
|
||||
writer.close();
|
||||
}else{
|
||||
String username = req.getParameter("username");
|
||||
String articlecontent = req.getParameter("articleContent");
|
||||
String articletitle = req.getParameter("articleTitle");
|
||||
String articleico = req.getParameter("articleIco");
|
||||
int userid = userservice.selectIdByName(username);
|
||||
Boolean bool = articleservice.insertArticle(
|
||||
userid,articlecontent,articletitle,articleico
|
||||
);
|
||||
if(bool == true){
|
||||
setResultOk("发布成功",writer);
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
public void setResult(Integer code, String msg, PrintWriter writer) {
|
||||
HashMap<String, Object> result = new HashMap<>();
|
||||
result.put("code", code);
|
||||
result.put("msg", msg);
|
||||
String jsonString = JSONObject.toJSONString(result);
|
||||
writer.println(jsonString);
|
||||
writer.close();
|
||||
}
|
||||
public void setResultError(String msg, PrintWriter writer) {
|
||||
setResult(500, msg, writer);
|
||||
}
|
||||
public void setResultOk(String msg, PrintWriter writer) {setResult(200, msg, writer);}
|
||||
|
||||
}
|
Reference in New Issue
Block a user