📄 qshell.java
字号:
// servlet QShell, 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 MyNa.utils.DBHandler; // communicate with database
import MyNa.utils.RowSequence; // database results
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 QShell extends HttpServlet {
final String filePath = "MyNa/QShell/"; // C:\JRun\jsm-default\MyNa\QShell
final String iniFileName = "QShell.ini"; // defaults for servletRequest
final String topFileName = "top.htm"; // or override with getInitParam
final String ctlFileName = "ctl.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");
if("InitDBHandler".equals(dbOperation))lg.clearLog();
if("InitDBHandler".equals(dbOperation))doInit(sess,E,W);
else if(dbOperation==null)sendCtl(sess,E,W);
else if("Logout".equals(dbOperation))doEnd(sess,W);
else doQuery(sess,E,W);
}catch(Exception ex){
W.wrapPage("doPost failure",Logger.stackTrace(ex));}
}
/* A normal session will involve one call on doInit, from
the login page (e.g. QShellLogin.htm); this causes us to read
definitions, e.g. from QShell.ini, and send a page such
as top.htm; top.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 top.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. If more than one
QueryShell is desired, we simply give an alias with an
initParameter of filePath (relative to the servlet-default
filepath, such as C:\JRun\jsm-default) which points us in
the right direction.
*/
public void doInit(HttpSession sess,Env E,HtmlWrapper W)
throws SQLException,Exception {
String fP=setStr(E,"filePath","",filePath);
if(!fP.endsWith("/"))E.put("filePath",fP+="/");
sess.putValue("filePath",fP);
String ini=setStr(E,"iniFileName",fP,iniFileName);
E.addBufferedReader(MiscFile.getBufferedReader(ini));
setStr(E,"templateFile",fP,"topFileName",topFileName);
String myURL=E.getStr("dbServlet");
if(myURL.indexOf('?')<0)
E.put("dbServlet",myURL+"?dummyField=dummyVal");
sess.putValue("theDBHandler",new DBHandler(E));
W.wrapEnvPage(E); // the Env defines output
}
public void sendCtl(HttpSession sess,Env E, HtmlWrapper W){
// normally used to fill in a frame, but as a response
// to null query this is a usage message.
String fP=(String)sess.getValue("filePath");
setStr(E,"templateFile",fP,"ctlFileName",ctlFileName);
lg.logIt("sendCtl(filePath="+fP+"): "+E.toString());
W.wrapEnvPage(E);
}
public void doQuery(HttpSession sess,Env env,HtmlWrapper W)
throws SQLException,IOException {
// process user query
lg.logIt("doQuery: "+env.toString());
String fP=(String)sess.getValue("filePath");
DBHandler dbH=(DBHandler)sess.getValue("theDBHandler");
if(dbH==null)
W.wrapPage("doPost Failure","No dbhandler in sess "+sess.getId());
else {
setStr(env,"templateFile",fP,env.getStr("dbOperation")+".htm");
RowSequence rows=dbH.getQueryRows(env);
W.wrapRowsPage(rows,env); // again the Env defines output.
}
dbH.gotoSleep();
}
public void doEnd(HttpSession sess, HtmlWrapper W)
throws IOException,SQLException {
lg.logIt("doEnd sess "+sess.getId());
DBHandler dbH=(DBHandler)sess.getValue("theDBHandler");
if(dbH!=null)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 + -