Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ServletContext shouldn't be empty

Status
Not open for further replies.

TyzA

Programmer
Jan 7, 2002
86
BE
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(&quot;\n&quot;);
}
}

log.out(strMethod, Global.LOG_ERR, &quot;***************************\n\n&quot;, conn);

strErrBigMsg.append(Global.ERROR_PROCESS_REQ);
strErrBigMsg.append(&quot;\n\n&quot;);
strErrBigMsg.append(strErrMsg);


if (bSendMail){
mail = new Mail();

strMsg.append(&quot;An error occured in the GUI:\n&quot;);
strMsg.append(&quot;----------------------------\n\n&quot;);
strMsg.append(&quot;\tDate: &quot;);
strMsg.append(strDateTime);
strMsg.append(&quot;\n\tServlet & method: &quot;);
strMsg.append(strMethod);
strMsg.append(&quot;\n\tError message: &quot;);
strMsg.append(strErrBigMsg);
strMsg.append(&quot;\n\n\n&quot;);
strMsg.append(&quot;Please check also the log files for with the above date.\n&quot;);

mail.mailFrom = &quot;error@GUI.comparitel&quot;;
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(&quot;Error getting the mail object&quot;);
StackTraceElement[] steMe = me.getStackTrace(); // get the error stack
strError = me.toString();
bSendMail = false;
throwError(strMethodError, &quot;Error while trying to send the error mail (MessagingException)&quot;, steMe, conn, log);
me.printStackTrace();
}

if (!(bMailOK)) {
bSendMail = false;
throwError(strMethodError, &quot;Error while trying to send the error mail (Other)&quot;, null, conn, log);
}
}

} // end throwError

} // end class
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top