美好的一天,所有。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.-->
<!--Spacers.-->
<button name = "depButton" value = "Deposit">Deposit</button> <!--"Deposit" button.-->
<!--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。
此外,如果描述和编码的方式来检查这些会话属性的存在和他们的条件 分配是不正确的,请告诉我如何更好地实现这一点。
非常感谢!