I am trying to connect to a database on button click on a jsp page. The value from the textbox of the html page should be captured and used to satisfy the conditions necessary to display the database values. I am using HTML,JSP,Jquery and Ajax. My database is MySql.

I have created a **'forgot password' **link which opens up a new webpage containing a textbox (to enter a mail address to which the password has to be sent) and a send button. How do I proceed with it?

I have tried using ajax and jquery on the jsp page to activate database on button click but they fail.

Please help!!!!!

Example codes are very much apprecited.

Member Avatar
LastMitch

Please help!!!!!

@MariannJ

Please post your code.

Need help in Ajax and send mail part of the code!!!

forgotPassword.jsp

<html>
<script>
function emailAck()
{
//document.getElementById("mail").innerHTML="<b>Password has been sent to your mail</b>";
document.getElementById("mail").style.visibility="hidden";
document.getElementById("email").style.visibility="hidden";
document.getElementById("sendbtn").style.visibility="hidden";
/*setTimeout(function() {
    window.location = 'http://localhost:8080/bank/main';
}, 3000);*/
}
</script>
<head>
    <title>Forgot Password</title>
</head>
<%@ include file="/template/banner.jsp" %> 
<center>
<p id="mail" name="mailtext"><b>Please Enter your Email</b></p>
<input type="text" id="email"/>
<input type="submit" value="Send" id="sendbtn" onclick="emailAck()" />
<div class="log"></div>
<script type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type = "text/javascript">

$(function() {

$('#sendbtn').click(function() {


  // I want connect to database using ajax and jsp

  $.ajax({
                //Really not sure of the Ajax code....

               }

 });

}

return false;
    });



});

</script>
</body>
<center></center>

//passwordAck.jsp containing database connection

<html>
<%@ page import="java.sql.*" %>
<% 
String content=request.getParameter("email");//email is value from the textbox on the other page
%>
<p><%=content%></p>
<%
Connection con = null;
String url = "jdbc:mysql://localhost:3306/dbapp";
String driver = "com.mysql.jdbc.Driver";
String userName ="root";
String password="password";
Statement st;
try{
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url,userName,password);
st = con.createStatement();
ResultSet rs = st.executeQuery("select Passwrd, from cust_login where email="'+content+'"");
while(rs.next())
{   
    //send password to the specified mail
%>
<br><br>
<form method="post" name="form">
<div align="center">
<p><b>Password has been sent to your mail</b></p>
<table border="0">
<tr><th>Password</th><th>Email</th></tr>
</tr>
</table>
</div>
 <%
  }
  }

     catch(Exception e){
       Exception ex = e;
       out.println("Mysql Database Connection Not Found");
     }
%>
</form>

</html>