java – 在JSP中更新数据库
发布时间:2020-12-15 02:08:53 所属栏目:Java 来源:网络整理
导读:我为我的JSP页面编写了这个 Java代码,以更新用户的当前登录详细信息.代码未显示任何错误或异常,但未更新 MySql数据库. 帮我实现这个功能; 我的代码: %//variable declaration for encrypt and decryptbyte [] input ;byte [] keyBytes = "12345678".getByte
我为我的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,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和存储在会话中的请求.它会阻止恶意
sql injections.
其次,您的安全约束应该在web.xml和Servlet中处理,这最适合后端维护.遵循良好的编程习惯可以防止你因烦扰日志而疯狂. 我可以帮助您实现您尝试使用Servlet执行的操作,但在此之前,我需要了解以下内容: >显而易见:你有Servlet吗? 这是完成你想要的最有效的方法.请提供答案,我将用一些代码更新我的答案:) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |