0

Respected Sir,
I am trying to execute above code with rerquest response object. In my

jsp file i have 3 text boxes 1.mobile 2.username 3.password ,

All the above three attributes are stored in MySql table emp.
I want to execute this code in a fashion like when i entered the mobile number in text box

the values of other attributes like username and password should be automatically retrieved

in respective text boxes. if for solving above problem servlet needed then help me with that

also.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<%@ page import ="java.sql.*;" %>
<html>




<script language="javascript">
function validate()
{
	if(document.loginname.user.value=="")
	{
		alert("Please Enter Username");
		document.loginname.user.focus();
		document.loginname.user.select();
	}
	else if(document.loginname.passw.value=="")
	{
		alert("Please Enter Password");
		document.loginname.passw.focus();
		document.loginname.passw.select();
	}	
	else
	{
		document.loginname.submit();
	}
}

            
 
              
            
            
</script>


<form name="loginname" method="post" >
<body>
        <% if (request.getParameter("msg") != null )
		{
	%>
		<table align="center"><tr><td>
		<center><font color="#993300" style="font-family:Verdana, Arial, Helvetica, 

sans-serif; font-size:12px">Connection Reset.Try Again.</font></center>
		</td></tr></table>
	<%		
		}	
	%>
	<% if (request.getParameter("logmsg") != null )
		{
	%>
		<table align="center"><tr><td>
		<center><font color="#993300" style="font-family:Verdana, Arial, Helvetica, 

sans-serif; font-size:12px">Invalid Login Data.Try Again.</font></center>
		</td></tr></table>
	<%		
		}	
	%>
	<br>



<table  width="300px" align="center" cellpadding="5" cellspacing="5">
<tr><td>Insert Mobile Number: </td><td><input type="text" name="mobile"/></td></tr>
                <tr><td>Username: </td><td><input type="text" name="empuser" /></td></tr>
		<tr><td>Password: </td><td><input type="password" name="emppassw" 

/></td></tr>	
		<tr>
			<td colspan="2">
				<center>
				<input type="button" onClick="validate()" value="Submit"/>
				<input type="reset" value="reset" />
				</center>
				
</body>
</form>
<script language="javascript">
 
        function usernameandpass()
        {

<%! String str;%>
<%! int id2;%>
<%! Connection con = null;%>
<%! Statement st = null;%>
<%! ResultSet rs = null;%>

                     
             
                
<%
                String str=request.getParameter("mobile");
                System.out.print(str);
                

            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
               // String mobile=request.getParameter("mobile");
                
            } catch (ClassNotFoundException ce) {
                out.println(ce);
            }

            try {
                con = DriverManager.getConnection("jdbc:mysql://localhost:3306/online", 

"root", "root");
                st = con.createStatement();
                rs = st.executeQuery("Select * from emp where mobile="+mobile" ");
                while (rs.next()) {
                    id2 = rs.getInt(1);
                }
                id2++;
                rs.close();
                st.close();
                con.close();


            } catch (SQLException exception) {
            }


            System.out.print(id2);

%>
        document.write(mobile);
    }
   
</script>

</html>

Edited by peter_budo: Please do not use CODE SNIPPED option to post your questions. Use FORUM THREAD

2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by javaAddict
1

You cannot execute java code from inside javascript.

You need to submit the data and then update the textfields:

String mobile = request.getParameter("mobile");
String username="";
String password="";
String message=null;

if (mobile !=null) { // the first time the page will render you will not have the mobile, so skip this part

  // use the mobile to get the data
  // depending on the results generate the appropriate html code
  // If there is any kind of error store it into the String message:

  try {
     if mobile OK   {
       message = "Data retrieved successfully";
       username = ....;
       password = ....;
     }
     else message="Could not get data";
  } catch (Exception e) {
    message = "An error has occured";
  }

}


// the rest of the HTML with the javascript validation code:

<%
if (message!=null) {
%>
Results: <%= message%>
<%
}
%>
<form name="mobileForm" action="" method="post">
   <input type="text" name="mobile" value="<%= mobile%>">
   <input type="text" name="username" value="<%= username%>">
   <input type="text" name="password" value="<%= password%>">
</form>

The above is simple, beginners way, but not very efficient.
For what you are trying to accomplish:

the values of other attributes like username and password should be automatically retrieved

you will need to learn Ajax.
But given the code you just wrote, you need to learn first proper jsp, understand that javascript is not java and cannot be mixed in the way you did, understand how java code inside the jsp is executed and how it renders the page, what happens when the page loads and how submitting forms work.

So stick to the usual sending the request from one page to the other, retrieving the data and sending them to another page. That is what servlets do.

Also don't you ever open connections or execute queries in JSPs. Have separate methods and classes that do that and call them from inside the jsp.

Edited by javaAddict: n/a

0

Respected Sir,
Thank you for providing me solution. can you tell me how to
open connections or execute queries in JSPs. Which are the separate methods and classes that do that and call them from inside the jsp.

0

Respected Sir,
Thank you for providing me solution. can you tell me how to
open connections or execute queries in JSPs. Which are the separate methods and classes that do that and call them from inside the jsp.

You will write the methods that open the connection and execute queries. Those methods will have the code that you are using now, but instead of having the code inside the jsp, you have it in seperate methods and classes that you will createa and call these methods from your query

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.