MyException - 我的异常网
当前位置:我的异常网» Java Web开发 » 这是咋回事啊

这是咋回事啊

www.MyException.Cn  网友分享于:2013-01-20  浏览:18次
这是怎么回事啊?
HTML code

<%@ page language="java" contentType="text/html;charset=GB2312" %>
<%@ page import="java.io.*" %>      //导入包
<html>
<head>
<title>第一个JSP访问页面</title>
</head>
<body>
<center>
<table style="border:#000000 double 1px;">
<tr>
<td>
<%= "第一个JSP访问页面" %></td>
</tr>
<tr>
<td>
    <%!
        int i=0;
        File file=new File("count.txt");
        synchronized void count()
        {
            if(!file.exists())
            {
                i++;
                try
                {
                    file.createNewFile();
                    FileOutputStream out=new FileOutputStream("count.txt");
                    DataOutputStream dataOut=new DataOutputStream(out);
                    out.close();
                    dataOut.close();
                }
                catch(IOException ee){}锛?
            }
            else
            {
                try
                {
                    FileInputStream in=new FileInputStream("count.txt");
                    DataInputStream dataIn=new DataInputStream(in);
                    i=dataIn.readInt();
                    i++;
                    in.close();
                    dataIn.close();
                    FileOutputStream out=new FileOutputStream("count.txt");
                    DataOutputStream dataOut=new DataOutputStream(out);
                    dataOut.writeInt(i);
                    out.close();
                    dataOut.close();
                }
                catch(IOException ee){};
            }
        }        
    %>
  <% count(); %>

您是第<%=i %>个访问本站的客户</td>
</tr>
</table>
</center>
</body>
</html>




下面是错误提示:
An error occurred at line: 16 in the jsp file: /Hello.jsp
Syntax error on tokens, delete these tokens
13: </tr>
14: <tr>
15: <td>
16: <%!
17: int i=0;
18: File file=new File("count.txt");
19: synchronized void count()




------解决方案--------------------
在初次访问的时候,你创建txt文件但是最后没有写入数据,所以再读取数据的时候就发生异常
换成下面这样就OK了
HTML code<%@ page language="java" contentType="text/html;charset=GB2312" %>
<%@ page import="java.io.*" %> //导入包
<html>
<head>
<title>第一个JSP访问页面</title>
</head>
<body>
<center>
<table style="border:#000000 double 1px;">
<tr>
<td>
<%= "第一个JSP访问页面" %></td>
</tr>
<tr>
<td>
<%!
int i=0;
File file=new File("count.txt");
synchronized void count()
{
if(!file.exists())
{
i++;
try
{
file.createNewFile();
FileOutputStream out=new FileOutputStream("count.txt");
DataOutputStream dataOut=new DataOutputStream(out);
dataOut.writeInt(1);
out.close();
dataOut.close();
}
catch(IOException ee){
ee.printStackTrace();
}
}
else
{
try
{
FileInputStream in=new FileInputStream("count.txt");
DataInputStream dataIn=new DataInputStream(in);
i=dataIn.readInt();
i++;
in.close();

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有