如题所述
ä¸ãåéåæ¹æ³ç声æ
1.å¨<%!ä¸%>ä¹é´æ¾ç½®å£°æåéæ¯é¡µé¢çæååéã声æçåéå¨æ´ä¸ªJSP页é¢é½ææãä¾å¦
<%!
int i=100;
Data date;
%>
å 为JSPå¼æ转æ¢JSP页é¢æJavaæ件æ¶ï¼å°è¿äºåéä½ä¸ºç±»çæååéï¼è¿äºåéçå å空é´ç´å°æå¡å¨å ³éæ被éæ¾ã
æ以ææ请æ±è¯¥é¡µé¢ç线ç¨å ±äº«JSP页é¢çæååéï¼ä»»ä½ç¨æ·å¯¹è¯¥æååéçæä½ç»æé½å½±åå ¶å®å®¢æ·ã
ç®å页é¢è®¡æ°å¨
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<body>
<%!
int i=0;
%>
<%
i++;
%>
<p>
æ¨æ¯ç¬¬<%=i%>个访é®æ¬ç«çç¨æ·ã
</body>
</html>
2. 声ææ¹æ³
å¨<%!å%>ä¹é´å£°æçæ¹æ³å¨æ´ä¸ªJSP页é¢é½ææï¼ä½æ¯è¯¥æ¹æ³å å®ä¹çåéåªå¨è¯¥æ¹æ³å ææãå½å¤ä¸ªç¨æ·è¯·æ±ä¸ä¸ªJSP页é¢æ¶ï¼å¯ä»¥ä½¿ç¨æ¹æ³æä½æååéã
<%@ page contentType="text/htnl;charset=gb2312"%>
<html>
<body>
<%!
int number=0;
synchronized void contPeople()
{
number++;
}
%>
<%
countPeople();
%>
<p>
æ¨æ¯ç¬¬<%=number%>个访é®å®¢æ·ã
</body>
</html>
å°è®¿é®äººæ°ä¿åå°ææ¬æ件ä¸
<%@ page contentType="text/htnl;charset=gb2312"%>
<%@ page import="java.io.*" %>
<html>
<body>
<%!
int number=0;
File file=new File("count.txt");
synchronized void contPeople()
{
if(!file.exists())
{
number++;
try{
file.createNewfile();
FileOutputStream out =new FileOutputStream("count.txt");
DataOutputStream dataOut=new DataOutputStream(out);
dataOut.writeInt(number);
out.close();
dataOut.close();
}
catch(IOException ex){}
}
else
{
try{
FileInputStream in=new FileInputStream("count.txt");
DataInputStream dataIn=new DataInputStream(in);
number=dataIn.readInt();
number++;
in.close();
dataIn.close();
FileOutputStream out =new FileOutputStream("count.txt");
DataOutputStream dataOut=new DataOutputStream(out);
dataOut.writeInt(number);
out.close();
dataOut.close();
}
catch(IOException ex){}
}
}
%>
<%
countPeople();
%>
<p>
æ¨æ¯ç¬¬<%=number%>个访é®å®¢æ·ã
</body>
</html>
1.å¨<%!ä¸%>ä¹é´æ¾ç½®å£°æåéæ¯é¡µé¢çæååéã声æçåéå¨æ´ä¸ªJSP页é¢é½ææãä¾å¦
<%!
int i=100;
Data date;
%>
å 为JSPå¼æ转æ¢JSP页é¢æJavaæ件æ¶ï¼å°è¿äºåéä½ä¸ºç±»çæååéï¼è¿äºåéçå å空é´ç´å°æå¡å¨å ³éæ被éæ¾ã
æ以ææ请æ±è¯¥é¡µé¢ç线ç¨å ±äº«JSP页é¢çæååéï¼ä»»ä½ç¨æ·å¯¹è¯¥æååéçæä½ç»æé½å½±åå ¶å®å®¢æ·ã
ç®å页é¢è®¡æ°å¨
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<body>
<%!
int i=0;
%>
<%
i++;
%>
<p>
æ¨æ¯ç¬¬<%=i%>个访é®æ¬ç«çç¨æ·ã
</body>
</html>
2. 声ææ¹æ³
å¨<%!å%>ä¹é´å£°æçæ¹æ³å¨æ´ä¸ªJSP页é¢é½ææï¼ä½æ¯è¯¥æ¹æ³å å®ä¹çåéåªå¨è¯¥æ¹æ³å ææãå½å¤ä¸ªç¨æ·è¯·æ±ä¸ä¸ªJSP页é¢æ¶ï¼å¯ä»¥ä½¿ç¨æ¹æ³æä½æååéã
<%@ page contentType="text/htnl;charset=gb2312"%>
<html>
<body>
<%!
int number=0;
synchronized void contPeople()
{
number++;
}
%>
<%
countPeople();
%>
<p>
æ¨æ¯ç¬¬<%=number%>个访é®å®¢æ·ã
</body>
</html>
å°è®¿é®äººæ°ä¿åå°ææ¬æ件ä¸
<%@ page contentType="text/htnl;charset=gb2312"%>
<%@ page import="java.io.*" %>
<html>
<body>
<%!
int number=0;
File file=new File("count.txt");
synchronized void contPeople()
{
if(!file.exists())
{
number++;
try{
file.createNewfile();
FileOutputStream out =new FileOutputStream("count.txt");
DataOutputStream dataOut=new DataOutputStream(out);
dataOut.writeInt(number);
out.close();
dataOut.close();
}
catch(IOException ex){}
}
else
{
try{
FileInputStream in=new FileInputStream("count.txt");
DataInputStream dataIn=new DataInputStream(in);
number=dataIn.readInt();
number++;
in.close();
dataIn.close();
FileOutputStream out =new FileOutputStream("count.txt");
DataOutputStream dataOut=new DataOutputStream(out);
dataOut.writeInt(number);
out.close();
dataOut.close();
}
catch(IOException ex){}
}
}
%>
<%
countPeople();
%>
<p>
æ¨æ¯ç¬¬<%=number%>个访é®å®¢æ·ã
</body>
</html>
温馨提示:答案为网友推荐,仅供参考