jakarta-cactus-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jose Alfonso Martinez <tril...@linuxcenter.com.mx>
Subject Re: calling getServletContext() in a servlet - how to??
Date Wed, 13 Aug 2003 01:17:15 GMT
Yes, well I am only showing the init() method of InscribeServlet :(
I don't know if I could/should show more. If you think
it could be of any help I will give it another thought ok? :)

... since we both will benefit from doing it,  specially me :)  


I was thinking it could be a classpath issue.  Something that avoids the
ServletRedirector to create the config object. Anyway, I may not 
have an idea of what I am talking about. I will keep trying 
and if I find out something... I'll tell you

Thanks a lot


TestInscribeServlet:
=====================

package com.cevex.servlet;

import com.cevex.servlet.InscribeServlet;

import javax.servlet.ServletException;

import org.apache.cactus.ServletTestCase;
import org.apache.cactus.ServletTestSuite;
import org.apache.cactus.WebRequest;

import junit.framework.Test;

public class TestInscribeServlet extends ServletTestCase
{
        public static void main(String[] theArgs)
        {
                junit.textui.TestRunner.main(new String[] {TestInscribeServlet.class.getName()});
        }

        public static Test suite()
        {
                ServletTestSuite suite = new ServletTestSuite();
                suite.addTestSuite(TestInscribeServlet.class);
                return suite;
        }

        public void beginPostMethod(WebRequest theRequest)
        {
                theRequest.addParameter("nombres", "Juan Alberto");
                theRequest.addParameter("apellidos", "Robles");
                theRequest.addParameter("email", "email@email.com");
                theRequest.addParameter("ciudad", "Guadalajara");
                theRequest.addParameter("estado", "Jalisco");
                theRequest.addParameter("pais", "México");
                theRequest.addParameter("deseaBoletines", "true");
                theRequest.addParameter("tipoDeInscripcion", "regular");
                theRequest.addParameter("sexo", "true");
                theRequest.addParameter("anioNacimiento", "1988");
                theRequest.addParameter("directo", "");
                theRequest.addParameter("confirmacion", "true");
        }

        public void testPostMethod() throws ServletException
        {
                assertNotNull("aha config is null", config);
                InscribeServlet inscribeServlet = new InscribeServlet();
                inscribeServlet.init(config);
                //              inscribeServlet.doPost(request, response);
        }

}



InscribeServlet: 
====================

public class InscribeServlet extends HttpServlet {

... some member fields declarations

  /** Initialize this servlet 
   *  <UL>
   *  <LI> Get the context's datasource</LI>
   *  <LI> Initialize mail session </LI>
   *  <LI> Read emails </LI>
   *  </UL>
   **/
  public void init() throws CevexServletException
  {
    try
    {
      // get the context
      Context initCtx = new InitialContext();
      Context envCtx = (Context) initCtx.lookup("java:comp/env");

      // get the datasource
      ds = (DataSource) envCtx.lookup("jdbc/cevexDB");

      /* something is worng with the mail session, check later */
      //session = (Session) envCtx.lookup("mail/Session");         

      // initialize new mail seession
      Properties props = new Properties();
      props.put("mail.smtp.host", "localhost");
      Session session = Session.getDefaultInstance(props, null);
      session.setDebug(false);

      // Read email strings from files
      String realPath = getServletContext().getRealPath("/");
      mailInscripcionMasc = new CevexMail(session, realPath + getInitParameter("mailInscripcionMasc"));
      mailInscripcionFem  = new CevexMail(session, realPath + getInitParameter("mailInscripcionFem"));
      mailInscripcionConInvitacionMasc = new CevexMail(session, realPath +
                                             getInitParameter("mailInscripcionConInvitacionMasc"));
      mailInscripcionConInvitacionFem = new CevexMail(session, realPath +
                                             getInitParameter("mailInscripcionConInvitacionFem"));

    } // end try 
    catch (FileNotFoundException fe) {
       getServletContext().log("InscribeServlet: Al inicializar el servlet, " +
                               "no se encontro un archivo: " + fe.getMessage());
       throw new CevexServletException(getServletConfig(), "No se encontro un archivo: " +
                                       fe.getMessage() + " al iniciar InscribeServlet");
    }
    catch (IOException ioe)
    {
       getServletContext().log("InscribeServlet: Error al leer el archivo: " + ioe.getMessage());
       throw new CevexServletException(getServletConfig(), "Error de escritura en el archivo:
" +
                                       ioe.getMessage() + " al iniciar InscribeServlet");
    }
    catch (MessagingException me)
    {
        throw new CevexServletException(getServletConfig(), me);
    }
    catch (NamingException ne)
    {
       throw new CevexServletException(getServletConfig(), ne);
    }
  } //end init 


  // Temporary allow GET for testing
   public void doGet(HttpServletRequest req,
           HttpServletResponse res) throws ServletException, IOException {
     doPost(req, res);
   }

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

	

	... bla bla bla ...
   }
}



On Tue, Aug 12, 2003 at 09:32:13PM +0200, Vincent Massol wrote:
> 
> 
> > -----Original Message-----
> > From: Jose Alfonso Martinez [mailto:trilock@linuxcenter.com.mx]
> > Sent: 12 August 2003 19:32
> > To: Cactus Users List
> > Subject: Re: calling getServletContext() in a servlet - how to??
> > 
> > 
> > In fact, config is null
> > 
> > why does it happen???
> > 
> 
> If you had asked me, I would have said it isn't possible, seen the
> Cactus implementation code! :-)
> 
> Would it be possible for you to send us your InscribeServlet and your
> TestInscribeServlet code?
> 
> The only other option I see is that you happen to have an instance
> variable called config that isn't initialized... but then I'm pretty
> sure you would have noticed that... ;-)
> 
> Thanks
> -Vincent
> 

Mime
View raw message