日期:2014-05-18  浏览次数:20634 次

这是怎么回事啊?
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
{