开发者

Passing values from Server through JSP to javascript functions

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")%>

开发者_如何学JAVA<%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!


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

  • why-business-logic-should-be-moved-out-of-jsp


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜