📄 htmlout.java
字号:
/*
*
*/
import java.io.*;
import java.text.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
*
*
* @author Chen Xin Wu
*/
public class HtmlOut {
public HttpServletResponse m_response;
public ServletOutputStream out ;
public HtmlOuter m_HttpOut,m_HttpErr;
public int m_nError = 0 ;
public String m_sModule;
public String m_sReturnURL;
public String m_sRedirect;
//get out stream by this function;
public HtmlOut(HttpServletResponse response){
try{
m_response = response;
response.setContentType("text/html;charset=GB2312");
out = response.getOutputStream();
m_HttpOut = new HtmlOuter (out);
m_HttpErr = new HtmlOuter (out);
m_sRedirect=null;
m_nError = 0;
m_sReturnURL = "";
}catch(Exception e){
System.out.println(e.toString() );
}
}
//Add string to http out
public void addString(String sAdd){
if(m_HttpOut==null){
//System.out.println("htmlout null!");
return;
}
m_HttpOut.addString (sAdd);
}
public void addStringPlain(String sAdd){
if(m_HttpOut==null)
System.out.println("htmlout null!");
m_HttpOut.addString(CommonMethods.makeHtmlString (sAdd));
}
public void setRedirect(String sDir){
m_sRedirect = sDir;
}
public boolean addCookie(String sCName,String sCValue){
Cookie cookie = new Cookie (sCName,sCValue);
if( m_response!=null && cookie!=null ){
m_response.addCookie (cookie);
return true;
}
return false;
}
public boolean addCookie(Cookie c){
if( m_response!=null && c!=null ){
m_response.addCookie (c);
return true;
}
return false;
}
//add and out error message;
public void setErrorID(int ErrorID , String sModule , String sError ,
String sReturn){
m_nError = ErrorID;
m_sModule= sModule;
m_HttpErr.addString (m_sModule+sError);
m_sReturnURL = sReturn;
}
public void outHtml(){
try{
//first if redirect ...
if( m_sRedirect !=null ){
if( m_sRedirect.indexOf (".htm" )!=-1
||m_sRedirect.length()==0 )//static web page
{
if( m_sRedirect.length()==0 )
m_response.sendRedirect( "/reindex.htm" );
else
m_response.sendRedirect( CommonMethods.GetWebRootDir()+m_sRedirect );
}
else // cgi path;
m_response.sendRedirect(m_sRedirect);
}
if(m_nError==0)
m_HttpOut.outString ();
else{
OutError();
}
}catch(Exception e){
}
}
public boolean writeHtml( String sFile )
{
try{
CommonMethods.WriteToDisk ( m_HttpOut.m_Buffer .getData(),
m_HttpOut.m_Buffer .getSize(),
sFile);
}catch(Exception e){
}
return true;
}
//out error message ;
void OutError(){
try{
if( m_nError==USER_NEED_LOGIN )
m_response.sendRedirect(CommonMethods.GetWebRootDir());
else if( m_nError==SHOW_SIMPLE_MESSAGE_WITHCLOSE ){
OutSimpleMessageByTemplate();
}
else{
OutSimpleErrorByTemplate();
}
}catch(Exception e){
}
}
//out error message by the template;
void OutSimpleErrorByTemplate(){
System.out.println("out error...");
TemplateList tempList = CommonMethods.getErrorTemplate();
if( tempList==null ){
System.out.println("templaste null1");
m_HttpErr.outString ();
}
SingleTemplate st = tempList.searchTemplate ("ROOT");
if( st==null ){
System.out.println("templaste null2");
m_HttpErr.outString ();
}
int nKey = st.getKeyNum ();
String sKey;
for(int i=0;i<nKey;i++){
m_HttpErr.outStr (st.getStringAt (i));
sKey =st.getKeyAt (i);
if( sKey .compareTo("cgi-path")==0 )
m_HttpErr.outStr (CommonMethods.sHostNameCGI);
if(sKey.compareTo ( "ErrorMessage")==0 ){
m_HttpErr.outString ();
}
if( sKey.compareTo ("ReturnURL")==0 ){
if( m_sReturnURL==null )
m_sReturnURL="";
if( m_sReturnURL.length ()==0||m_sReturnURL.indexOf (".htm" )!=-1 ){//static web page
if( m_sReturnURL.length ()==0 )
m_HttpErr.outStr ("/reindex.htm");
else
m_HttpErr.outStr ( CommonMethods.GetWebRootDir()+m_sReturnURL );
}
else // cgi path;
m_HttpErr.outStr ( m_sReturnURL);
}
}
m_HttpErr.outStr (st.getEndString ());
}
void OutSimpleMessageByTemplate(){
TemplateList tempList = CommonMethods.getMessageTemplate();
if( tempList==null ){
m_HttpErr.outString ();
}
SingleTemplate st = tempList.searchTemplate ("ROOT");
if( st==null ){
m_HttpErr.outString ();
}
int nKey = st.getKeyNum ();
String sKey;
for(int i=0;i<nKey;i++){
m_HttpErr.outStr (st.getStringAt (i));
sKey =st.getKeyAt (i);
if( sKey .compareTo("cgi-path")==0 )
m_HttpErr.outStr (CommonMethods.sHostNameCGI);
if(sKey.compareTo ( "ErrorMessage")==0 ){
m_HttpErr.outString ();
}
}
m_HttpErr.outStr (st.getEndString ());
}
//error ID
static public int USER_NEED_LOGIN = 1;
static public int TEMPLATE_NOTFIND = 2;
static public int SINGLE_TEMPLATE_NOTFIND = 3;
static public int CHANNEL_NOT_FIND = 4;
static public int DATABASE_CANT_CONNECT = 5;
static public int DATABASE_OPER_ERROR = 6;
static public int SHOW_SIMPLE_MESSAGE = 7;
static public int SHOW_SIMPLE_MESSAGE_WITHCLOSE = 8;
//for cxw
//for zjm
static public int USER_HAS_BEEN_EXISTED = 100;
static public int USER_HAS_SUCCEEDED_TO_CHANGE_HIS_INFOMATION = 101;
static public int USER_ANSWER_IS_INCORRECT = 102;
static public int USER_HAS_NOT_BEEN_EXISTED = 103;
static public int USER_HAS_SUCCEEDED_TO_REGISTE =104;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -