爱心技术专栏专题

一个J2EE实现的反面例子续(java代码)

摘录:java 来源:java 加入时间:2006年08月11日
摘要:
一个J2EE实现的反面例子续(java代码)

  1. Index.java
  2. import classnotepad.*;
  3. import javax.servlet.http.*;

  4. 转载:转载请保留本信息,本文来自
    http://www.51dibs.com
    /html/2006/article/info11/a_880f304f3f3a384c.htm

一个J2EE实现的反面例子续(java代码)

站点:爱心种子小博士 关键字:一个J2EE实现的反面例子续(java代

   
一个J2EE实现的反面例子续(java代码)
  1. Index.java
  2. import classnotepad.*;
  3. import javax.servlet.http.*;
  4. import javax.servlet.*;
  5. import java.io.*;
  6. public class index extends  {
  7.     public static ConnectionPool pool;
  8.     /** init the index servlet and init db connection pool*/
  9.     public void init( config) throws {
  10.         super.init(config);
  11.         pool = ConnectionPool.instance();
  12.         pool.createConnection();
  13.     }
  14.     public void doGet( request,  response) throws , {
  15.         response.setContentType("text/html; charset=gb2312");
  16.          out = response.getWriter();
  17.         out.println("<html>");
  18.         out.println("<title>XX同学留言簿</title>");
  19.         out.println("        <body bgColor=#456589 >");
  20.         out.println("            <head><CENTER><h2>XX同学留言簿登陆页面 </head>");
  21.         out.println("            <form action=/ustc9611/liuyan.jsp method=POST>");
  22.         out.println("              请输入登陆名 <CENTER>");
  23.         out.println("              姓名:<input type=text name=id><CENTER><BR>");
  24.         out.println("              请输入密码 <CENTER>");
  25.         out.println("              密码:<input type=password name=passwd><CENTER><BR>");
  26.         out.println("              <input type=submit value=确定><input type=reset value=取消><CENTER>");
  27.         out.println("            </form>");
  28.         out.println("            <form action=/ustc9611/zhuce.html method=POST>");
  29.         out.println("              注册新用户");
  30.         out.println("              <input type=submit value=确定>");
  31.         out.println("            </form>");
  32.         out.println("        <BR>");
  33.         out.println("        <form action=/ustc9611/visit.jsp method=POST>");
  34.         out.println("            查询同学最近登陆时间<input type=submit value=确定>");
  35.         out.println("        </form>");
  36.         out.println("        </body>");
  37.         out.println("</html>");
  38.     }
  39.     
  40.     public void doPost( request,  response) throws , {
  41.         doGet(request,response);
  42.     }
  43.     
  44.     /** destroy the index servlet and close the connections in pool*/
  45.     public void destroy(){
  46.         super.destroy();
  47.         pool.closeConnection();
  48.         pool = null;
  49.     }
  50. }
  51. LiuYanBean.java
  52. package classnotepad;
  53. import java.util.*;
  54. import java.io.*;
  55. import java.sql.*;
  56. import javax.servlet.jsp.*;
  57. public class LiuYanBean{
  58.     private  id;
  59.     private  text;
  60.     private  subject;
  61.     private  passwd;
  62.     
  63.     private  hash;
  64.     private  conn;
  65.     private  statement;
  66.     private  stmt;
  67.     private static int classID = 1;
  68.     private ConnectionPool pool;
  69.     
  70.     public LiuYanBean(){
  71.       id = null;
  72.       text = null;
  73.       pool = ConnectionPool.instance();
  74.     }
  75.     
  76.     /** id */
  77.     public void setId( ID){
  78.       id = ID;
  79.     }
  80.     public  getId(){
  81.       return id;
  82.     }
  83.     
  84.     /** passwd */
  85.     public  getPasswd(){
  86.       return passwd;
  87.     }
  88.     public void setPasswd( pwd){
  89.       passwd = pwd;
  90.     }
  91.     
  92.     /** text */
  93.     public void setText( ptext){
  94.       text = ptext;
  95.     }
  96.     public  getText(){
  97.       return text;
  98.     }
  99.     
  100.     /** subject */
  101.     public void setSubject( psub){
  102.       subject = psub;
  103.     }
  104.     public  getSubject(){
  105.       return subject;
  106.     }
  107.     
  108.     /** show the registed users leaved words */
  109.     public void show( out){
  110.       try{
  111.           openConnection();
  112.           statement = conn.createStatement();
  113.           
  114.            select = "select user_name,title,context,create_dt from coninfo where class_id ="
  115.                             +classID+" order by create_dt DESC";
  116.            set = statement.executeQuery(select);
  117.           while(set.next()){
  118.               out.println("留言人:"+set.getString(1));
  119.               out.println("<p>");
  120.               out.println("标题:"+set.getString(2));
  121.               out.println("<p>");
  122.               out.println("<pre>");
  123.           //    byte[] db = set.getString(3).getBytes("ISO-8859-1");
  124.               out.println( set.getString(3) );
  125.               out.println("</pre>");
  126.               out.println("<p>");
  127.               out.println("<right>留言时间:"+set.getString(4));
  128.               out.println("<p>");
  129.               out.println("<center>-------------------------------------------------------------------------------------------------------------------</center>");
  130.               out.println("<p>");
  131.               out.println("<BR>");
  132.           }
  133.       }catch( sqle){sqle.printStackTrace();}
  134.       catch( ioe){ioe.printStackTrace();}
  135. //      catch(java.io.UnsupportedEncodingException ue){ue.printStackTrace();}
  136.       finally{
  137.           closeConnection();
  138.       }
  139.     }
  140.     
  141.     /** show the registed users visiting dates */
  142.     public void showVisitDate( out){
  143.       try{
  144.           openConnection();
  145.           statement = conn.createStatement();
  146.            select = "select user_name,visit_dt from passwd where class_id ="
  147.                             +classID+" order by visit_dt DESC";
  148.            set = statement.executeQuery(select);
  149.           while(set.next()){
  150.             out.println("<pre>");
  151.               out.println("访问者:"+set.getString(1)+"        最新访问时间:"+set.getString(2));
  152.             out.println("---------------------------------------------------------------------------------------------------------------");
  153.               out.println("</pre>");
  154.           }
  155.       }catch( sqle){sqle.printStackTrace();}
  156.       catch( ioe){ioe.printStackTrace();}
  157. //      catch(java.io.UnsupportedEncodingException ue){ue.printStackTrace();}
  158.       finally{
  159.           closeConnection();
  160.       }
  161.     }
  162.     public void addLiuYan(){
  163.       if(subject==null || text==null)    return;
  164.       try{
  165.             openConnection();
  166.             statement = conn.createStatement();
  167.             java.util. dt = .getInstance().getTime();
  168.              insert = "insert into coninfo(class_id,user_name,title,context,create_dt) values("+classID+","
  169.                             //+new String(id.getBytes(),"ISO-8859-1")+","+new String(subject.getBytes(),"ISO-8859-1")+","+new String(text.getBytes(),"ISO-8859-1")+","+"Date())";//Date()是access的内部函数                            
  170.                             +id+","+subject+","+text+","+"getdate())";//Date()是access的内部函数                            
  171.             statement.executeUpdate(insert);
  172.       }catch( sqle){sqle.printStackTrace();}
  173. //      catch(java.io.UnsupportedEncodingException ue){ue.printStackTrace();}
  174.       finally{
  175.           closeConnection();
  176.       }
  177.     }
  178.     public boolean check(){
  179.         boolean flag = false;
  180.         if (id == null ) return flag;
  181.         try{
  182.             openConnection();
  183.             statement = conn.createStatement();
  184.              search = "select * from passwd where class_id = "+classID+" and user_name = "+id+" and passwd = "+passwd+"";
  185.              set = statement.executeQuery(search);
  186.             if(set.next()){
  187.                 flag = true;
  188.             }
  189.             
  190.             if(flag){
  191.                  buffer = new ();
  192.                 buffer.append("update passwd set visit_dt = getdate() where class_id = ");
  193.                 buffer.append(classID);
  194.                 buffer.append(" and user_name = ");
  195.                 buffer.append(id);
  196.                 buffer.append(" and passwd = ");
  197.                 buffer.append(passwd);
  198.                 buffer.append("");
  199.                 statement.executeUpdate(buffer.toString());
  200.             }
  201.         }catch( sqle){sqle.printStackTrace();
  202.         }finally{
  203.             closeConnection();
  204.         }
  205.         return flag;
  206.     }
  207.     
  208.     private void openConnection(){
  209.         hash  = pool.getConnection();
  210.         if(hash.elements().hasMoreElements()){
  211.             conn = ()hash.elements().nextElement();
  212.         }
  213.     }
  214.     
  215.     private void closeConnection(){
  216.         try{
  217.             if(statement!= null){
  218.                 statement.close();
  219.             }
  220.             if(stmt!=null){
  221.                 stmt.close();
  222.             }
  223.             statement = null;
  224.             stmt = null;
  225.             pool.putConnection(hash);
  226.         }catch( sqle){sqle.printStackTrace();}
  227.     }
  228. }
  229. UploadBean.java
  230. package classnotepad;
  231. import java.util.*;
  232. import java.io.*;
  233. import java.sql.*;
  234. import javax.servlet.jsp.*;
  235. public class UploadBean{
  236.     private  id;
  237.     private  passwd;
  238.     private  imageName;
  239.     private  subject;
  240.     
  241.     private  hash;
  242.     private  conn;
  243.     private  statement;
  244.     private  stmt;
  245.     private static int classID = 1;
  246.     private ConnectionPool pool;
  247.     
  248.     public UploadBean(){
  249.       id = null;
  250.       imageName = null;
  251.       pool = ConnectionPool.instance();
  252.     }
  253.     
  254.     /** id */
  255.     public void setId( ID){
  256.       id = ID;
  257.     }
  258.     public  getId(){
  259.       return id;
  260.     }
  261.     
  262.     /** passwd */
  263.     public  getPasswd(){
  264.       return passwd;
  265.     }
  266.     public void setPasswd( pwd){
  267.       passwd = pwd;
  268.     }
  269.     
  270.     
  271.     /** imageName */
  272.     public void setImageName( imgName){
  273.         imageName = imgName;
  274.     }
  275.     
  276.     public  getImageName(){
  277.         return imageName;
  278.     }
  279.     /** subject */
  280.     public void setSubject( sub){
  281.         subject = sub;
  282.     }
  283.     
  284.     public  getSubject(){
  285.         return subject;
  286.     }
  287.     public void addImageInfo(){
  288.       if( imageName == null )    return;
  289.       try{
  290.             openConnection();
  291.             statement = conn.createStatement();
  292.             java.util. dt = .getInstance().getTime();
  293.              insert = "insert into imageinfo(class_id,create_dt,image_name,title) values("+classID+","
  294.                             //+new String(id.getBytes(),"ISO-8859-1")+","+new String(subject.getBytes(),"ISO-8859-1")+","+new String(text.getBytes(),"ISO-8859-1")+","+"Date())";//Date()是access的内部函数                            
  295.                             +"getdate(),"+imageName+","+subject+")";//Date()是access的内部函数                            
  296.             statement.executeUpdate(insert);
  297.       }catch( sqle){sqle.printStackTrace();}
  298. //      catch(java.io.UnsupportedEncodingException ue){ue.printStackTrace();}
  299.       finally{
  300.           closeConnection();
  301.       }
  302.     }
  303.     
  304.     private void openConnection(){
  305.         hash  = pool.getConnection();
  306.         if(hash.elements().hasMoreElements()){
  307.             conn = ()hash.elements().nextElement();
  308.         }
  309.     }
  310.     
  311.     private void closeConnection(){
  312.         try{
  313.             if(statement!= null){
  314.                 statement.close();
  315.             }
  316.             if(stmt!=null){
  317.                 stmt.close();
  318.             }
  319.             statement = null;
  320.             stmt = null;
  321.             pool.putConnection(hash);
  322.         }catch( sqle){sqle.printStackTrace();}
  323.     }
  324. }
  325. AddUserBean.java
  326. package classnotepad;
  327. import java.util.*;
  328. import java.sql.*;
  329. public class AddUserBean{
  330.     private  id;
  331.     private  passwd;
  332.     private  confirm;
  333.     private  connection;
  334.     private  statement;
  335.     private  stmt;
  336.     private static  hash;
  337.     private ConnectionPool pool;
  338.     
  339.     public AddUserBean(){
  340.       id = null;
  341.       passwd = null;
  342.       pool = ConnectionPool.instance();
  343.     }
  344.     
  345.     public void setId( ID){
  346.       id = ID;
  347.     }
  348.     public  getId(){
  349.       return id;
  350.     }
  351.     public void setConfirm( con){
  352.       confirm = con;
  353.     }
  354.     public  getConfirm(){
  355.       return confirm;
  356.     }
  357.     public void setPasswd( pswd){
  358.       passwd = pswd;
  359.     }
  360.     public  getPasswd(){
  361.       return passwd;
  362.     }
  363.     public boolean isNewUser(){
  364.         try{
  365.             openConnection();
  366.             statement = connection.createStatement();
  367.              search = "select * from passwd where class_id=1 and user_name= "+id+"";
  368.              set = statement.executeQuery(search);
  369.             if( set.next()){
  370.                 return false;
  371.             }
  372.         }catch( sqle){sqle.printStackTrace();}
  373.         finally{closeConnection();}
  374.         return true;
  375.     }
  376.     
  377.     public boolean addUser(){
  378.       try{
  379.           openConnection();
  380.           statement = connection.createStatement();
  381.            insert = "insert into passwd(class_id,user_name,passwd) values( 1,"
  382.                           +""+this.id+","+this.passwd+")";
  383.           statement.executeUpdate(insert);
  384.           return true;
  385.       }catch( sqle){sqle.printStackTrace();}
  386.       finally{
  387.           closeConnection();
  388.       }
  389.       return false;
  390.     }
  391.     
  392.     private void openConnection(){
  393.         hash  = pool.getConnection();
  394.         if(hash.elements().hasMoreElements()){
  395.             connection = ()hash.elements().nextElement();
  396.         }
  397.     }
  398.     
  399.     private void closeConnection(){
  400.         try{
  401.             if(statement!= null){
  402.                 statement.close();
  403.             }
  404.             if(stmt!=null){
  405.                 stmt.close();
  406.             }
  407.             statement = null;
  408.             stmt = null;
  409.             pool.putConnection(hash);
  410.         }catch( sqle){sqle.printStackTrace();}
  411.     }
  412. }
  413. ConnectionPool.java
  414. package classnotepad;
  415. import java.sql.*;
  416. import java.util.*;
  417. public final class ConnectionPool{
  418.     private  static int DB_MAX = 4;
  419.     private  int STATUS_UNINIT = 1;//未初始化状态
  420.     private  int STATUS_PREPARED = 2;//已经初始化状态
  421.     private  int STATUS_INUSED = 3;//正在使用状态
  422.     private  int STATUS_CLOSED = 4;//关闭状态
  423.     private  int status[] = {STATUS_UNINIT,STATUS_UNINIT,STATUS_UNINIT,STATUS_UNINIT};
  424.     private  static boolean singleFlag = false;
  425.     private  static [] connection = new [DB_MAX];
  426.     private  final  sqlDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
  427.     private  final  url = "jdbc:odbc:liuyan";
  428.     private  final  user = "administrator";
  429.     private  final  passwd = "";
  430.     private static ConnectionPool pool;
  431.     
  432.     /** singleton design pattern */
  433.     private ConnectionPool(){
  434.         for(int i =0;i < DB_MAX;i++){
  435.             connection[i] = null;
  436.         }
  437. //        createConnection();
  438.     }
  439.     
  440.     /** create a instance pool */
  441.     public static synchronized ConnectionPool instance(){
  442.         if(!singleFlag){
  443.             singleFlag = true;
  444.             pool = new ConnectionPool();
  445.         }
  446.         return pool;
  447.     }
  448.     
  449.     /** init connection pool */
  450. &nb;