2017-10-06 95 views
1

美好的一天,所有。Java JSP - scriptlet與if()的用法 - else()

我具有以下設置:

  • 甲JSP,一個HTML形式輸出到瀏覽器,則當按鈕中的一個用戶輸入在 文本字段的量之後被點擊時,
  • 數據發送給執行特定計算的servlet並將結果數據發送回JSP。
  • JSP然後顯示結果數據並保持運行,以便用戶可以使用該程序進行更多迭代。

我試圖對JSP進行編碼,當它運行時,它首先檢查是否存在2個會話屬性「balance」 和「formattedBal」。如果這些屬性不存在(這意味着這是在 用戶剛打開其URL地址後第一次運行JSP),則JSP執行必須創建並分配這些會話屬性。如果這些屬性 確實存在(這意味着這不是JSP的初始啓動,而是隨後的啓動,而servlet已經將用戶 恢復爲JSP併發送了結果數據,因此非空會話屬性),JSP必須只需執行HTML輸出 即可在合適的編碼位置顯示所述會話屬性的值。

以下是JSP的代碼:

<html> 
    <hr>  <!--Horizontal line.--> 
    <title>Online Bank ATM Simulator</title>  <!--Title to show on browser title bar.--> 
    <h1 align = "center">Bank ATM Simulation</h1> <!--Page heading, centered on page.--> 

    <SCRIPT LANGUAGE = JAVASCRIPT> 
    <!-- 

    function checkAttributes() 
    { 
    <% 
    // Set balance and formatted balance as session attributes. 
    if(request.getSession().getAttribute("balance") = null && request.getSession().getAttribute("formattedBal") = null) 
    { 
     request.getSession().setAttribute("balance", 0); 
     request.getSession().setAttribute("formattedBal", "$0"); 
    } 
    %> 
    } 
    --> 
    </SCRIPT> 

    <body onLoad = "checkAttributes()", "amount.focus()">    <!--Set focus to the text-field.--> 
     <form method = "POST" action = "../servlet/JSPBank"> <!--Form method and submission address.--> 
      <center>  <!--Tag to center the following output on page.--> 
      Amount: 
      <input type = "text" name = "amount" id = "amount" size = "20"><br><br> <!--Amount text field.--> 
      Balance: 
      <%=(String)request.getSession().getAttribute("formattedBal")%> + "<br><br> <!--Current formatted balance shown.--> 
      <button name = "balButton" value = "Balance">Balance</button> <!--"Balance" button.--> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <!--Spacers.--> 
      <button name = "depButton" value = "Deposit">Deposit</button> <!--"Deposit" button.--> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <!--Spacers.--> 
      <button name = "withdrButton" value = "Withdraw">Withdraw</button> <!--"Withdraw" button.--> 
      </center>  <!--Tag to end centering of output on page.--> 
     </form>  <!--End of form.--> 
    </body> 
    <br> 
    <hr>  <!--Horizontal line.--> 
</html> 

的問題是,我得到一個指向第11行錯誤 - 在<%的標籤。是否我使用函數與否,誤差 遺體並讀取準確如下:

HTTP狀態500 - 類型異常報告 消息 描述服務器遇到一個內部錯誤()阻止其完成此請求。 例外 org.apache.jasper.JasperException:無法編譯類JSP 發生錯誤時行:11在jsp文件:/bank.jsp 生成的servlet錯誤:令牌 語法錯誤「=」 =預計 org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84) org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328) org.apache.jasper.compiler.JDTCompiler。 generateClass(JDTCompiler.java:397) org.apache.jasper.compiler.Compiler.compile(Compiler.java:288) org.apache.jasper.compiler.Compiler.compile(Compiler.java:267) org.apache .jasper.compiler.Compiler.compile(Compiler.java:255) org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:556) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:296) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet的.java:295) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:245) javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

我一直在研究這和相關的錯誤,但沒有太大的功效。

什麼是沒有意義的錯誤狀態,我使用=符號,但它期望!=組合,而當你看到 行指向的時候,從來沒有一個=標誌。

我是Java新手,有可能這是一個簡單的錯誤,你可以幫忙找出它嗎?

當我需要使用「標準」Java命令時,我需要在JSP中使用scriptlets。

此外,如果描述和編碼的方式來檢查這些會話屬性的存在和他們的條件 分配是不正確的,請告訴我如何更好地實現這一點。

非常感謝!

回答

0

你有問題(這是一個常見的問題)是你必須在單引號內封裝scriptlet在JavaScript內使用它。所以像這樣:

'<%=(String)request.getSession().getAttribute("formattedBal")%>'