<%
String name = "test";//get from database
%>
<script>
function check(){
document.form.name.value= "<%=name%>";
}
</script>

解决方案 »

  1.   

    记住:jsp是服务器运行的,Javascript是在前台运行的,也就是说,Javascript可以用jsp的变量,jsp就。。赫赫
      

  2.   

    给你一个程序看看,从数据库中取值操作:
    <%--
    File     :   gdgg.jsp
    Purpose  :   滚动公告
    Created  :   2001-06-08
    By       :   lonelyfox
    History  :
    --%>
    <%@ page import="java.sql.*,com.shung.*" %>
    <%@ page import="java.util.*" %>
    <jsp:useBean id="orasql" scope="page" class="com.shung.xbswg.orasql169" />
       <%
    String strQry;
    strQry=" select count(*) as count from t_bulletin where type in (0,1)";  //取得公告条数
    ResultSet rs=null;
    int iTemp=0,iCount=0,iLength=0;  
    try 
    {
    orasql.setCon();
    rs=orasql.SelectExecute(strQry);
    while(rs.next())
    {
    Integer intTemp=new Integer(0);                 // 转换String为int     
    iCount=intTemp.parseInt(rs.getString("count"));//型数据
    }
    }
    catch(Exception e)
    {
    out.println("Error in "+e.getMessage());
    }
    String[] strGgbt = new String[iCount]; //在程序运行时根据公告的数量生成数组,
    String[] strBull = new String[iCount]; 
    strQry="select ggbt,bulletin_id from t_bulletin where type in (0,1) order by bulletin_id DESC";
    try
    {
    rs=orasql.SelectExecute(strQry);
    while(rs.next())
    {

    strGgbt[iTemp] = rs.getString("ggbt");
    strBull[iTemp] = rs.getString("bulletin_id");
    iTemp++;
    }

    }
    catch(Exception e)
    {
    out.println("Error in "+e.getMessage());
    }

    %>   <table border='0' width='20%' height='80'>
    <tr>
      <td width='100%' height='90'>
        <p align='center'>
    <script language=javascript>
    <!--
    var index=<%=iTemp%>
    link = new Array(index);
    text = new Array(index);

    <%
    for(int i=0;i<iTemp;i++)
    {
    %>
    link[<%=i%>]="showbulletin.jsp?bt=<%=orasql.strNullTo(strBull[i])%>"
    text[<%=i%>]="<%=orasql.strNullTo(strGgbt[i])%>"
    <%}%>

    document.write("<marquee scrollamount='1' scrolldelay='100' direction= 'up' width='180' height='100' id=kevintam onmouseover=stop() onmouseout=start()>");
    for(j=0;j<index;j++)
    { document.write("<span align='center' style='cursor:hand' onclick=window.open('"+link[j]+"','ttt','width=380,height=483,resizable=0')>");
    document.write("<div class=more>"+text[j]+"</div></span><br>");
    }
    document.write("</marquee>");
    //-->
    </script>
        </p>
       </td>
    </tr>
    </table>