Hi,
I have a problem with the servletcontext.
At server startup I store some mail parameters in the servletcontext.
I retrieve these parameters from several servlets.
Except in this one (see code below) I can't retrieve the parameters. Yet, I do exactly the same as in all the others servlets in which I can retrieve the parameters.
Does anybody knows what I do wrong?
The servletcontext is always null
Thanks for your help,
Tijs
The Code (see the code between //// BEGIN/END ////):
---------
package com.comparitel.web.util;
//=============================== Imports ============================
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.Date;
import java.text.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.comparitel.util.*;
import com.comparitel.db.*;
import com.comparitel.core.*;
import javax.mail.MessagingException;
import com.javaexchange.dbConnectionBroker.*;
public class ErrorHandler extends HttpServlet{
//============================ Declarations =======================
DisplayMessage sm = new DisplayMessage();
//=========================== Public Methods ======================
/*-----------------------------------------------------------------
* Method: doGet
* Description: This method can only called via the errorhandling in the web.xml file
*
* Due to the xml file, only 1 parameter can be parsed. The xml file doesn't recognise the & symbol
* for multiple parameters
------------------------------------------------------------------*/
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
PrintWriter out = res.getWriter();
// First gets a connection to the DB
DbConnectionBroker pool = (DbConnectionBroker) getServletContext().getAttribute(Global.CONTEXT_CONN_POOL);
Log log = (Log) getServletContext().getAttribute(Global.CONTEXT_LOG);
Connection conn = pool.getConnection();
String strMethod = "web.xml file";
String strErrMsg = req.getParameter("error"
throwError(strMethod, strErrMsg, null, conn, log);
String strMessage = sm.createMessage(Global.MSG_PIC_ERROR);
out.println(strMessage);
out.close();
}
/*-----------------------------------------------------------------
* Method: throwError
* Description: This method will catch all incomming requests from the GUI
------------------------------------------------------------------*/
public void throwError( String strMethod, String strError, StackTraceElement[] ste, Connection conn, Log log ) throws ServletException, IOException {
///////////// BEGIN ///////////////////
ServletContext context = null;
try{
context = getServletContext();
}
catch(Exception ze){
ze.printStackTrace();
}
/////////////// END //////////////////
//PrintWriter out = res.getWriter();
String strMethodError = "ErrorHandler.throwError";
StringBuffer strMsg = new StringBuffer();
StringBuffer strErrMsg = new StringBuffer();
StringBuffer strErrBigMsg = new StringBuffer();
Calendar calendar = new GregorianCalendar();
Date date = calendar.getTime();
DateFormat localDate = DateFormat.getDateInstance();
DateFormat localTime = DateFormat.getTimeInstance();
String strDateTime = localDate.format( date ) + " - " + localTime.format( date );
String strHost = (String)context.getAttribute(Global.MAIL_HOST);
String strUsr = (String)context.getAttribute(Global.MAIL_USR);
String strPwd = (String)context.getAttribute(Global.MAIL_PWD);
String strSMTP = (String)context.getAttribute(Global.MAIL_SMTP);
String strMailTo = (String)context.getAttribute(Global.MAIL_ADDR_ERR);
boolean bSendMail = true;//Util.getBoolean ((String)context.getAttribute(Global.MAIL_ERR) );
boolean bMailOK = false;
Mail mail = null;
//StackTraceElement[] ste = e.getStackTrace(); // get the error stack
//strError = e.toString(); //get the error message
log.out(strMethod, Global.LOG_ERR, "***************************", conn);
log.out(strMethod, Global.LOG_ERR, Global.ERROR_PROCESS_REQ, conn);
log.out(strMethod,Global.LOG_ERR," ", conn);
log.out(strMethod,Global.LOG_ERR," - Message: ", conn);
log.out(strMethod,Global.LOG_ERR, strError, conn);
strErrMsg.append(strError);
strErrMsg.append("\n"
if (ste != null) {
// Loop through the error stack and write it to the log file
for ( int i = 0; i < ste.length; i ++ ){
log.out(strMethod,Global.LOG_ERR, ste.toString(), conn);
strErrMsg.append(ste.toString());
strErrMsg.append("\n"
}
}
log.out(strMethod, Global.LOG_ERR, "***************************\n\n", conn);
strErrBigMsg.append(Global.ERROR_PROCESS_REQ);
strErrBigMsg.append("\n\n"
strErrBigMsg.append(strErrMsg);
if (bSendMail){
mail = new Mail();
strMsg.append("An error occured in the GUI:\n"
strMsg.append("----------------------------\n\n"
strMsg.append("\tDate: "
strMsg.append(strDateTime);
strMsg.append("\n\tServlet & method: "
strMsg.append(strMethod);
strMsg.append("\n\tError message: "
strMsg.append(strErrBigMsg);
strMsg.append("\n\n\n"
strMsg.append("Please check also the log files for with the above date.\n"
mail.mailFrom = "error@GUI.comparitel";
mail.host = strHost;
mail.sendHost = strSMTP;
mail.mailUser = strUsr;
mail.mailPwd = strPwd;
mail.mailTo = strMailTo;
try
{
bMailOK = mail.sendMail(Global.GUI_ERROR_UPPER, strMsg.toString(), conn ,log);
}
catch(MessagingException me)
{
System.out.println("Error getting the mail object"
StackTraceElement[] steMe = me.getStackTrace(); // get the error stack
strError = me.toString();
bSendMail = false;
throwError(strMethodError, "Error while trying to send the error mail (MessagingException)", steMe, conn, log);
me.printStackTrace();
}
if (!(bMailOK)) {
bSendMail = false;
throwError(strMethodError, "Error while trying to send the error mail (Other)", null, conn, log);
}
}
} // end throwError
} // end class
I have a problem with the servletcontext.
At server startup I store some mail parameters in the servletcontext.
I retrieve these parameters from several servlets.
Except in this one (see code below) I can't retrieve the parameters. Yet, I do exactly the same as in all the others servlets in which I can retrieve the parameters.
Does anybody knows what I do wrong?
The servletcontext is always null
Thanks for your help,
Tijs
The Code (see the code between //// BEGIN/END ////):
---------
package com.comparitel.web.util;
//=============================== Imports ============================
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.Date;
import java.text.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.comparitel.util.*;
import com.comparitel.db.*;
import com.comparitel.core.*;
import javax.mail.MessagingException;
import com.javaexchange.dbConnectionBroker.*;
public class ErrorHandler extends HttpServlet{
//============================ Declarations =======================
DisplayMessage sm = new DisplayMessage();
//=========================== Public Methods ======================
/*-----------------------------------------------------------------
* Method: doGet
* Description: This method can only called via the errorhandling in the web.xml file
*
* Due to the xml file, only 1 parameter can be parsed. The xml file doesn't recognise the & symbol
* for multiple parameters
------------------------------------------------------------------*/
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
PrintWriter out = res.getWriter();
// First gets a connection to the DB
DbConnectionBroker pool = (DbConnectionBroker) getServletContext().getAttribute(Global.CONTEXT_CONN_POOL);
Log log = (Log) getServletContext().getAttribute(Global.CONTEXT_LOG);
Connection conn = pool.getConnection();
String strMethod = "web.xml file";
String strErrMsg = req.getParameter("error"
throwError(strMethod, strErrMsg, null, conn, log);
String strMessage = sm.createMessage(Global.MSG_PIC_ERROR);
out.println(strMessage);
out.close();
}
/*-----------------------------------------------------------------
* Method: throwError
* Description: This method will catch all incomming requests from the GUI
------------------------------------------------------------------*/
public void throwError( String strMethod, String strError, StackTraceElement[] ste, Connection conn, Log log ) throws ServletException, IOException {
///////////// BEGIN ///////////////////
ServletContext context = null;
try{
context = getServletContext();
}
catch(Exception ze){
ze.printStackTrace();
}
/////////////// END //////////////////
//PrintWriter out = res.getWriter();
String strMethodError = "ErrorHandler.throwError";
StringBuffer strMsg = new StringBuffer();
StringBuffer strErrMsg = new StringBuffer();
StringBuffer strErrBigMsg = new StringBuffer();
Calendar calendar = new GregorianCalendar();
Date date = calendar.getTime();
DateFormat localDate = DateFormat.getDateInstance();
DateFormat localTime = DateFormat.getTimeInstance();
String strDateTime = localDate.format( date ) + " - " + localTime.format( date );
String strHost = (String)context.getAttribute(Global.MAIL_HOST);
String strUsr = (String)context.getAttribute(Global.MAIL_USR);
String strPwd = (String)context.getAttribute(Global.MAIL_PWD);
String strSMTP = (String)context.getAttribute(Global.MAIL_SMTP);
String strMailTo = (String)context.getAttribute(Global.MAIL_ADDR_ERR);
boolean bSendMail = true;//Util.getBoolean ((String)context.getAttribute(Global.MAIL_ERR) );
boolean bMailOK = false;
Mail mail = null;
//StackTraceElement[] ste = e.getStackTrace(); // get the error stack
//strError = e.toString(); //get the error message
log.out(strMethod, Global.LOG_ERR, "***************************", conn);
log.out(strMethod, Global.LOG_ERR, Global.ERROR_PROCESS_REQ, conn);
log.out(strMethod,Global.LOG_ERR," ", conn);
log.out(strMethod,Global.LOG_ERR," - Message: ", conn);
log.out(strMethod,Global.LOG_ERR, strError, conn);
strErrMsg.append(strError);
strErrMsg.append("\n"
if (ste != null) {
// Loop through the error stack and write it to the log file
for ( int i = 0; i < ste.length; i ++ ){
log.out(strMethod,Global.LOG_ERR, ste.toString(), conn);
strErrMsg.append(ste.toString());
strErrMsg.append("\n"
}
}
log.out(strMethod, Global.LOG_ERR, "***************************\n\n", conn);
strErrBigMsg.append(Global.ERROR_PROCESS_REQ);
strErrBigMsg.append("\n\n"
strErrBigMsg.append(strErrMsg);
if (bSendMail){
mail = new Mail();
strMsg.append("An error occured in the GUI:\n"
strMsg.append("----------------------------\n\n"
strMsg.append("\tDate: "
strMsg.append(strDateTime);
strMsg.append("\n\tServlet & method: "
strMsg.append(strMethod);
strMsg.append("\n\tError message: "
strMsg.append(strErrBigMsg);
strMsg.append("\n\n\n"
strMsg.append("Please check also the log files for with the above date.\n"
mail.mailFrom = "error@GUI.comparitel";
mail.host = strHost;
mail.sendHost = strSMTP;
mail.mailUser = strUsr;
mail.mailPwd = strPwd;
mail.mailTo = strMailTo;
try
{
bMailOK = mail.sendMail(Global.GUI_ERROR_UPPER, strMsg.toString(), conn ,log);
}
catch(MessagingException me)
{
System.out.println("Error getting the mail object"
StackTraceElement[] steMe = me.getStackTrace(); // get the error stack
strError = me.toString();
bSendMail = false;
throwError(strMethodError, "Error while trying to send the error mail (MessagingException)", steMe, conn, log);
me.printStackTrace();
}
if (!(bMailOK)) {
bSendMail = false;
throwError(strMethodError, "Error while trying to send the error mail (Other)", null, conn, log);
}
}
} // end throwError
} // end class