gpt4 book ai didi

java - 在 JSP 中更新数据库

转载 作者:可可西里 更新时间:2023-11-01 07:44:13 29 4
gpt4 key购买 nike

我为我的 JSP 页面编写了这段 Java 代码,以更新用户的当前登录详细信息。代码未显示任何错误或异常,但未更新 MySql 数据库。

帮我实现这个功能;

我的代码:

<%
//variable declaration for encrypt and decrypt
byte [] input ;
byte [] keyBytes = "12345678".getBytes();
byte [] ivBytes ="input123".getBytes();

SecretKeySpec key = new SecretKeySpec(keyBytes,"DES");
IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
Cipher cipher;
byte[] cipherText;
int ctLength=0;

Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);

if(request.getParameter("submit")!=null){
String cuser=request.getParameter("currentusername");
String user = request.getParameter("username");
String pwd = request.getParameter("password");
String cpwd = request.getParameter("confirmpassword");

Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
input = pwd.getBytes();
key = new SecretKeySpec(keyBytes, "DES");
ivSpec = new IvParameterSpec(ivBytes);
cipher = Cipher.getInstance("DES/CTR/NoPadding","BC");

cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
cipherText = new byte[cipher.getOutputSize(input.length)];

ctLength+=cipher.update(input, 0, input.length, cipherText, 0);

ctLength+= cipher.doFinal(cipherText, ctLength);
String enpwd = new String(cipherText);


String sql2 = "update webadmin set username=? ,password=? where username='"+cuser+"' ";

if((cuser!=null &&cuser.length()>0)
&& (user!=null &&user.length()>0)
&& (pwd!=null && pwd.length()>0)
&& cpwd!=null && cpwd.length()>0) {

if((pwd.equals(cpwd))){
pst =conn.prepareStatement(sql2);
pst.setString(1, user);
pst.setString(2, enpwd);

pst.executeUpdate();
%>
<script language="JavaScript">
alert("Sucessfully Updated");
</script>
<%
}else{
%>
<script language="JavaScript">
alert("Passwords are not matching try again");
</script>
<%

}
}
}
}

%>

注意:我实现了对密码进行加密并将加密的密码存储到数据库中。

HTML 格式;

<form id="login-form"  action="adminpg-mysettings.jsp" method="post" role="form" style="display: block;">
<div class="form-group">
<input type="text" name="currentusername" id="currentusername" tabindex="1" class="form-control" placeholder="Current Username" value="" required="">
</div>
<div class="form-group">
<input type="text" name="username" id="username" tabindex="1" class="form-control" placeholder="New Username" value="" required="">
</div>
<div class="form-group">
<input type="password" name="password" id="password" tabindex="2" class="form-control" placeholder="New Password" required="">
</div>
<div class="form-group">
<input type="password" name="confirmpassword" id="password" tabindex="2" class="form-control" placeholder="Confirm New Password" required="">
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<input type="submit" name="submit" id="submit" tabindex="4" class="form-control btn btn-login" value="Save">
</div>
</div>
</div>
</form>

最佳答案

首先,就像每个人都会告诉您的那样,将 Java 放在 JSP 中是一个非常糟糕的主意。正确的操作方式是使用一个 Servlet 并将请求存储在 session 中。它将防止恶意 sql injections .

其次,您的安全约束应该在web.xmlServlet 中处理,这最适合后端维护。遵循良好的编程习惯将防止您因窃听日志而发疯。

我可以帮助您实现您尝试使用 Servlet 执行的操作,但在此之前,我需要了解以下内容:

  • 显而易见:您有 Servlet 吗?
  • 您使用 JDBC/JNDI 连接吗?
  • 你有用户的实体类和 session 类吗?
  • 您使用哪种 IDE/框架来开发您的应用?
  • 您要部署到哪个服务器?

这是实现您的目标的最有效方式。请提供答案,我会用一些代码更新我的答案:)

关于java - 在 JSP 中更新数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32087235/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com