返回用户所有信息
This commit is contained in:
@@ -277,5 +277,19 @@ public class UserDao {
|
||||
|
||||
return label;
|
||||
}
|
||||
public userEntity selectUserByName(String name )
|
||||
{
|
||||
userEntity userEntity;
|
||||
try {
|
||||
Connection conn = JdbcUtils.getConnection();
|
||||
QueryRunner runner = new QueryRunner();
|
||||
String sql = "select * from user where userName=? and isValid = 1";
|
||||
userEntity = runner.query(conn, sql, new BeanHandler<>(userEntity.class),name);
|
||||
userEntity.setUserPassword("*");
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return userEntity;
|
||||
}
|
||||
}
|
||||
|
@@ -64,4 +64,6 @@ public class UserService {
|
||||
public int contributionValueReturn(String name){return userDao.contributionValueReturn(name);}
|
||||
|
||||
public int addLabel(String username,String str){return userDao.addLabel(username,str);}
|
||||
|
||||
public userEntity selectUserByName(String name ){return userDao.selectUserByName(name);}
|
||||
}
|
||||
|
74
src/com/hellogithub/servlet/retUserServlet.java
Normal file
74
src/com/hellogithub/servlet/retUserServlet.java
Normal file
@@ -0,0 +1,74 @@
|
||||
package com.hellogithub.servlet;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.hellogithub.entity.userEntity;
|
||||
import com.hellogithub.service.UserService;
|
||||
import jakarta.servlet.ServletConfig;
|
||||
import jakarta.servlet.ServletException;
|
||||
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;
|
||||
|
||||
public class retUserServlet extends HttpServlet {
|
||||
private UserService userService = new UserService();
|
||||
@Override
|
||||
public void init(ServletConfig config) throws ServletException {
|
||||
|
||||
}
|
||||
|
||||
@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");
|
||||
resp.setContentType("text/html; charset=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();
|
||||
userEntity userEntity= null;
|
||||
userEntity=(userEntity)session.getAttribute("user");
|
||||
if(userEntity ==null)
|
||||
{
|
||||
setResultError("用户未登录",writer);
|
||||
}
|
||||
else{
|
||||
String jsonString = JSONObject.toJSONString(userService.selectUserByName(userEntity.getUserName()));
|
||||
writer.println(jsonString);
|
||||
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