【问题标题】:updating user session from backend requests从后端请求更新用户会话
【发布时间】:2020-02-04 22:24:40
【问题描述】:

我有一个在会话中存储用户数据的应用。每当我在后端对用户进行更改时,我必须记住还要更新会话数据。这很好,除非我对不是由他的操作发起的用户进行更改。然后我无法访问他的请求,因此无法访问他的会话。这意味着用户不会看到对其用户记录的更新,更糟糕的是,如果他在会话更新之前对用户记录进行了更改,他可能会清除系统所做的更改。

我唯一的解决方案是在保存更改时从数据存储区而不是会话中提取。这样我就永远不会用过时的数据覆盖好的数据。

如果 stackoverflow 的代码与我的网站类似:

用户声誉编号存储在会话中,如果有人对他们的答案之一进行投票,他们在数据存储中的声誉就会增加,但这不会传播到会话。用户现在可以更新他们的 about 字段并消除他们刚刚获得的声誉。

希望这一切都有意义,请随时询问我可能错过的任何细节。

【问题讨论】:

    标签: java google-app-engine session


    【解决方案1】:

    在你的 JSP 顶部插入这个来识别会话中的用户

    if (session!=null && request.getSession().getAttribute("loggedin") != null) {
        if (request.getSession().getAttribute("role").equals("STUDENT")) {
            response.sendRedirect("index.jsp");
            return;
        } 
    
    }else{
        response.sendRedirect("index.jsp");
        return;
    }%>
    <% if (request.getSession().getAttribute("loggedin") == null) {
        response.sendRedirect("index.jsp");
        return;
    }
    StudentDTO dto = (StudentDTO) request.getSession().getAttribute("user");
    

    然后在下面的 JSP 中插入这段代码以将新数据发布到 servlet

    <form method="POST" action="EditUserServlet">
    
                        <div class="form-submit">   
                            My Information
                            <div class="submit-section">
                                <div class="form-row">
    
                                    <div class="x">
                                        <label>Name</label>
                                        <input type="text" class="form-control" value="<%= dto.getName()%>" name="name">
                                    </div>
    
                                    <div class="x">
                                        <label>Email</label>
                                        <input type="email" class="form-control" value="<%= dto.getEmail()%>" name="email">
                                    </div>
    
                                <div class="form-group x">
                                    <button class="btn" type="submit">Save Changes</button>
                                </div>
                            </div>
                        </div>
                    </form>
    

    在 EditUserServlet 中

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    
        try {
             StudentDB db = new StudentDB();
    
            StudentDTO dto = db.getStudentByID(((StudentDTO) request.getSession().getAttribute("user")).getId());
    
            dto.setName(request.getParameter("name"));
            dto.setEmail(request.getParameter("email"));
    
    
    
    
            db.updateStudent(dto);
            request.getSession().setAttribute("user", dto);
    

    在 StudentDB java 文件中插入这个以通过 Id 查找学生,然后更新学生 sql 文件

    public StudentDTO getStudentByID(int id) {
    
        StudentDTO obj = null;
        String query = "Select * from students where id=?";
        PreparedStatement pst = null;
        ResultSet rs = null;
    
        try {
            pst = conn.prepareStatement(query);
            pst.setInt(1,id);
            rs = pst.executeQuery();
            if (rs != null) {
                if (rs.next()) {
    
                    obj = new StudentDTO();
                    obj.setId(rs.getInt(1));
                    obj.setName(rs.getString(2));
                    obj.setEmail(rs.getString(3));
                    obj.setPassword(rs.getString(4));
    
    } catch (SQLException ex) {
            Logger.getLogger(StudentDB.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException ex) {
                    Logger.getLogger(StudentDB.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
    
    public boolean updateStudent(StudentDTO obj)
    {
        int affectedRows = 0;
        String query = "update `student` set name=? , email=lower(?)   where id=?";
        PreparedStatement pst = null;
        try {
             pst = conn.prepareStatement(query);
            pst.setString(1,obj.getName());
            pst.setString(2,obj.getEmail());
    
    
    
            pst.setInt(10, obj.getId());
            System.out.println(pst);
    
            affectedRows = pst.executeUpdate();
    
        } catch (SQLException ex) {
            Logger.getLogger(StudentDB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return affectedRows > 0;
    }
    

    【讨论】:

      【解决方案2】:

      为什么要将数据存储在会话中,IMO 您可以直接将其映射到数据存储区,这样您就不会拥有陈旧的旧数据,并且每次获取数据时都会更新数据源

      【讨论】:

      • 出于几个原因,我将用户存储在会话中,一个是确保用户有权执行他们正在尝试执行的操作。我在更改期间在会话中使用用户,因此我不必调用数据存储,因为它已经可用。
      【解决方案3】:

      你熟悉四人组观察者模式吗? 也没有完全遵循您的用例...

      【讨论】:

      • 观察者模式可以完美运行,但是当需要更新会话时,我将如何访问它。通常更新会话我做 request.getSession().setAtribute("user", userObject);如果我在不是由用户生成的请求中,我不能这样做。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-09
      • 2019-03-17
      • 2017-02-23
      相关资源
      最近更新 更多