刚学JSP遇到了点问题
jsp计算某数据的平方根(在jsp页面中静态包含文件)
要求:在一个页面中使用指令标签”include”,静态包含另一页面文件。
程序算法提示:
example4_1.jsp:使用”include”指令标签静态包含页面Computer.jsp
Computer.jsp:包括:
创建表单,该表单包含一个文本框,从文本框获取正整数。
计算整数的平方根
输出平方根
我写的代码如下(有错误,请大家帮我看看是哪里的问题):
文件:Noname5-1.jsp 代码
<%@ page contentType="text/html;charset=GB2312" %>
<HTML>
<BODY>
<jsp:include page="Noname5-2.jsp" />
</BODY>
</HTML>
文件:Noname5-2.jsp 代码
<%@ page contentType="text/html;charset=GB2312" %>
<HTML>
<BODY bgcolor=cyan>
<FORM method="POST" name="form1" action="Noname5-2.jsp" method=post>
<INPUT TYPE="TEXT" name="zhi">
<INPUT type=submit value=提交>
<%
String str=request.getParameter("zhi");//取得参数number的值
if(str!=null)
{
double m=Double.parseDouble(str);} //将字符串转换成整型
else
out.print("输入错误");
%>
<%=Math.sqrt(m)%>
</FORM>
</BODY>
</HTML>
错误提示:
type Exception report
message
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: 16 in the jsp file: /Noname5-2.jsp
m
cannot be resolved13: else
14: out.print("输入错误");
15: %>
16: <%=Math.sqrt(m)%>
17:
18: </FORM>
19: </BODY>
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:423)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:317)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:295)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:282)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:586)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:968)
org.apache.jsp.Noname5_002d1_jsp._jspService(Noname5_002d1_jsp.java:56)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.18 logs.
------解决方案--------------------double m=Double.parseDouble(str);} 变量定义的位置不对,提到外面去定义。
<%
double m =0.0;
String str=request.getParameter("zhi");//取得参数number的值
if(str!=null)
{