title: Create Change Password page by JSP & Servlet
body: <p>I created a <code>Change Password</code> form using JSP, and a servlet page to manipulate data and enter it into the DB.
  This is an internal website (intranet) therefore the DB already contains details about <code>Email, FName, LName</code> for all users. Only the password field is null. What I'm trying to do is, after the user fills in their email address, password, confirms it and then presses on the submit button, the servlet page should update the password field for the current Email. Unfortunately, in all cases the user seems to be redirected to <code>ChangeError.jsp</code>.
  Please find enclosed both files.(<code>ChangePassword.JSP</code>  and <code>ChangePassword.Servlet</code>). Any help would be much appreciated.</p>
  
  <pre><code>    //ChangePassword.jsp
      &lt;FORM ACTION="ChangePassword" METHOD="GET"&gt;
  &lt;div id="login_box"&gt;
    &lt;div id="login_header"&gt;
          Login
    &lt;/div&gt;
    &lt;div id="form_val"&gt;
      &lt;div class="label"&gt;Email:&lt;/div&gt;
      &lt;div class="control"&gt;&lt;input type="text" name="Email" id="Email"/&gt;&lt;/div&gt;
  
      &lt;div class="label"&gt;Password:&lt;/div&gt;
      &lt;div class="control"&gt;&lt;input type="password" name="Password" id="Password"/&gt;&lt;/div&gt;
  
      &lt;div class="label"&gt;Confirm Password:&lt;/div&gt;
      &lt;div class="control"&gt;&lt;input type="password" name="Password" id="Password"/&gt;&lt;/div&gt;
      &lt;div style="clear:both;height:0px;"&gt;&lt;/div&gt;
  
      &lt;div id="msgbox"&gt;&lt;/div&gt;
    &lt;/div&gt;
  
    &lt;div id="login_footer"&gt;
    &lt;!-- &lt;script type="text/javascript"&gt;
    function closewindow() {
    window.close()
    }
    &lt;/script&gt;--&gt;
       &lt;label&gt;
      &lt;input type="submit" onclick="location.href='http://localhost:8080/IntegrateAll/Index.jsp'" window.close();" name="Submit" id="Submit" value="Submit" class="send_button" /&gt;
      &lt;/label&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p></p>
  
  <p>
  </p>
  
  <pre><code>    //ChangePassword.servlet        
      import java.io.IOException;
      import javax.servlet.*;
      import javax.servlet.ServletException;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import javax.sql.DataSource;
      import java.sql.Connection;
      import java.sql.ResultSet;
      import java.sql.PreparedStatement;
      import javax.naming.*;
      public class ChangePassword extends HttpServlet {
  private static final long serialVersionUID = 1L;
  public ChangePassword() {
      super();
  }
  protected void doGet(HttpServletRequest request,
          HttpServletResponse response) throws ServletException, IOException {
      String Email = request.getParameter("Email");
      String Password = request.getParameter("Password");
      String CPassword = request.getParameter("Password");
  
      Connection con =   GetConnectToDb();
      boolean flag = CheckLogin(Password,CPassword,Email,con);
      if(flag==true){
          gotoPage("/LoginPage.jsp",request,response);
      }
      else{
          gotoPage("/ChangeError.jsp",request,response);
      }
  }
  public void gotoPage(String address, HttpServletRequest request,
          HttpServletResponse response) throws ServletException, IOException {
      RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(address);
      dispatcher.forward(request, response);
  }
  public boolean CheckLogin(String Password, String CPassword,String Email, Connection con) {
      if (Password != null &amp;&amp; CPassword != null &amp;&amp; Email !=null) {
          try {
              PreparedStatement ps = con.prepareStatement("UPDATE PUBLIC.IAUSERS SET Password = ('"+Password+"') WHERE Email='"+Email+"'");
              ResultSet rs = ps.executeQuery();
          while(rs.next()) {
                  return true;
              }
          } catch (Exception e) {
          }
      }
      return false;
  }
  public Connection GetConnectToDb() {
  
      try {
          InitialContext ctx = new InitialContext();
          Connection con = null;
          DataSource ds = (DataSource) ctx.lookup("java:/DefaultDS");
          con = ds.getConnection();
          return con;
      } catch (Exception e) {
          return null;
      }
  }
  protected void doPost(HttpServletRequest request,
          HttpServletResponse response) throws ServletException, IOException {
  
      }}
  </code></pre>