首页 > 其他分享 >2023/01/24

2023/01/24

时间:2024-02-03 19:37:59浏览次数:16  
标签:24 01 request new 2023 import hhh servlet response

开始设计数据库操作方法来修改密码

    public boolean updateCode(Base_InformationBean baseInformationBean) //修改密码
    {
        DBUtil db=new DBUtil();
        Connection conn=db.getConnection();
        String sql="update hhh set password=? where code=?";
        try {
            PreparedStatement pstm=conn.prepareStatement(sql);
            pstm.setString(1,baseInformationBean.getPassword());
            pstm.setString(2,baseInformationBean.getCode());
            pstm.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }

然后通过servlet来进行调用完成密码修改,修改完成后将会直接重定向返回主页面并给出提示。

package com.example.demo;

import bean.Base_InformationBean;
import bean.InfoDAO;
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 java.io.IOException;

@WebServlet(value = "/updatepwd_servlet")
public class UpdatePwdServlet extends HttpServlet {
    private String code;
    private String password;
    private Base_InformationBean hhh;
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        code=request.getParameter("nameid");
        password=request.getParameter("pwd");
        hhh=new Base_InformationBean();
        hhh.setCode(code);
        hhh.setPassword(password);
        boolean flag= new InfoDAO().updateCode(hhh);
        request.setAttribute("msg","密码修改完成");
        try {
            request.getRequestDispatcher("index.jsp").forward(request,response);
            } catch (ServletException e) {
                throw new RuntimeException(e);
            }
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html");
        doGet(request,response);
    }
}

然后在注册账号时我们还要添加一个操作来判断该用户名是否已经被注册,这刚好也要用到我们之前写的查找账号的方法。

修改完成的servlet为

package com.example.demo;

import bean.Base_InformationBean;
import bean.InfoDAO;
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 java.io.IOException;

@WebServlet(value="/register-servlet")
public class FegisterServlet extends HttpServlet {
    private String code;
    private String password;
    private Base_InformationBean hhh;
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        code=request.getParameter("nameid");
        password=request.getParameter("pwd1");
        hhh=new Base_InformationBean();
        hhh.setCode(code);
        hhh.setPassword(password);
        if(new InfoDAO().findCode(hhh))
        {
            request.setAttribute("msg","该账号已被注册请重新填写");
            try {
                request.getRequestDispatcher("register.jsp").forward(request,response);
            } catch (ServletException e) {
                throw new RuntimeException(e);
            }
        }
        else {
            new InfoDAO().register(hhh);
            request.setAttribute("msg", "注册完成");
            try {
                request.getRequestDispatcher("index.jsp").forward(request, response);
            } catch (ServletException e) {
                throw new RuntimeException(e);
            }
        }
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html");
        doGet(request,response);
    }
}

这样我们就完成了忘记密码时的密码修改和补充了账号重复注册时会出现问题的bug

 

标签:24,01,request,new,2023,import,hhh,servlet,response
From: https://www.cnblogs.com/zhenaifen/p/17985501

相关文章

  • 2023/01/23
    设计找回密码页面,为了达到页面的复用,这里设计了两层,一层用来查询账号是否存在,另一层用于修改密码。<%--CreatedbyIntelliJIDEA.User:龚涵彬Date:2024/2/3Time:16:43TochangethistemplateuseFile|Settings|FileTemplates.--%><%@pagecontentT......
  • 2024/01/22
    设计注册页面,并对输入的值进行一个简单的初步判断。<%--CreatedbyIntelliJIDEA.User:龚涵彬Date:2024/2/2Time:19:52TochangethistemplateuseFile|Settings|FileTemplates.--%><%@pagecontentType="text/html;charset=UTF-8"language="ja......
  • 2024.2.3寒假每日总结25
    算法:1690.石子游戏VII-力扣(LeetCode)使用的HBuilderX版本:3.98Git插件已安装:项目结构如下:右击项目目录,在git命令中-》检查已修改,可以发现还是能检索到修改过的文件:文件是有修改过的,但是在上图中没有任何的修改标识,这些文件也没有添加......
  • (python)代码学习||2024.2.3||题目是codewars上的【Validate Sudoku with size `NxN`
    题目的要求是写一个Sudoku类,类中要有一个实例函数判断传给对象的二维数组是否符合数独规则题目链接:https://www.codewars.com/kata/540afbe2dc9f615d5e000425/python下面是写完题后看到的别人的解决方法fromitertoolsimportchainclassSudoku(object):def__init__......
  • 2024/01/17
    编写一个简单的登录界面<html><head><title>JSP-HelloWorld</title></head><bodybackground="imagine/鸡哥头.jpg"style="background-repeat:no-repeat;background-attachment:fixed;background-size:100%100%">......
  • 0129-0203部分校赛题解复盘
    vj第一场A题https://codeforces.com/gym/103480/problem/A该题让我们可以从回文串的特点入手,即两个相同的字母便可增加长度2,所以并不用思考该回文串要如何排序出来,而是看有多少对相同的字母,使用map<char,int>来记录字母出现的次数,再计算可以除以2的次数即可。点击查看代码#i......
  • uni-app小程序开发 基础 #2月摸鱼计划01
    前言:最近看uni-app框架的时候发现了这套课程,看到网络上大都是收费的资料,所以打算把这份资源开源共享出来,如果觉得有帮助的话,务必支持一下,关注......
  • Go语言 猜谜游戏+在线词典 2月摸鱼计划01
    3.1猜谜游戏3.1.2生成随机数v2packagemainimport( "fmt" "math/rand" "time")funcmain(){ maxNum:=100 rand.Seed(time.Now().UnixNano()) secretNumber:=rand.Intn(maxNum) fmt.Println("Thesecretnumberis",secre......
  • [THUSCH2017] 巧克力
    [THUSCH2017]巧克力题目描述「人生就像一盒巧克力,你永远不知道吃到的下一块是什么味道。」明明收到了一大块巧克力,里面有若干小块,排成\(n\)行\(m\)列。每一小块都有自己特别的图案,它们有的是海星,有的是贝壳,有的是海螺……其中还有一些因为挤压,已经分辨不出是什么图案了。......
  • 北京地铁经纬度结构化数据收集20240203
    地铁图 原始数据共250个站点,或有遗漏1苹果园,39.9263251384,116.17778062822古城,39.9072014648,116.19024753573八角游乐园,39.9074319023,116.21282100684八宝山,39.9072673042,116.23582363135玉泉路,39.9073331435,116.25303268436五棵松,39.......