问题描述
- jsp中response实验,为什么出不了结果
-
inputnumber.jsp
<%@ page contentType="text/html;charset=utf-8" %> <%@ taglib tagdir="/WEB-INF/tags" prefix="computer"%> <html> <body bgcolor="blanchedalmond"> <font size="3"> <form action="" method="post" name="form" > 输入运算数,选择运算符号: <br><input type="text" name="numberone" size="6" /> <select name="operator"> <option value="+">+ <option value="-">- <option value="*">* <option value="/">/ </select> <input type="text" name="numbertwo" size="6" /> <br><input type="submit" value="提交你的选择" name="submit" /> </form> <% String a=request.getParameter("numberone"); String b=request.getParameter("numbertwo"); String operator=request.getParameter("operator"); if(a==null||b==null){ a=""; b=""; } if(a.length()>0&&b.length()>0){ %><computer:computer numberA="<%=a%>" numberB="<%=b%>" operator="<%=operator%>" /> 计算结果是:<%=a%><%=operator%><%=b%>=<%=result%> <%}%> </font> </body> </html>
computer.tag
<%@ tag pageEncoding="utf-8" %> <%@ attribute name="numberA" required="true" %> <%@ attribute name="numberB" required="true" %> <%@ attribute name="operator" required="true" %> <%@ variable name-given="result" scope="AT-END" %> <% try{ double a=Double.parseDouble(numberA); double b=Double.parseDouble(numberB); double r=0; if(operator.equals("+")) r=a+b; else if(operator.equals("-")) r=a-b; else if(operator.equals("*")) r=a*b; else if(operator.equals("/")) r=a/b; jspContext.setAttribute("result",String.valueOf(r)); } catch(Exception e){ jspContext.setAttribute("result","发生异常:"+e); } %>
网页运行结果:
type Exception report message Unable to compile class for JSP: description The server encountered an internal error that prevented it from fulfilling this request. exception org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 40 in the jsp file: /inputnumber.jsp result cannot be resolved to a variable 37: } 38: if(a.length()>0&&b.length()>0){ 39: %><computer:computer numberA="<%=a%>" numberB="<%=b%>" operator="<%=operator%>" /> 40: 璁$畻缁撴灉鏄?細<%=a%><%=operator%><%=b%>=<%=result%> 41: <%}%> 42: </font> 43: </body>
解决方案
乱码了,编码不对,你试试把中文去掉,如果正常了就是编码问题,检查下你对容器编码设置
解决方案二:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 40 in the jsp file: /inputnumber.jsp
result cannot be resolved to a variable
很明显,result不能被解析为一个变量。需要通过getAttribute获得。
解决方案三:
改了下
计算结果:<%=a%><%=operator%><%=b%>=<%=request.getAttribute("result")%>
运行结果:
计算结果:5+8=null
出不了值 一直是=null
时间: 2024-12-26 15:29:45