JSP+Servlet+Tomcat+Mysql实现注册、登陆、修改、添加、查看、分页实例之----密码修改

1、新建页面reset.jsp

<%--
  Created by IntelliJ IDEA.
  User: lvjun
  Date: 2018-03-22
  Time: 10:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>密码修改</title>
    </head>
    <body>
        <div align="center">
            ${message}
            <form action="reset" method="post">
                <table border="1">
                    <tr>
                        <td>账号</td>
                        <td><input type="text" name="uname"></td>
                    </tr>
                    <tr>
                        <td>密码</td>
                        <td><input type="password" name="upwd"></td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center"><input type="submit" value="修改"></td><!--参数提交doPost-->
                    </tr>
                </table>
            </form>
        </div>
    </body>
</html>

2、新建ResetServlet

package com.servlet.users;

import com.dao.UsersDao;
import com.entity.UsersEntity;

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 java.io.IOException;

/**
 * 用户密码修改
 * Created by lvjun on 2018-03-23.
 */
@WebServlet(name = "ResetServlet")
public class ResetServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收登陆页面过来的跳转
        request.getRequestDispatcher("/reset.jsp").forward(request, response);
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        //获取前端参数
        String uname = request.getParameter("uname");
        String upwd = request.getParameter("upwd");
        //传入参数
        UsersEntity usersEntity = new UsersEntity();
        usersEntity.setUname(uname);
        usersEntity.setUpwd(upwd);
        UsersDao usersDao = new UsersDao();
        usersDao.GetReset(usersEntity);
        request.getRequestDispatcher("/index.jsp").forward(request, response); //成功后跳转到登陆
    }


}

3、数据库操作方法

/**
     * 用户密码修改
     */
    public  void GetReset(UsersEntity usersEntity){
        Dbmanage dbmanage = new Dbmanage();
        Connection conn = null;
        Statement sta = null;
        try {
            conn = dbmanage.initDB();
            sta = conn.createStatement();
            String sql = "UPDATE users SET upwd='"+usersEntity.getUpwd()+"' WHERE  uname='"+usersEntity.getUname()+"'";
            sta.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            dbmanage.closeDB(sta, conn);
        }
    }


展开阅读全文

没有更多推荐了,返回首页