Java – update database in JSP

I wrote this java code for my JSP page to update the user's current login details The code does not show any errors or exceptions, but the MySQL database is not updated

Help me realize this function;

My code:

<%
//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>
           <%

     }
    }
  }
}

%>

Note: I implement the encrypted password and store the encrypted password in the database

HTML form;

<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>

Solution

First, as everyone will tell you, putting Java into JSP is a very bad idea The correct operation is to use servlets and requests stored in the session It prevents malicious SQL injections

Second, your security constraints should be on the web XML and servlet, which is most suitable for back - end maintenance Following good programming habits can prevent you from being crazy about annoying logs

I can help you do what you're trying to do with a servlet, but before that, I need to know the following:

>Obvious: do you have a servlet? > Do you use JDBC / JNDI connection? > Do you provide users with entity and session courses? > Which ide / framework do you use to develop your application? > What is the server you are deploying?

This is the most effective way to accomplish what you want Please provide the answer and I will update my answer with some code:)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>