-4

JSPでWebアプリケーションを開発しています。このページは、JSP、CSS、HTML で作成しました。ボタンは6つです。すべてのボタンは 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>

ここで、Java スクリプト メソッドがある StInfo.jsp ファイルを表示します。

<%@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;
    }
}

ここで問題は、 stInfo.jsp で * <%=ctx.getAttribute("id")%> *メソッドを呼び出すと、このメソッドが非常に coloum で 1 を出力することです。<%=DataRet.get(2)%> メソッドを呼び出すと、ファイルが機能しませんでした ....

4

1 に答える 1

1

JSP スクリプトレットからのデータの保存は、あなたが持っているのと同じように非常に簡単です。

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 エラーがあるかどうかを確認することから始めます。

それが役に立てば幸い。

于 2013-02-13T16:40:46.780 に答える