0

I have a serious problem.

Firstly, I am trying to retrieve values from my database by individual scripts on my JSP pages. Here is part of the code:


<%String driver = "com.mysql.jdbc.Driver";

Class.forName(driver).newInstance();

Connection con=null;

ResultSet rst=null;`

Statement stmt=null;

try{

String url="jdbc:mysql://localhost:3306/MajorProjectDB?user=cdsadmin2&password=enjoyit";

con=DriverManager.getConnection(url);

stmt=con.createStatement();

}
catch(Exception e){
System.out.println(e.getMessage());
}

String name= request.getRemoteUser();

rst=stmt.executeQuery("select verifycode from Staff where name='"+name+"'");
%>

<%
int no=1;`
while(rst.next()){

%>

<%=no%>

<%=rst.getString("VERIFYCODE")%>

<%String code= request.getParameter("code"); %>

<% } %>

In this code I am storing a string from VERIFYCODE column in my Staff table. I will be able to compare it with the user input within the form by using a getparameter.

HOWEVER, the comparison should be done with onClick function, but due to the fact that it is running as an individual script, it cannot be attached to the onClick function.

Now I am trying to insert an SQL statement in the following javascript in my JSP page:


<script type="text/javascript">

function verifyCode(){ 

// a SELECT sql statement which gets String from database

//comparison between String retrieved from database and user input

if{

  }

else{

}

}

</script>

May I know if anyone has any solution? Or is there a easier method of doing it? Thanks in advance, any help is greatly appreciated!

Sangeet Menon
  • 9,555
  • 8
  • 40
  • 58
gymcode
  • 4,431
  • 15
  • 72
  • 128
  • Try using [jQuery AJAX](http://api.jquery.com/jQuery.ajax/) for making a request to a JSP page for data and then return the data as a response to that request. Try googling abou **AJAX** – Sangeet Menon Aug 16 '11 at 12:06

2 Answers2

2

Server converts jsp to HTML and sends to client and client gets only HTML javascript are executed on client side.

For your case you can use DWR, to call java method from server from the javascript

See Also

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
0

JSP is server side, javascript is client side !

Your javascript cannot insert anything into a DB its client side

some tips: Throw away scriptlets and use proper seperation of view logic and business logic.

Peter
  • 5,728
  • 20
  • 23