<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%! int count=0; %> <% String scount; request.getSession(true); if(session.isNew()){ count++; scount=String.valueOf(count); session.putValue("scount", scount); } out.print("您是本站第"); out.print(session.getValue("scount")); out.print("位访客!"); session.invalidate(); %>
<%@ page language="java" contentType="text/html; charset=gb2312" pageEncoding="gb2312"%> <%@ page import="java.util.*,javax.servlet.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <% Object oCount=application.getAttribute("count"); Integer count=null; if(oCount==null){ count=new Integer(1); application.setAttribute("count",count); }else{ count=(Integer)application.getAttribute("count"); count=new Integer(count.intValue()+1); application.setAttribute("count",count); } %> 您是本站第<%=count.intValue() %>位访客!
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <form action="login.jsp" method="post"> 用户名:<input id="name" name="name" type="text"/></br> 密码:<input id="password" name="password" type="password"/></br> <input type="submit" value="登陆"/><input type="reset" value="重置"/> </form>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <% String username=request.getParameter("name"); String userpassword=request.getParameter("password"); if(username.equals("lcxia")&&userpassword.equals("111")){ out.print("登陆成功!!( ̄▽ ̄)"); }else{ out.print("用户名或密码错误,请重新登陆!"); } %>
<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@page import="java.util.*" %> <% response.setHeader("Refresh", "5"); out.print(new Date().toString()); %>
<%@ page language="java" import="java.sql.*" import="javax.sql.*" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>this is test file!</title> </head> <h1>数据库插入和删除测试</h1> <body> <form action="insert.jsp" method="post"> 用户名:<input name="name" type="text"><br> 密码:<input name="password" type="password"><br> <input name="submit" type="submit" value="插入"> </form> <a href="search.jsp">点击此处进入数据库查询页面</a> </body> </html>
<%@ page language="java" import="java.sql.*" import="javax.sql.*" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <% try { Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test", "root", "1234"); PreparedStatement pstate = conn.prepareStatement( "select * from user", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = pstate.executeQuery(); rs.moveToInsertRow(); rs.updateString(2, request.getParameter("name")); rs.updateString(3, request.getParameter("password")); rs.insertRow(); out.print("用户注册成功!"); } catch (Exception e) { out.print("用户注册失败!"); out.print(e); } %>
<%@ page language="java" import="java.sql.*" import="javax.sql.*" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <html> <title>数据库查询页面</title> <h4>数据库查询</h4>> <% try{ Class.forName("com.mysql.jdbc.Driver"); Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","1234"); PreparedStatement pstate=conn.prepareStatement("select * from user;"); ResultSet rs=pstate.executeQuery(); out.print("<table border=1>"); out.print("<tr><td></td><td>用户名</td><td>密码</td></tr>"); int i=0; while(rs.next()){ out.print("<tr><td>"); out.print(++i); out.print("."); out.print("</td><td>"); out.print(rs.getString(2)); out.print("</td><td>"); out.print(rs.getString(3)); out.print("</td></tr>"); } out.print("</table>"); }catch(Exception e){ out.print(e); } %> <form action="delete.jsp" method="post"> 请输入要删除的序号:<input type="text" name="number"><br> <input type="submit" value="删除"> </form>> </html>
<%@ page language="java" import="java.sql.*" import="javax.sql.*" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <% try { Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test", "root", "1234"); PreparedStatement pstate = conn.prepareStatement( "select * from user", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = pstate.executeQuery(); rs.absolute(Integer.valueOf(request.getParameter("number"))); rs.deleteRow(); } catch (Exception e) { out.print("数据删除失败!"); out.print(e); } %> <a href="search.jsp">返回查询页面</a>
//没有使用书中的方法,使用的是proxool技术
//1、下载proxool 地址:http://proxool.sourceforge.net
//2、解压缩proxool-0.9.0RC2.zip,拷贝lib/proxool-0.9.0RC2.jar到web-info/lib;拷贝jdbc驱动到web-info/lib
//3、在web-info下建立文件:proxool.xml
<?xml version="1.0" encoding="UTF-8"?> <something-else-entirely> <proxool> <alias>Develop</alias> <driver-url>jdbc:mysql://localhost/test</driver-url> <driver-class>org.gjt.mm.mysql.Driver</driver-class> <driver-properties> <property name="user" value="root" /> <property name="password" value="1234" /> </driver-properties> <maximum-connection-count>10</maximum-connection-count> <house-keeping-test-sql>select CURRENT_DATE</house-keeping-test-sql> </proxool> </something-else-entirely>
//4、在web.xml里新增如下:
<servlet> <servlet-name>proxoolServletConfigurator</servlet-name> <servlet-class>org.logicalcobwebs.proxool.configuration.ServletConfigurator</servlet-class> <init-param> <param-name>xmlFile</param-name> <param-value>WEB-INF/proxool.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>Admin</servlet-name> <servlet-class>org.logicalcobwebs.proxool.admin.servlet.AdminServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Admin</servlet-name> <url-pattern>/admin</url-pattern> </servlet-mapping>
//5、在调用数据库连接代码:
Class.forName("org.logicalcobwebs.proxool.ProxoolDriver"); Connection conn = DriverManager.getConnection("proxool.Develop");
//6、利用proxool监控工具查看数据库运行状态。地址:./admin
package com.inoue.test; import java.io.IOException; import java.sql.*; import javax.sql.*; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletDemo extends HttpServlet { public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out=response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet Demo</TITLE></HEAD>"); out.println(" <BODY>"); try{ Class.forName("com.mysql.jdbc.Driver"); Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","1234"); Statement state=conn.createStatement(); int result=state.executeUpdate("insert into user(name,password) values('"+request.getParameter("name")+"','"+request.getParameter("password")+"')"); if(result==1){ out.print("注册成功!"); }else{ out.print("注册失败!"); } }catch(Exception e){ out.print(e); } out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } }
<%@ page language="java" import="java.sql.*" import="javax.sql.*" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>this is login test !!</title> </head> <body> <form action="ServletDemo" method="post"> 用户名:<input name="name" type="text"><br> 密码:<input name="password" type="password"><br> <input type="submit" value="注册"> </form> </body> </html>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>WebTest</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>ServletDemo</servlet-name> <servlet-class>com.inoue.test.ServletDemo</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletDemo</servlet-name> <url-pattern>/ServletDemo</url-pattern> </servlet-mapping> </web-app>