存储我的Java函数返回String In JavaScript变量的值

store value of my Java function return String In JavaScript variable

本文关键字:JavaScript 变量 In String 我的 Java 函数 返回 存储      更新时间:2023-09-26

我正在JSP中开发一个web应用程序。我用JSP、CSS和HTML制作了这个页面。有六个按钮。每个按钮都调用一个JavaScript方法,所以第一个按钮是对par()方法的调用。

  <html>
<head>
<title>Welcome   d To Student University</title>
<link rel="stylesheet" type="text/css" href="../css/stlogin.css">
<link rel="stylesheet" type="text/css" href="../css/background.css">
<link rel="stylesheet" type="text/css" href="../css/back.css">
<script src="StInfo.jsp">
</script>
</head>
<body>
<div class=main>
    <div class=blank></div>
    <div class=welcome><h1 class=welcome><center>Welcome Student</center></h1>
    <div class=logout><a href='logout.jsp?value=st'>Logout</a></div></div>
</div>
<br>
<div class=menu>
    <div class=leftgap>.
    </div>
    <div class=option>
    <center>
    <button type="button" onclick=par() class="Bt_menu">Check Parsnal Info</button>
    </center>
    </div>
    <div class=option>
    <center>
    <button type="button" onclick=faculty() class="Bt_menu">All Faculty Details</button>
    </center>
    </div>
    <div class=option>
    <center>
    <button type="button" onclick=exam() class="Bt_menu">Next Exams Details</button>
    </center>
    </div>
    <div class=option>
    <center>
    <button type="button" onclick=atten() class="Bt_menu">Attendance Details</button>
    </center>
    </div>
    <div class=option>
    <center>
    <button type="button" onclick=Result() class="Bt_menu">Exam Result Details</button>
    </center>
    </div>
    <div class=option>
    <center>
    <button type="button" onclick=Notices() class="Bt_menu">College Notices / Details</button>
    </center>
    </div>
</div>
<p id=Table></p>
</body>
</html>

在这个页面中,我使用了这个脚本作为:-

<script src="StInfo.jsp">
</script>

现在我向你展示我的StInfo.jsp文件,其中有java脚本方法。

<%@page import="data.*;" %>
<%
ServletConfig con=getServletConfig();
ServletContext ctx=con.getServletContext();
DataRet d;
%>
function par()
{
try
{
// i sat ctx.setAttribute("id") is 1 already in my last page . so the output will be 1 of it .
<%DataRet.setAtt(""+ctx.getAttribute("id"),"stlogin");%>
var id=<%=ctx.getAttribute("id")%>                // if i did that than the value 1 store in id .
var id=<%=DataRet.get(2)%>                        // but when i did that nething happen and code didn't work .
}catch(err)
{
txt="There was an error on this page.'n'n";
txt+="Error description: " + err.message + "'n'n";
txt+="Click OK to continue.'n'n";
alert(txt);
}
//alert("id");
document.getElementById("Table").innerHTML="<center><table border='10'><th>College Id</th> <th>Name</th><th>Father Name</th><th>Department</th><th>Year</th><th>Semester</th><th>Ph. No.</th><th>Address</th>'
<tr>'
<td>"+id+"</td>'
<td>"+id+"</td>'
<td>"+id+"</td>'
<td>"+id+"</td>'
<td>"+id+"</td>'
<td>"+id+"</td>'
<td>"+id+"</td>'
<td>"+id+"</td>'
</tr></table><center>";

}

这是我的DataRet文件--

   package data;
import java.sql.*;
import connection.connection;
public class DataRet
{
static Connection c;
static ResultSet re;
static Statement s;
static String id;
static
{
    try
    {
        c=connection.getConnect();
        System.out.println(c);
        s = c.createStatement();
        System.out.println("Statement Object Created = "+s);
    }catch(Exception e){System.out.println(e);}
}
public static void setAtt(String table)
{
    try{
    re=s.executeQuery("select * from "+table);
    }catch(Exception e){}
}
public static void setAtt(String att,String table)
{
    System.out.println("Table Sated");
    id=att;
    int i=0;
    try
    {
        re=s.executeQuery("select * from "+table);
        while(re.next() && re.getString(3).equals(att))
        {
            i++;
            break;
        }
    System.out.println("curser on "+i);
    }catch(Exception e){System.out.println(e);}
    }
    public static void change()
    {
        try{
            re.next();
        }catch(Exception e){}
    }
    public static String get(int val)
    {
        System.out.println("value geted of "+val);
        try{
            String o=re.getString(val);
            //o=string.valueOf(o);
            System.out.println(o);
            return o;
        }catch(Exception e){ System.out.println("Problum in Geting Value"+e);}
        System.out.println("return null");
        return null;
    }
}

现在的问题是,当我调用一个方法*<%=ctx.getAttribute("id")%>*在stInfo.jsp中,然后这个方法在非常大的空间中打印1。当我打电话的时候<%=DataRet.get(2)%>方法,然后文件不起作用。。。。

JSP scriptlet中的数据存储应该非常直接,就像您拥有的一样

var id=<%=ctx.getAttribute("id")%>

尽管我建议您小心返回的数据,但如果它不是一个数字,那么您必须将其放在引号之间,以确保JS不会中断。例如:

如果<%=DataRet.get(2)%>返回字符串"TEST",则生成的JS看起来像:

var id=TEST

因为并没有名为TEST的变量,所以这只是简单的中断。你需要用双引号或单引号把它括起来,比如:

var id="<%=DataRet.get(2)%>";

此外,您必须记住每行末尾的分号,并转义任何可能破坏JS代码的字符。请记住,在编译JSP之后,JS代码还没有执行,所以这就像手动编写JS代码一样。

如果此代码不起作用,请首先检查<%=DataRet.get(2)%>返回的内容,以及是否存在任何JS错误。

希望能有所帮助。