⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 birthdaybean.java

📁 java web 开发,Java Xml 编程指南书籍源码
💻 JAVA
字号:
package birthday;

import javax.servlet.http.*;
import java.util.Vector;
import java.io.BufferedReader;
import java.io.StringReader;
import java.util.Enumeration;
import javax.mail.MessagingException;

import java.sql.SQLException;
import MyNa.utils.*;

/* This class is a bean, directed by a JSP page which handles the display.
It illustrates a JSP<-->bean<-->database interaction, where the dbase is
a simple table of birthdays and email addresses:
CREATE TABLE Birthdays(Who VARCHAR,When DATE, 
The idea is to mail birthday announcements to members of a list:
   we use a DBHandler which finds who (if anyone) has a birthday today,
   we use JSP to initialize a message,
   we send the message.

The initial html provides the DBHandler properties
  dbUser,dbPwd (which the user must normally enter)
  along with dbDriver, dbName, dbQueries and the
  individual query definitions (with types if needed)
  as required by DBHandler. 
    We've already shown how to do this within an init file for
  security; here we show how to do it within a textarea called
  "initdefs" for flexibility. Finally, the initial html
  defines dbOperation (if this is an operation with params,
  then Parameter1, Parameter2 &c must be provided also).

The initial html also sets bean properties 
  bbcmd (what next?) = login;
  when (what date are we thinking about?); format yyyy-MM-dd
  who (defaults as dbUser)  
  retaddr (user's return email address)
  smtphost (mail send service machine)

The jspbean sets bbcmd and others as required;
  logout needs no other data;
  dodb needs a dbOperation (possibly with Parameter1, ..)
  send needs smtphost,retaddr,toaddr,subject,msgtext
*/
public class BirthdayBean {
    
  String bbcmd=null; //BirthdayBeanCommand from JSP specifies action
         // login; dodb; send; logout;
  String jspcmd=null; // JSPCommand from BBean specifies display
        // birthdaylist; list; msgsent; change; error; logout

  String who=null; String when=null; String retaddr=null;

  String toaddr=null; String smtphost=null; String mailuser=null;
  String subject=null; String msgtext=null; String initdefs=null;

  Env env=null; String errorString=null;

  DBHandler dbh=null; RowSequence rows=null;
  String[][]resultTable=null; String numberAffected=null;
 
  
  public BirthdayBean(){}

public void setBbcmd(String S){bbcmd=S;}
public void setWhen(String S){if(null!=S)when=S;}
public void setRetaddr(String S){retaddr=S;}
public void setToaddr(String S){toaddr=S;}
public void setSmtphost(String S){smtphost=S;}
public void setSubject(String S){subject=S;}
public void setMsgtext(String S){msgtext=S;}
public void setInitdefs(String S){initdefs=S;}

public void setNumberAffected(String S){
 jspcmd="change"; numberAffected=S;
}

public void setResultTable(String[][]S){
 jspcmd="list"; resultTable=S;
}
public void setRows(RowSequence re){
 jspcmd="enum"; rows=re;
}
public void setErrorString(String S){
  errorString=S;
  if(null!=S && S.length()>0)jspcmd="error";
}
public String getErrorString(){return errorString;}
public String getNumberAffected(){return numberAffected;}
public String getWhen(){return when;} // to a new jsp page


public String[][] getResultTable(){return resultTable;}
public String getJspcmd(){return jspcmd;}
public Env getEnv(){return env;}

public boolean shouldDoDB(){
  return "login".equalsIgnoreCase(bbcmd) || "dodb".equalsIgnoreCase(bbcmd);
}

public void processRequest(HttpServletRequest request){
  try{
  if(shouldDoDB()){
    try{
    env=new Env(request);
    }catch(java.lang.NullPointerException e){
      setErrorString("null in Env init"); return;
    }
    if(null==env)setErrorString("can't initialize env from request");
    else if(null!=initdefs){
      StringReader sr=new StringReader(initdefs);
      BufferedReader brin=new BufferedReader(sr);
      env.addBufferedReader(brin);}         
      }
 try{    if("login".equalsIgnoreCase(bbcmd))dbh=new DBHandler(env);
    }catch(java.lang.NullPointerException e){
      setErrorString("null in DBHandler init"); return;
    }
 }catch(ParseSubstException e){setErrorString("bad initdefs"+e);
 }catch(Exception e){setErrorString("dbhandler failure: "+e);}
}
public void doCommand(){
  if("logout".equalsIgnoreCase(bbcmd))doLogout();
  else if("send".equalsIgnoreCase(bbcmd))sendMessage();
  else if(shouldDoDB())doDB();
  else setErrorString("unrecognized command ["+bbcmd+"]");
}

public void doLogout(){
  jspcmd="logout";
  try{if(dbh!=null)dbh.close();}
  catch(java.sql.SQLException e){setErrorString("query failure: "+e);}
}

public void sendMessage(){
  try{
    MiscMail.sendMail(smtphost,retaddr,toaddr,subject,msgtext);
    jspcmd="msgsent";
  }catch(MessagingException e){setErrorString("send failure: "+e);}
}

public void doDB(){
  env.remove("NumberOfRowsAffected");
  env.remove("ResultTable");
  try{dbh.getQueryResult(env);}
  catch(java.sql.SQLException e){setErrorString("query failure: "+e);}
  String n=env.getStr("NumberOfRowsAffected");
  if(null!=n){setNumberAffected(n); return;}
  setResultTable((String[][])env.get("ResultTable"));
  if("BIRTHDAYLIST".equalsIgnoreCase(env.getStr("dbOperation")))
    pruneResults();
 }
public void doDB2(){
  env.remove("NumberOfRowsAffected");
//  env.remove("ResultTable");
  RowSequence re=null;
  try{re=dbh.getQueryRows(env);
  String n=env.getStr("NumberOfRowsAffected");
  if(null!=n){setNumberAffected(n); return;}
  if("BIRTHDAYLIST".equalsIgnoreCase(env.getStr("dbOperation")))
    setRows(pruneRows(re,when));
  else setRows(re);
  }catch(java.sql.SQLException e){setErrorString("query failure: "+e);}
 }

public void pruneResults(){
 // when=="1999-03-25", march 25 1999;
 // this is the basic jdbc escape format.
 // we want to prune the result table so that
 // only birthday matches survive.
  jspcmd="birthdaylist";
  if(resultTable.length==0)return;
   String monthDay=when.substring(4); // "-03-25"
  Vector v=new Vector();
  for(int i=0;i<resultTable.length;i++){
    String S=resultTable[i][1]; // the "When" field;
    if(null==S)continue;
    if(S.indexOf(monthDay)!=4)continue;
    v.addElement(resultTable[i]);
    }
  resultTable=new String[v.size()][];
  for(int i=0;i<v.size();i++)resultTable[i]=(String[])v.elementAt(i);
}

public RowSequence pruneRows(RowSequence re,String when)
  throws SQLException {
  // when == "1999-03-25", march 25 1999, jdbc escape format;
  String monthDay=when.substring(4); // "-03-25";
  return new PrunedRowSequence(re,"When",monthDay,4);
}

public class PrunedRowSequence extends RowSequence { 
  String fieldName; String match; int offset;
  // we produce only those elements of re where fieldname
  // matches match beginning at offset.
  public PrunedRowSequence(RowSequence re, String fieldName,
                                String S, int N)
         throws SQLException{
    super();
    initFromRowSequence(re);
    this.fieldName=fieldName;this.match=S;this.offset=N;
  }
  public boolean next(){
    boolean gotAnother=true;
    while((gotAnother=super.next())){
      String val=getRow().getStr(fieldName);
      if(val.substring(offset).startsWith(match))continue;
      }
    return gotAnother;
  }
}
 
}
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -