[JSP] User information interface operation ---- delete user



User information interface operation-delete user

Ⅰ. Modify userinfo.jsp

  • When designing the operation content in userinfo.jsp before, there was no specific implementation. Let’s implement a simple user deletion operation today.
  • href="DeleteServlet?username=<%=resultSet.getString(2)%>"Here, a hyperlink is used to transfer the name of the user to be deleted from the current interface to the Servlet for processing.
<td>
    <%--问号右边表示要传递的内容--%>
    <a href="DeleteServlet?username=<%=resultSet.getString(2)%>" style="color: red">删除</a>
    |
    <a href="#" style="color:blue">修改</a>
    |
    <a href="#" style="color:blue">修改头像</a>
</td>

Back to top


Ⅱ. Improve the dbHelper class and add user deletion methods

/**
* userinfo.jsp页面操作之删除用户
* @param username
* @return
*/
public int deleteUser(String username){
    
    
   try {
    
    
       String sql = "delete from userinfo_copy1 where username=?";
       preparedStatement = connection.prepareStatement(sql);
       preparedStatement.setString(1,username);
       int num = preparedStatement.executeUpdate();
       if (num>0){
    
    
           return 1;
       }else {
    
    
           return 2;
       }
   } catch (Exception e){
    
    
       e.printStackTrace();
       return 0;
   }
}

Back to top


Ⅲ. Create DeleteServlet to realize the user deletion function

  • When implementing the user deletion function, we have to consider a question: can we delete the currently logged-in user? The answer is definitely no, then how should we proceed to the interpretation of the statement?
  • Here we use session for operation processing:
    • In web development, the B/S architecture is used. The browser and the server are directly connected, and a session object will be automatically created on the server side.

    • session.setAttribute(“username”,username);It is to save the username in the session, the key value of the session is "username", and the value value is the real value or reference value of the username. Then session.getAttribute(“username”)get this object through the method of.

For example:
When the user has logged in to the system, the session will store a user information object. After that, you can retrieve this object from the session at any time to perform some operations, such as performing identity verification and getting the user's account information.

  • 1. Itrequest.getSession()
    can help you get the HttpSession type object, usually called the session object. The scope of the session object is a session. Usually, the saved value will not disappear if the browser is not closed. Of course, you can also set the effective time of the session. Set the session timeout in the server. There is a session time out in web.xml. Tomcat defaults to 30 minutes.

  • 2. Itsession.setAttribute(“user”,userName); is the method of session setting value, the principle is similar to the key-value pair of HashMap in java, meaning that the key is now "user"; the stored value is userName, userName should be a String variable? See your own definition.

  • 3.session.getAttribute(“key”); You can use to get the value, thinking you can get the value of userName.

  • 4. Note: .getAttribute()The return value type is Object, which needs to be downcast and converted to your userName type . Simply put, what is stored, or what is taken out! ! ! It means that it needs to be cast, or it is declared as an object class.

  • 5, .setAttribute()and.getAttribute()
    it is based on the realization of a HashMap put and get methods, generally called key-value pairs or key-value, that is, to find the key values. For example, the relationship between your name and your person, as long as you call your name, you will yell, people who find you by your name, simply put this is the concept of key-value pairs.

  • Here how do we get the name information of the currently logged in user? You can store the name information of the currently logged-in user when logging in, and make judgments when deleting.
    Insert picture description here
    Note: Operations 3 and 4 in the above figure must be present, and 1, 2 can be absent.
package com.zte;

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.io.PrintWriter;

@WebServlet(name = "DeleteServlet",urlPatterns = "/DeleteServlet")
public class DeleteServlet extends HttpServlet {
    
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
        // 对相应结果的编码方式设置
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        // 内容输出
        PrintWriter out = response.getWriter();

        // 设定选定的参数
        String username = request.getParameter("username");

        // 创建dbHelper对象
        dbHelper db = new dbHelper();
        // 获取Session
        HttpSession session = request.getSession();
        // 首先判断删除的用户是否为当前登录的用户
        if(!username.equals(session.getAttribute("LoginUser"))) {
    
    
            switch (db.deleteUser(username)) {
    
    
                case 0:
                    out.println("<script>");
                    out.println("alert('系统错误');");
                    out.println("window.location='userinfo.jsp';");
                    out.println("</script>");
                    break;
                case 1:
                    out.println("<script>");
                    out.println("alert('删除成功');");
                    out.println("window.location='userinfo.jsp';");
                    out.println("</script>");
                    break;
                case 2:
                    out.println("<script>");
                    out.println("alert('删除失败');");
                    out.println("window.location='userinfo.jsp';");
                    out.println("</script>");
                    break;
            }
        }else{
    
    
            out.println("<script>");
            out.println("alert('当前用户不可删除!');");
            out.println("window.location='userinfo.jsp';");
            out.println("</script>");
        }
        // 刷新、结束
        out.flush();
        out.close();
    }
}

Back to top


Ⅳ. Effect display

① Delete the currently logged-in user

Insert picture description here
Insert picture description here

② Delete non-current login users

Insert picture description here
Insert picture description here
Insert picture description here

Back to top


Previous article: After login successfully, the user information interface will display basic user information.                                                        Next article: User information interface operation-user information modification

Guess you like

Origin blog.csdn.net/qq_45797116/article/details/113447760