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

📄 query3.java

📁 java web 开发,Java Xml 编程指南书籍源码
💻 JAVA
字号:
// servlet Query3, which really doesn't know what it's doing
// it is aware that "InitDBHandler" means it should start up
// a new session containing a DBHandler initialized with the 
// environment provided by the servlet request; it is aware
// that LOGOUT means it should kill the session. All the
// information about what queries are to be used, and how to
// present the results, comes from Somewhere Else: in this
// case, the queries are defined in the initdbhandler html
// form, and each query can refer to an html template file
// for output.


import javax.servlet.*;  // communicate with client
import javax.servlet.http.*; 

import MyNa.utils.Logger;  // saves admin/debug info to file

import MyNa.utils.Env;   // basic package
import MyNa.utils.MiscFile;   // basic package
import java.util.Enumeration;   // 
import MyNa.utils.DBHandler;   // communicate with database
import java.sql.SQLException;   // thrown by LookerUpper

import MyNa.utils.HtmlWrapper; // sends HTML to client.
import java.io.IOException; // thrown by HtmlWrapper
import MyNa.utils.MiscDate; // for logging.


public class Query3 extends HttpServlet { 
  String filePath = "MyNa/Query3/"; // C:\JRun\jsm-default\MyNa\Query3
  String iniFileName = "Q3.ini";    // defaults for servletRequest
  String topFileName = "Q3top.htm";  // or override with getInitParam
  String ctlFileName = "Q3ctl.htm";
  Logger lg;

  public void doGet (HttpServletRequest req, 
                     HttpServletResponse res)
          throws ServletException, IOException{
    doPost(req,res);
  }

  public void doPost (HttpServletRequest req, 
                     HttpServletResponse res)
          throws ServletException, IOException{
    res.setContentType("text/html");
    HtmlWrapper W=new HtmlWrapper(res.getWriter());
    HttpSession sess=req.getSession(true);
    lg=new Logger();
    try{
      Env E=new Env(req);
      String myURL=res.encodeURL(req.getRequestURI());
      E.put("dbServlet",myURL);
      String dbOperation=E.getStr("dbOperation");
      lg.logIt("dbOperation="+dbOperation+"; dbServlet="+E.getStr("dbServlet"));
      filePath=setStr(E,"filePath","",filePath);
      if("InitDBHandler".equals(dbOperation))doInit(sess,E,W);
      else if(dbOperation==null)sendCtl(E,W);
      else if("Logout".equals(dbOperation))doEnd(sess,W);
      else doQuery(sess,E,W);
    }catch(Exception ex){
      W.wrapPage("doPost failure",""+ex);}
  }

/* A normal session will involve one call on doInit, from
 the login page (e.g. login3.htm); this causes us to read
 definitions, e.g. from Q3.ini, and send a page such 
 as Q3top.htm; Q3top.htm is a frame page which invokes a 
 re-call on the servlet to fill in one frame with sendCtl.
 Subsequent calls will involve doQuery, until we read doEnd.
 If frames are not desired, the ini file or login page can 
 define the  "topFileName" to be produced instead of top3.htm; 
 this  will presumably be a control frame itself. The values
 of "topFileName", "iniFileName", and "ctlFileName" are sought
 first in the servlet init parameters, then in the request,
 and finally as default values provided here.
*/

public void doInit(HttpSession sess,Env E,HtmlWrapper W)
    throws SQLException,Exception {
    // read ini file, init DBHandler, fix "dbServlet"; this
    // URL may appear outside a form, so _must_ have an argument;
    // FRAME src="/servlet/Query3" does not invoke the servlet
    // whereas src="/servlet/Query3?dummyField=dummyVal" does
  filePath = setStr(E, "filePath", "", filePath);
  String ini=setStr(E,"iniFileName",filePath,iniFileName);
  E.addBufferedReader(MiscFile.getBufferedReader(ini));
  setStr(E,"templateFile",filePath,"topFileName",topFileName);
  String myURL=E.getStr("dbServlet");
  if(myURL.indexOf('?')<0)E.put("dbServlet",myURL+"?dummyField=dummyVal");
  sess.putValue("theDBHandler",new DBHandler(E));
  lg.logIt("Q3.doInit template: "+E.getStr("templateFile"));
  W.wrapEnvPage(E); // the Env defines output
}

public void sendCtl(Env E, HtmlWrapper W){
    // normally used to fill in a frame, but as a response
    // to bad query this is a usage message.
  setStr(E,"templateFile",filePath,"ctlFileName",ctlFileName);
  lg.logIt("q3.sendCtl: templateFile="+E.getStr("templateFile"));
  W.wrapEnvPage(E); 
}

public void doQuery(HttpSession sess,Env E,HtmlWrapper W)
    throws SQLException,IOException {
  // process user query
  DBHandler dbH=(DBHandler)sess.getValue("theDBHandler");
  if(dbH==null)
    W.wrapPage("doPost Failure","No dbhandler in sess "+sess.getId());
  else {
    Env result=dbH.getQueryResult(E); // use DBHandler
  lg.logIt("q3.doQuery: templateFile="+E.getStr("templateFile"));
    W.wrapEnvResultPage(result); // send result to HtmlWrapper
    }
}

public void doEnd(HttpSession sess, HtmlWrapper W)
    throws IOException,SQLException {
  DBHandler dbH=(DBHandler)sess.getValue("theDBHandler");
  if(null!=dbH)dbH.close();
  sess.invalidate();
  W.wrapPage("Session Ends","come back soon"); 
}


public String setStr(Env E,String resNm,String pre,String nm,String dflt){
  //sets E[resNm] to pre+(getInitParameter(nm) or E[nm] or dflt)
  String val=getInitParameter(nm);
  if(null==val)val=E.getStr(nm);
  if(null==val)val=dflt;
  E.put(resNm,pre+val);
  return pre+val;
}
public String setStr(Env E,String nm,String pre,String dflt){
  //sets E[nm] to pre+(getInitParameter(nm) or E[nm] or dflt)
  return setStr(E,nm,pre,nm,dflt);
}

}


⌨️ 快捷键说明

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