刚接触jsp,按照书上的程序试验一下,可出现了好多出错信息!

w_hb 2004-10-28 09:02:45
主要功能是实现一个计数器,将访问者数量保存在count.txt中。
我把count.txt和count.jsp建在同一目录中
源代码如下:
count.jsp

<%@ page contentType="text/html;charset=GB2312"%>
<HTML>
<TITLE>计数器</TITLE>
<BODY>
<center>计数器</center>
<hr>
<%
String path =request.getRealPath("./");
FileReader fr=new FileReader(path+"count.txt");
BufferedReader br=new BufferedReader(fr);
String strNum=br.readLine();
br.close();
fr.close();
%>
<center>您是第<% =strNum% >位访问者。</center>
<%
FileWriter fw =new FileWriter(path+"count.txt");
int Num=Integer.parseInt(strNum)+1;
fw.write(String.valueOf(Num));
fw.close();
%>
</BODY>
</HTML>


运行后提示的信息:
org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 14 in the jsp file: /jsp/count.jsp

Generated servlet error:
[javac] Compiling 1 source file

D:\Tomcat 4.1\work\Standalone\localhost\_\jsp\count_jsp.java:59: illegal start of expression
=strNum% >浣????????</center>
^

An error occurred at line: 14 in the jsp file: /jsp/count.jsp

Generated servlet error:
D:\Tomcat 4.1\work\Standalone\localhost\_\jsp\count_jsp.java:59: illegal character: \12290
=strNum% >浣????????</center>
^

An error occurred at line: 6 in the jsp file: /jsp/count.jsp

Generated servlet error:
D:\Tomcat 4.1\work\Standalone\localhost\_\jsp\count_jsp.java:51: cannot resolve symbol
symbol : class FileReader
location: class org.apache.jsp.count_jsp
FileReader fr=new FileReader(path+"count.txt");
^

An error occurred at line: 6 in the jsp file: /jsp/count.jsp

Generated servlet error:
D:\Tomcat 4.1\work\Standalone\localhost\_\jsp\count_jsp.java:51: cannot resolve symbol
symbol : class FileReader
location: class org.apache.jsp.count_jsp
FileReader fr=new FileReader(path+"count.txt");
^

An error occurred at line: 6 in the jsp file: /jsp/count.jsp

Generated servlet error:
D:\Tomcat 4.1\work\Standalone\localhost\_\jsp\count_jsp.java:52: cannot resolve symbol
symbol : class BufferedReader
location: class org.apache.jsp.count_jsp
BufferedReader br=new BufferedReader(fr);
^

An error occurred at line: 6 in the jsp file: /jsp/count.jsp

Generated servlet error:
D:\Tomcat 4.1\work\Standalone\localhost\_\jsp\count_jsp.java:52: cannot resolve symbol
symbol : class BufferedReader
location: class org.apache.jsp.count_jsp
BufferedReader br=new BufferedReader(fr);
^

An error occurred at line: 14 in the jsp file: /jsp/count.jsp

Generated servlet error:
D:\Tomcat 4.1\work\Standalone\localhost\_\jsp\count_jsp.java:63: cannot resolve symbol
symbol : variable fw
location: class org.apache.jsp.count_jsp
fw.write(String.valueOf(Num));
^

An error occurred at line: 14 in the jsp file: /jsp/count.jsp

Generated servlet error:
D:\Tomcat 4.1\work\Standalone\localhost\_\jsp\count_jsp.java:64: cannot resolve symbol
symbol : variable fw
location: class org.apache.jsp.count_jsp
fw.close();
^

An error occurred at line: 14 in the jsp file: /jsp/count.jsp

Generated servlet error:
Note: D:\Tomcat 4.1\work\Standalone\localhost\_\jsp\count_jsp.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
8 errors

...全文
123 点赞 收藏 12
写回复
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
mhz 2004-10-29
同意他的写法monkeyhjl(爬上墙头等红杏)
运行时你的path要有count.txt文件
回复
紫翎观星 2004-10-29
JSP里面可以用try catch的,但不建议这样用。
回复
monkeyhjl 2004-10-29
<%@ page contentType="text/html;charset=GB2312"%>
<%@ page import="java.io.*"%>
<HTML>
<TITLE>计数器</TITLE>
<BODY>
<center>计数器</center>
<hr>
<%
String path =request.getRealPath("/");
FileReader fr=new FileReader(path+"count.txt");
BufferedReader br=new BufferedReader(fr);
String strNum=br.readLine();
if (strNum == null)
strNum = "1";
br.close();
fr.close();
%>
<center>您是第<%=strNum%>位访问者。</center>
<%
FileWriter fw =new FileWriter(path+"count.txt");
int Num=Integer.parseInt(strNum)+1;
fw.write(String.valueOf(Num));
fw.close();
%>
</BODY>
</HTML>
回复
w_hb 2004-10-29
我导入<%@page import="java.io.*"%>
将<% =strNum% >改为<%=strNum%>(改成<%= strNum %>也是一样的)
运行之后是找不到服务器的页面
回复
w_hb 2004-10-29
我照着monkeyhjl(爬上墙头等红杏)说的运行了一下,(我的count.txt文件在web站点主目录位置应该没错吧)结果是这样的
org.apache.jasper.JasperException: /jsp/count.jsp(4,0) Page directive: can't have multiple occurrences of contentType
at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:94)
at org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:428)
at org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:140)
at org.apache.jasper.compiler.Validator$PageDirectiveVisitor.visit(Validator.java:161)
at org.apache.jasper.compiler.Node$PageDirective.accept(Node.java:280)
at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:1028)
at org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:1070)
at org.apache.jasper.compiler.Node$Visitor.visit(Node.java:1076)
at org.apache.jasper.compiler.Node$Root.accept(Node.java:232)
at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:1028)
at org.apache.jasper.compiler.Validator.validate(Validator.java:581)
at org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:230)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:369)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:473)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:190)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2422)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:163)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:199)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:828)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:700)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:584)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:536)

回复
bsnail 2004-10-28
JSP不用try catch?
回复
ziying 2004-10-28
有try catch可以查下错在哪
回复
xitianjile 2004-10-28
JSP里面不用try catch的.
回复
xitianjile 2004-10-28
都没导入类..
回复
yuaoyao 2004-10-28
应该导入java.io.*这个包
还有你这名<% =strNum% >应改为<%=strNum%>去掉空格.
回复
cold_blooded 2004-10-28
好像没有try catch 也
回复
comcn 2004-10-28
你是不是还应导入<%@page import="java.io.*"%>
回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告