java script varable in jsp

In our project we need to create form with two Fields in such way that first field is combo box and second is textbox. when we select any item in combobox by that selected value we need fetch related dat from data base and display on textbox.

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

<%@ page import="java.io.*" %>

<%!

Connection con =null;

PreparedStatement pst =null;

ResultSet rs=null;

%>

<html>

<head>

<title>Form</title>

<script language="javascript">

function func()

{

<% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

con=DriverManager.getConnection("jdbc:odbc:yourdsn"," "," ");

%>

var str=f1.ccode.value;

<%

pst=con.prepareStatement("Select course_name from course_master where course_id=?);

String ss=(String)%>str<%;

pst.setString(1,ss);

rs=pst.executeQuery();

if(rs.next())

String cn=rs.getString(1);

%>

f1.cname.value="<%=cn%>";

}

</script>

</head>

<body>

<form name="f1">

<table align="center">

<tr><td><b>Course Code</b></td><td><select name="ccode" size="1" onchange="func()">

<option>001</option><option>002</option><option>003</option><option>004</option>

</select>

<b>Starting Date</b><input id"Text" type="text" name="cname" /></td></tr>

</table>

</form>

</body>

</html>

[2297 byte] By [vijay_gvka] at [2007-11-26 17:54:38]
# 1
This will not work my dear friend...Understand one thig..jsp is some script running from the server side where as javascript is the script running from ur client side..U should have a round trip to the server to runn your code written inside the scriplet.......regardsShanu
mshanua at 2007-7-9 5:07:41 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Maybe Ajax could help you with your problem. Look for it in Google. There are a lot of infomation about it.In this page you can find a library and information http://getahead.ltd.uk/dwr/
Chidoa at 2007-7-9 5:07:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Dear Vijay,

The way you have written will not work. I guess you are new to jSP/Servlets.

While writting JSP/Servelts you need to keep following things in mind

1). JavaScript: Always executes on client side

2). Java: Always executes on server side. Even if Java code written with in scriplets in JSP file.

3) Inorder to fetch data on JSP you need to submit your JSP to server

4). To submit your JSP use <form name="f1" action="/<<JSPNAME>>>

5). If you want to use same jsp to load data from data base, then please use name same JSP in <form> tag.

6). So on change of dropdown submit your page, re-load you page to exceute the java code and then display the desired data.">

Lavea at 2007-7-9 5:07:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...