periodservlet2.java
来自「B/S架构的软件项目实训;包括全部详细文档合源程序」· Java 代码 · 共 64 行
JAVA
64 行
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.util.*;
public class PeriodServlet2 extends HttpServlet
{
static int counter;
public void init()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection("jdbc:odbc:test");
Statement stmt=conn.createStatement();
String strQuery="select * from ViewNum";
ResultSet rs=stmt.executeQuery(strQuery);
boolean firstAccess=true;
while (rs.next())
{
counter=rs.getInt("num");
firstAccess=false;
}
if (firstAccess)
{
counter=0;
String strInsert="INSERT INTO ViewNum VALUES(0)";
stmt.executeUpdate(strInsert);
}
conn.close();
}
catch(Exception e){}
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException
{
response.setContentType("text/HTML;charset=GB2312");
PrintWriter out=response.getWriter();
counter++;
out.println("<HTML>");
out.println("<head>");
out.println("<title> 保持计数器连续</title></head>");
out.println("<body>");
out.println("该Servlet已经被访问 <b>"+String.valueOf(counter)+" </b>次!"+"\n");
out.println("</body>");
out.println("</HTML>");
}
public void destroy()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection("jdbc:odbc:test");
Statement stmt=conn.createStatement();
String strUpdate="UPDATE ViewNum set num="+counter;
stmt.executeUpdate(strUpdate);
conn.close();
}
catch(Exception e){}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?