Re: getServletContext call from jsp page...
Adding extra functionality is not a bad thing as long as it conforms to the spec. But using the extra functionality makes your code non-portable. Of course, one may also think this a bug since Jasper assumes your JSP extends HttpServlet and you may issue a directive %@page extends=my.Clazz % where my.Clazz does NOT extend HttpServlet and only implements JspPage which has significantly less functionality. As a side note: BEA weblogic does not inherit their JSP's from HttpServlet, so that is one platform where you would have trouble if you made the assumption from below. Padhu Vinirs wrote: Wanted to know if this assumption is valid in most jsp containers: Is calling getServletContext() ( instead of getServletConfig().getServletContext() ) assuming that the servlet generated from the jsp will be derived from HttpServlet ? This works ( in Tomcat ) because HttpJspBase derives from HttpServlet. But getServletConfig() is part of the interface Servlet which every Servlet has to implement. -- padhu -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED]
RE: getServletContext() error
Please ignore the previous message I found the problem and it was just a mistake on my side. -Original Message- From: Jeff Macomber [mailto:[EMAIL PROTECTED]] Sent: Wednesday, April 17, 2002 5:39 PM To: 'Tomcat Users List' Subject: getServletContext() error Hi all, I am trying to share an object from a servlet to a JSP by using the getServletContext().setAttribute() method in the init() of my servlet. I am able to return the context from the JSP page but am unable to retrieve the attribute (getAttribute() returns null). I have setup the Context in the Server.xml file to contain crossContext=true and override=true for the jsp context. This allowed me to get the context as a non-null object. The attribute name and case are the same as in the setAttribute() statement. Also in the Servlet I am calling super.init(ServletConfig) so I am pretty sure that the Attribute is being added correctly. This technique worked in the 3.X versions of tomcat but I have tried it on both 4.0.1 and 4.0.3 and am not making any progress. If anyone has some experience with using a servlet context in a JSP I would gladly take any advice. Server.xml snippet: !--This is the Servlet Context -- Context path=/DBPoolingServer docBase=DBPoolingServer reloadable=true crossContext=true debug=9 override=true/ !--This is the Context for the JSP webapp-- Context path=/test docBase=test reloadable=true crossContext=true debug=9 override=true/ Servlet web.xml mappings: servlet-mapping servlet-namedbpoolingservlet/servlet-name url-pattern/dbpoolingservlet/url-pattern /servlet-mapping Servlet init() snippet: public void init(ServletConfig config) throws ServletException { super.init(config); mDBPoolHandler = new DBPoolingHandler(); mDBPoolHandler.initialize(); getServletContext().setAttribute(DBPool, mDBPoolHandler); } JSP Snippet: ServletContext lContext = application.getContext(/DBPoolingServer/dbpoolingservlet/*); if (lContext != null) { Object lHandler = lContext.getAttribute(DBPool); if (lHandler != null) { Object lConn = new String(); if (lConn !=null) { out.println(Connected); }else { out.println(not connected); } } else { out.println(lHandler = null); } } else { out.println(Context = null); } The JSP example above returns a non-null lContext and then a null lHandler. So the getAttribute() appears to be failing. Any help would be appreciated. Jeff -- To unsubscribe: mailto:[EMAIL PROTECTED] For additional commands: mailto:[EMAIL PROTECTED] Troubles with the list: mailto:[EMAIL PROTECTED] -- To unsubscribe: mailto:[EMAIL PROTECTED] For additional commands: mailto:[EMAIL PROTECTED] Troubles with the list: mailto:[EMAIL PROTECTED]
RE: getServletContext() returning null...
The easiest way to get this behavior is to override init(ServletConfig config) in your servlet and forget to call super.init(config) as instructed in the Javadoc. You can either call super.init(config), or override init() instead, which is called from super.init(config). Cheers, Larry -Original Message- From: Stan [mailto:[EMAIL PROTECTED]] Sent: Monday, January 14, 2002 9:39 AM To: [EMAIL PROTECTED] Subject: getServletContext() returning null... I have a servlet in which I am trying to pass control to a JSP page to conform with the JSP Model 2 scenario. I am using Tomcat 3.3 under Win98 and have setup a webapp (jforum) with the config files as listed below. For some reason when I try to obtain the ServletContext for the servlet in order to dispatch the request over to the JSP I get null back from the getServletContext() call. Is there anything in the configuration I have supplied which would stop this context being created appropriately? I would presume that a servlet would necessarily be part of a ServletContext, and therefore it couldn't return null, but perhaps I am wrong. Any help much appreciated, as this is driving me up the wall! Stan tomcat/conf/app-jforum.xml: --- ?xml version=1.0 encoding=ISO-8859-1? webapps Context path=/jforum docBase=webapps/jforum debug=9 / /webapps tomcat/webapps/jforum/WEB-INF/web.xml: -- ?xml version=1.0 encoding=ISO-8859-1? !DOCTYPE web-app PUBLIC -//Sun Microsystems, Inc.//DTD Web Application 2.2//EN http://java.sun.com/j2ee/dtds/web-app_2_2.dtd; web-app display-nameJForum/display-name description A web-based forum system, allowing threaded topics. /description servlet servlet-name jforum /servlet-name servlet-class slink.apps.jforum.ControlServlet /servlet-class init-param param-namemaxAge/param-name param-value120/param-value /init-param init-param param-namemaxObj/param-name param-value500/param-value /init-param init-param param-namedebug/param-name param-valuetrue/param-value /init-param /servlet /web-app -- To unsubscribe: mailto:[EMAIL PROTECTED] For additional commands: mailto:[EMAIL PROTECTED] Troubles with the list: mailto:[EMAIL PROTECTED] -- To unsubscribe: mailto:[EMAIL PROTECTED] For additional commands: mailto:[EMAIL PROTECTED] Troubles with the list: mailto:[EMAIL PROTECTED]
Re: getServletContext() returning null...
On Mon, 14 Jan 2002 09:59:49 -0500 you wrote: The easiest way to get this behavior is to override init(ServletConfig config) in your servlet and forget to call super.init(config) as instructed in the Javadoc. You can either call super.init(config), or override init() instead, which is called from super.init(config). Cheers, Larry fxKerching!/fx Of course! I don't often bother using the init() method and peform lazy instantiation of most things. I seem to remember I've forgotten this one before - thanks very much, Larry. Stan -- To unsubscribe: mailto:[EMAIL PROTECTED] For additional commands: mailto:[EMAIL PROTECTED] Troubles with the list: mailto:[EMAIL PROTECTED]
RE: getServletContext() throws NullPoinetException
Do you, perhaps, implement the init method and not call super.init(config)? Did this once and I'll never do it again. Randy -Original Message- From: Stefanos Karasavvidis [mailto:[EMAIL PROTECTED]] Sent: Tuesday, July 10, 2001 10:46 AM To: [EMAIL PROTECTED] Subject: getServletContext() throws NullPoinetException I've just installed tomcat 3.2.2 and have the following problem. I want to call getServletContext() from a servlets service method but get the folowing exception *Internal Servlet Error:* java.lang.NullPointerException at javax.servlet.GenericServlet.getServletContext(GenericServlet. java:205) at TestServlet.service(TestServlet.java:32) at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper .java:405) at org.apache.tomcat.core.Handler.service(Handler.java:287) at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372) at org.apache.tomcat.core.ContextManager.internalService(ContextM anager.java:797) at org.apache.tomcat.core.ContextManager.service(ContextManager.java:743) at org.apache.tomcat.service.connector.Ajp12ConnectionHandler.pro cessConnection(Ajp12ConnectionHandler.java:166) at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoin t.java:416) at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPo ol.java:501) at java.lang.Thread.run(Thread.java:484) Moreover the getServletConfig() returns null which is probably the main reason for this problem Any ideas?? Stefanos
RE: getServletContext() throws NullPoinetException
Did you override init(ServletConfig config) method? YOu must call super(config) if you did... --V. -Original Message- From: Stefanos Karasavvidis [mailto:[EMAIL PROTECTED]] Sent: Tuesday, July 10, 2001 7:46 AM To: [EMAIL PROTECTED] Subject: getServletContext() throws NullPoinetException I've just installed tomcat 3.2.2 and have the following problem. I want to call getServletContext() from a servlets service method but get the folowing exception *Internal Servlet Error:* java.lang.NullPointerException at javax.servlet.GenericServlet.getServletContext(GenericServlet.java:205) at TestServlet.service(TestServlet.java:32) at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:405) at org.apache.tomcat.core.Handler.service(Handler.java:287) at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372) at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:79 7) at org.apache.tomcat.core.ContextManager.service(ContextManager.java:743) at org.apache.tomcat.service.connector.Ajp12ConnectionHandler.processConnection (Ajp12ConnectionHandler.java:166) at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416) at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:501) at java.lang.Thread.run(Thread.java:484) Moreover the getServletConfig() returns null which is probably the main reason for this problem Any ideas?? Stefanos *** This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply e-mail and destroy all copies of the original message. ***
RE: getServletContext() throws NullPoinetException
Looking at the source (available from http://jakarta.apache.org/builds/jakarta-tomcat/release/) it looks like the only way this would happen is if it doesn't have the ServletConfig passed into its init() method. Are you intercepting the call to init() in your servlet? If so, are you making sure to call the superclass' init() method? -- Bill K. -Original Message- From: Stefanos Karasavvidis [mailto:[EMAIL PROTECTED]] Sent: Tuesday, July 10, 2001 7:46 AM To: [EMAIL PROTECTED] Subject: getServletContext() throws NullPoinetException I've just installed tomcat 3.2.2 and have the following problem. I want to call getServletContext() from a servlets service method but get the folowing exception *Internal Servlet Error:* java.lang.NullPointerException at javax.servlet.GenericServlet.getServletContext(GenericServlet. java:205) at TestServlet.service(TestServlet.java:32) at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper .java:405) at org.apache.tomcat.core.Handler.service(Handler.java:287) at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372) at org.apache.tomcat.core.ContextManager.internalService(ContextM anager.java:797) at org.apache.tomcat.core.ContextManager.service(ContextManager.java:743) at org.apache.tomcat.service.connector.Ajp12ConnectionHandler.pro cessConnection(Ajp12ConnectionHandler.java:166) at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoin t.java:416) at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPo ol.java:501) at java.lang.Thread.run(Thread.java:484) Moreover the getServletConfig() returns null which is probably the main reason for this problem Any ideas?? Stefanos
Re: getServletContext() throws NullPoinetException - solved
found it... I didn't call super.init(config) in the init() method sorry Stefanos Stefanos Karasavvidis wrote: I've just installed tomcat 3.2.2 and have the following problem. I want to call getServletContext() from a servlets service method but get the folowing exception *Internal Servlet Error:* java.lang.NullPointerException at javax.servlet.GenericServlet.getServletContext(GenericServlet.java:205) at TestServlet.service(TestServlet.java:32) at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:405) at org.apache.tomcat.core.Handler.service(Handler.java:287) at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372) at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:797) at org.apache.tomcat.core.ContextManager.service(ContextManager.java:743) at org.apache.tomcat.service.connector.Ajp12ConnectionHandler.processConnection(Ajp12ConnectionHandler.java:166) at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416) at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:501) at java.lang.Thread.run(Thread.java:484) Moreover the getServletConfig() returns null which is probably the main reason for this problem Any ideas?? Stefanos
RE: getServletContext()
Hi, getAttribute returns a java.lang.Object so you have to cast it to java.sql.Connection. You will also need to import java.sql. %@ page import=java.sql.* % ... % Connection Conn = (Connection)getServletContext().getAttribute(dbConnection); % Make sure dbConnection isn't null when you set the attribute, cause that would be another problem. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] Sent: Dienstag, 26. Juni 2001 21:06 To: [EMAIL PROTECTED] Subject: getServletContext() Hi, I have a problem I have been trying to solve for a very long time. I first have a initialization servlet set by load_on_startup tag. In this servlet I create a database connection and pass it to the ServletContext like this. getServletContext().setAttribute(dbConnection,dbConnection); Now the problem is how do I retrieve it from a jsp page. From by research, reading through books it shows how to get an attribute using : jsp:useBean action. However this is done if Beans/Objects are put into the setAttribute. In my case it is a database connection object. How do I retrieve this from a jsp page??? I tried (in a jsp page); % Connection connection = getServletContext().getAttribute(dbConnection); % but this returns a null. Connection never gets set.. If someone can help me please..
Re: getServletContext()
hey Is the initialization working properly? are you able to see that the database conneciton is being achieved and is being passed over. Then the next thing would be to check for the session, may be the session is getting invalidated and hence when you try to retrieve the Connection, it gets null. can help you if you tell me something about the above Kris --- [EMAIL PROTECTED] wrote: Hi, I have a problem I have been trying to solve for a very long time. I first have a initialization servlet set by load_on_startup tag. In this servlet I create a database connection and pass it to the ServletContext like this. getServletContext().setAttribute(dbConnection,dbConnection); Now the problem is how do I retrieve it from a jsp page. From by research, reading through books it shows how to get an attribute using : jsp:useBean action. However this is done if Beans/Objects are put into the setAttribute. In my case it is a database connection object. How do I retrieve this from a jsp page??? I tried (in a jsp page); % Connection connection = getServletContext().getAttribute(dbConnection); % but this returns a null. Connection never gets set.. If someone can help me please.. __ Do You Yahoo!? Get personalized email addresses from Yahoo! Mail http://personal.mail.yahoo.com/
Re: getServletContext()
I guess to save any object to a session, the object has to implement serializable interface. anil
RE: getServletContext null pointer exception
Thanks for your reply, This is the URL I am using http://localhost:8080/jdbctut/jdbctut.jsp The class file is going into the webapps/jdbctut/Web-inf/classes of the standard tom-cat installation. I am using ant and the standard build. Thanks again, Chuck -Original Message- From: Milt Epstein [SMTP:[EMAIL PROTECTED]] Sent: Saturday, March 10, 2001 9:26 AM To: [EMAIL PROTECTED] Subject: Re: getServletContext null pointer exception On Fri, 9 Mar 2001 [EMAIL PROTECTED] wrote: Hi, I have been unable to get at the servletcontext param values. I keep getting the null pointer exception. I have looked thru all the archives and quite a few sites trying all the recommendations, but to no avail. Must be doing something really stupid, or I've missed a basic concept somewhere. I am running tomcat 3.2.1 Any hints would be gratefully appreciated. Thanks in advance. Chuck What URL are you using to access the servlet? Where (in the directory structure) is your servlet class located? Here's my web.xml and the code trying to get at it. web-app display-namejdbctut/display-name descriptionThis is version X.X of an application to perform/description context-param param-namedbuser/param-name param-valuefabdev/param-value descriptionUser name to logon to the Db./description /context-param servlet servlet-nameDataBaseSelect/servlet-name description This servlet /description servlet-classDataBaseSelect/servlet-class !-- Load this servlet at server startup time -- load-on-startup5/load-on-startup /servlet servlet-mapping servlet-nameDataBaseSelect/servlet-name url-patternjdbctut/url-pattern /servlet-mapping session-config session-timeout30/session-timeout!-- 30 minutes -- /session-config /web-app import java.sql.*; import java.util.Vector; import javax.servlet.http.*; import javax.servlet.*; import java.io.*; public class DataBaseSelect extends HttpServlet { private Vector result; private String url = "jdbc:informix-sqli://poldev:2005/fabdev:INFORMIXSERVER=poldev_713_tcp"; public void init( ServletConfig config ) throws javax.servlet.ServletException{ super.init( config ) ; // Essential!! } public DataBaseSelect() { result = new Vector(); } // constructor DataBaseSelect public String connect() { try { Class.forName("com.informix.jdbc.IfxDriver").newInstance(); return "Driver Loaded!"; } catch (Exception E) { return "Unable to load driver."; } } public String select() { // String value = getServletContext().getInitParameter("dbuser"); // System.out.println(getServletConfig().getServletContext().getInitParameter (" dbuser")); ServletConfig sc = getServletConfig(); ServletContext sctx = sc.getServletContext(); System.out.println(sctx.getInitParameter("dbuser")); try { Connection C = DriverManager.getConnection(url, "fabdev", "fabdev$"); Statement Stmt = C.createStatement(); ResultSet myResult = Stmt.executeQuery("SELECT lst_nm from person_profile ORDER BY lst_nm"); while (myResult.next()) { result.addElement(myResult.getString(1)); } // Clean up myResult.close(); Stmt.close(); C.close(); return "Connection Success!"; } catch (SQLException E) { return "SQLException: " + E.getMessage(); } } /** * Accessor for result **/ public Vector getResult() { return result; } /** * Mutator for result **/ public void setResult(Vector avector) { result = avector; } } // class DataBaseSelect Sure there's apathy in the world. But who cares. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, email: [EMAIL PROTECTED] Milt Epstein Research Programmer Software/Systems Development Group Computing and Communications Services Office (CCSO) University of Illinois at Urbana-Champaign (UIUC) [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, email: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, email: [EMAIL PROTECTED]
RE: getServletContext null pointer exception
On Tue, 13 Mar 2001 [EMAIL PROTECTED] wrote: Thanks for your reply, This is the URL I am using http://localhost:8080/jdbctut/jdbctut.jsp The class file is going into the webapps/jdbctut/Web-inf/classes of the standard tom-cat installation. [ ... ] Hmmm. I didn't realize you were using a JSP. But since this is context-param's we're talking about, I don't think it should matter (how you set up init-param's varies slightly for servlets and JSPs). Where is the JSP file located? webapps/jdbctut/jdbctut.jsp? Can you give more info about what the problem is, what error you are getting? You say you are getting a null pointer exception. What does the stack trace look like on that? Does it indicate where the exception occurs (i.e. what line)? You might need to turn off the compiler to see the line numbers. -Original Message- From: Milt Epstein [SMTP:[EMAIL PROTECTED]] Sent: Saturday, March 10, 2001 9:26 AM To: [EMAIL PROTECTED] Subject:Re: getServletContext null pointer exception On Fri, 9 Mar 2001 [EMAIL PROTECTED] wrote: Hi, I have been unable to get at the servletcontext param values. I keep getting the null pointer exception. I have looked thru all the archives and quite a few sites trying all the recommendations, but to no avail. Must be doing something really stupid, or I've missed a basic concept somewhere. I am running tomcat 3.2.1 Any hints would be gratefully appreciated. Thanks in advance. Chuck What URL are you using to access the servlet? Where (in the directory structure) is your servlet class located? Here's my web.xml and the code trying to get at it. web-app display-namejdbctut/display-name descriptionThis is version X.X of an application to perform/description context-param param-namedbuser/param-name param-valuefabdev/param-value descriptionUser name to logon to the Db./description /context-param servlet servlet-nameDataBaseSelect/servlet-name description This servlet /description servlet-classDataBaseSelect/servlet-class !-- Load this servlet at server startup time -- load-on-startup5/load-on-startup /servlet servlet-mapping servlet-nameDataBaseSelect/servlet-name url-patternjdbctut/url-pattern /servlet-mapping session-config session-timeout30/session-timeout!-- 30 minutes -- /session-config /web-app import java.sql.*; import java.util.Vector; import javax.servlet.http.*; import javax.servlet.*; import java.io.*; public class DataBaseSelect extends HttpServlet { private Vector result; private String url = "jdbc:informix-sqli://poldev:2005/fabdev:INFORMIXSERVER=poldev_713_tcp"; public void init( ServletConfig config ) throws javax.servlet.ServletException{ super.init( config ) ; // Essential!! } public DataBaseSelect() { result = new Vector(); } // constructor DataBaseSelect public String connect() { try { Class.forName("com.informix.jdbc.IfxDriver").newInstance(); return "Driver Loaded!"; } catch (Exception E) { return "Unable to load driver."; } } public String select() { //String value = getServletContext().getInitParameter("dbuser"); // System.out.println(getServletConfig().getServletContext().getInitParameter (" dbuser")); ServletConfig sc = getServletConfig(); ServletContext sctx = sc.getServletContext(); System.out.println(sctx.getInitParameter("dbuser")); try { Connection C = DriverManager.getConnection(url, "fabdev", "fabdev$"); Statement Stmt = C.createStatement(); ResultSet myResult = Stmt.executeQuery("SELECT lst_nm from person_profile ORDER BY lst_nm"); while (myResult.next()) { result.addElement(myResult.getString(1)); } // Clean up myResult.close(); Stmt.close(); C.close(); return "Connection Success!"; } catch (SQLException E) { return "SQLException: " + E.getMessage(); } } /** * Accessor for result **/ public Vector getResult() { return result; } /** * Mutator for result **/ public void setResult(Vector avector) { result = avector; } } // class DataBaseSelect Sure there's apathy in the world. But who cares. - To unsubscribe, e-
RE: getServletContext null pointer exception
Yup, the jsp is in webapps/jdbctut/jdbctut.jsp This is where i get the err. if i use this stmt. == String value = getServletContext().getInitParameter("dbuser"); or if i use this stmt == System.out.println(getServletConfig().getServletContext().getInitParameter or if i use these stmts ServletConfig sc = getServletConfig(); == ServletContext sctx = sc.getServletContext(); System.out.println(sctx.getInitParameter("dbuser")); These are the credible methods that I found in various examples and recomendations, and these are the stack traces that come back. javax.servlet.ServletException at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImp l.java:459) at _0002fjdbctut_0002ejspjdbctut_jsp_0._jspService(_0002fjdbctut_0002ejspjdbctu t_jsp_0.java:134) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:119) at javax.servlet.http.HttpServlet.service(HttpServlet.java:853) at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.ja va:177) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:318) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:391) at javax.servlet.http.HttpServlet.service(HttpServlet.java:853) at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:404) at org.apache.tomcat.core.Handler.service(Handler.java:286) at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372) at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:79 7) at org.apache.tomcat.core.ContextManager.service(ContextManager.java:743) at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpC onnectionHandler.java:210) at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416) at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:498) at java.lang.Thread.run(Thread.java:484) Root cause: java.lang.NullPointerException at DataBaseSelect.select(DataBaseSelect.java:53) at _0002fjdbctut_0002ejspjdbctut_jsp_0._jspService(_0002fjdbctut_0002ejspjdbctu t_jsp_0.java:92) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:119) at javax.servlet.http.HttpServlet.service(HttpServlet.java:853) at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.ja va:177) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:318) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:391) at javax.servlet.http.HttpServlet.service(HttpServlet.java:853) at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:404) at org.apache.tomcat.core.Handler.service(Handler.java:286) at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372) at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:79 7) at org.apache.tomcat.core.ContextManager.service(ContextManager.java:743) at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpC onnectionHandler.java:210) at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416) at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:498) at java.lang.Thread.run(Thread.java:484) -Original Message- From: Milt Epstein [SMTP:[EMAIL PROTECTED]] Sent: Tuesday, March 13, 2001 2:23 PM To: [EMAIL PROTECTED] Subject: RE: getServletContext null pointer exception On Tue, 13 Mar 2001 [EMAIL PROTECTED] wrote: Thanks for your reply, This is the URL I am using http://localhost:8080/jdbctut/jdbctut.jsp The class file is going into the webapps/jdbctut/Web-inf/classes of the standard tom-cat installation. [ ... ] Hmmm. I didn't realize you were using a JSP. But since this is context-param's we're talking about, I don't think it should matter (how you set up init-param's varies slightly for servlets and JSPs). Where is the JSP file located? webapps/jdbctut/jdbctut.jsp? Can you give more info about what the problem is, what error you are getting? You say you are getting a null pointer exception. What does the stack trace look like on that? Does it indicate where the exception occurs (i.e. what line)? You might need to turn off the compiler to see the line numbers. -Original Message- From: Milt Epstein [SMTP:[EMAIL PROTECTED]] Sent: Saturday, March 10, 2001 9:26 AM To: [EMAIL PROTECTED] Subject: Re: getServletContext null pointer exception On Fri, 9 Mar 2001 [EMAIL PROTECTED] wrote: Hi, I have been unable to get at the servletcontext param values. I keep getting the null pointer exception. I have looked thru all the arch
RE: getServletContext null pointer exception
On Tue, 13 Mar 2001 [EMAIL PROTECTED] wrote: Yup, the jsp is in webapps/jdbctut/jdbctut.jsp This is where i get the err. if i use this stmt. == String value = getServletContext().getInitParameter("dbuser"); or if i use this stmt == System.out.println(getServletConfig().getServletContext().getInitParameter or if i use these stmts ServletConfig sc = getServletConfig(); == ServletContext sctx = sc.getServletContext(); System.out.println(sctx.getInitParameter("dbuser")); [ ... ] Ahhh, it looks to me like a ServletConfig problem -- probably the well known init ServletConfig problem. Do you override init(ServletConfig config) in your servlet? If so, you need to make sure that the first thing it does is call super.init(config). Or, as introduced in the 2.1 API, you can override a no-argument init() method. Scratch that -- I see that your servlet code is still included below, including the overriden init, and it looks like you do properly call super.init(config). But from what you indicated above, it looks like the ServletConfig or the ServletContext is null. I'm not sure why else that would be. How are you calling the servlet? Is that happening in the JSP? Maybe it has something to do with that. Not sure what else to suggest at this point. -Original Message- From: Milt Epstein [SMTP:[EMAIL PROTECTED]] Sent: Tuesday, March 13, 2001 2:23 PM To: [EMAIL PROTECTED] Subject:RE: getServletContext null pointer exception On Tue, 13 Mar 2001 [EMAIL PROTECTED] wrote: Thanks for your reply, This is the URL I am using http://localhost:8080/jdbctut/jdbctut.jsp The class file is going into the webapps/jdbctut/Web-inf/classes of the standard tom-cat installation. [ ... ] Hmmm. I didn't realize you were using a JSP. But since this is context-param's we're talking about, I don't think it should matter (how you set up init-param's varies slightly for servlets and JSPs). Where is the JSP file located? webapps/jdbctut/jdbctut.jsp? Can you give more info about what the problem is, what error you are getting? You say you are getting a null pointer exception. What does the stack trace look like on that? Does it indicate where the exception occurs (i.e. what line)? You might need to turn off the compiler to see the line numbers. -Original Message- From: Milt Epstein [SMTP:[EMAIL PROTECTED]] Sent: Saturday, March 10, 2001 9:26 AM To: [EMAIL PROTECTED] Subject:Re: getServletContext null pointer exception On Fri, 9 Mar 2001 [EMAIL PROTECTED] wrote: Hi, I have been unable to get at the servletcontext param values. I keep getting the null pointer exception. I have looked thru all the archives and quite a few sites trying all the recommendations, but to no avail. Must be doing something really stupid, or I've missed a basic concept somewhere. I am running tomcat 3.2.1 Any hints would be gratefully appreciated. Thanks in advance. Chuck What URL are you using to access the servlet? Where (in the directory structure) is your servlet class located? Here's my web.xml and the code trying to get at it. web-app display-namejdbctut/display-name descriptionThis is version X.X of an application to perform/description context-param param-namedbuser/param-name param-valuefabdev/param-value descriptionUser name to logon to the Db./description /context-param servlet servlet-nameDataBaseSelect/servlet-name description This servlet /description servlet-classDataBaseSelect/servlet-class !-- Load this servlet at server startup time -- load-on-startup5/load-on-startup /servlet servlet-mapping servlet-nameDataBaseSelect/servlet-name url-patternjdbctut/url-pattern /servlet-mapping session-config session-timeout30/session-timeout!-- 30 minutes -- /session-config /web-app import java.sql.*; import java.util.Vector; import javax.servlet.http.*; import javax.servlet.*; import java.io.*; public class DataBaseSelect extends HttpServlet { private Vector result; private String url = "jdbc:informix-sqli://poldev:2005/fabdev:INFORMIXSERVER=poldev_713_tcp"; public void init( ServletConfig config ) throws javax.servlet.ServletException{ super.init( config ) ; // Essential!! } public DataBaseSelect() { result = new Vector(); } // constructor DataBaseSelect public String connect() { try { Class.forName("com.in
RE: getServletContext null pointer exception
Well, here is the .jsp I don't know what could affect it, except maybe the scope on the useBean? It all seemed so logical, and so simple. Just made for exactly what I need to do. Thanks again, html head titleSelect everything from a database/title /head body jsp:useBean id="select" class="DataBaseSelect" scope="request"/ % out.print(select.connect()); % br % out.print(select.select()); % pFormat results br %@ page import="java.util.Vector" % % Vector aResult = select.getResult(); % table % for (int i=0; i aResult.size(); i++) { % tr td % out.print(i); % /td td % out.print(aResult.elementAt(i)); % /td /tr % } % /table /body /html -Original Message- From: Milt Epstein [SMTP:[EMAIL PROTECTED]] Sent: Tuesday, March 13, 2001 3:27 PM To: [EMAIL PROTECTED] Subject: RE: getServletContext null pointer exception On Tue, 13 Mar 2001 [EMAIL PROTECTED] wrote: Yup, the jsp is in webapps/jdbctut/jdbctut.jsp This is where i get the err. if i use this stmt. == String value = getServletContext().getInitParameter("dbuser"); or if i use this stmt == System.out.println(getServletConfig().getServletContext().getInitParameter or if i use these stmts ServletConfig sc = getServletConfig(); == ServletContext sctx = sc.getServletContext(); System.out.println(sctx.getInitParameter("dbuser")); [ ... ] Ahhh, it looks to me like a ServletConfig problem -- probably the well known init ServletConfig problem. Do you override init(ServletConfig config) in your servlet? If so, you need to make sure that the first thing it does is call super.init(config). Or, as introduced in the 2.1 API, you can override a no-argument init() method. Scratch that -- I see that your servlet code is still included below, including the overriden init, and it looks like you do properly call super.init(config). But from what you indicated above, it looks like the ServletConfig or the ServletContext is null. I'm not sure why else that would be. How are you calling the servlet? Is that happening in the JSP? Maybe it has something to do with that. Not sure what else to suggest at this point. -Original Message- From: Milt Epstein [SMTP:[EMAIL PROTECTED]] Sent: Tuesday, March 13, 2001 2:23 PM To: [EMAIL PROTECTED] Subject: RE: getServletContext null pointer exception On Tue, 13 Mar 2001 [EMAIL PROTECTED] wrote: Thanks for your reply, This is the URL I am using http://localhost:8080/jdbctut/jdbctut.jsp The class file is going into the webapps/jdbctut/Web-inf/classes of the standard tom-cat installation. [ ... ] Hmmm. I didn't realize you were using a JSP. But since this is context-param's we're talking about, I don't think it should matter (how you set up init-param's varies slightly for servlets and JSPs). Where is the JSP file located? webapps/jdbctut/jdbctut.jsp? Can you give more info about what the problem is, what error you are getting? You say you are getting a null pointer exception. What does the stack trace look like on that? Does it indicate where the exception occurs (i.e. what line)? You might need to turn off the compiler to see the line numbers. -Original Message- From: Milt Epstein [SMTP:[EMAIL PROTECTED]] Sent: Saturday, March 10, 2001 9:26 AM To: [EMAIL PROTECTED] Subject: Re: getServletContext null pointer exception On Fri, 9 Mar 2001 [EMAIL PROTECTED] wrote: Hi, I have been unable to get at the servletcontext param values. I keep getting the null pointer exception. I have looked thru all the archives and quite a few sites trying all the recommendations, but to no avail. Must be doing something really stupid, or I've missed a basic concept somewhere. I am running tomcat 3.2.1 Any hints would be gratefully appreciated. Thanks in advance. Chuck What URL are you using to access the servlet? Where (in the directory structure) is your servlet class located? Here's my web.xml and the code trying to get at it. web-app display-namejdbctut/display-name descriptionThis is version X.X of an application to perform/description context-param param-namedbuser/param-name param-valuefabdev/param-value descriptionUser name to logon to the Db./description /context-param servlet servlet-nameDataBaseSelect/servlet-name description This servlet /description servlet-classDataBaseSelect/servlet-class !-- Load this servlet at server startup time -- load-on-startup5/load-on-startup
RE: getServletContext null pointer exception
On Tue, 13 Mar 2001 [EMAIL PROTECTED] wrote: Well, here is the .jsp I don't know what could affect it, except maybe the scope on the useBean? It all seemed so logical, and so simple. Just made for exactly what I need to do. Well, you're getting beyond things I've done (e.g. using beans in JSPs), so I can't really say for sure, but here's my conjecture as to what's going on: You've coded up DataBaseSelect as a servlet, but you're not using it as a servlet, you're using it as a bean. So, its init() method is not getting called (that's done from the servlet container), so the ServletConfig is null, so you're getting a NullPointerException. I think your design is a little confused, and you need to better understand how things work (or maybe I'm the one that's confused :-). One thing you could do is read the context-param in the JSP -- using a line like the following: application.getInitParameter("dbuser"); and then pass that in the call to select.getResult(). html head titleSelect everything from a database/title /head body jsp:useBean id="select" class="DataBaseSelect" scope="request"/ % out.print(select.connect()); % br % out.print(select.select()); % pFormat results br %@ page import="java.util.Vector" % % Vector aResult = select.getResult(); % table % for (int i=0; i aResult.size(); i++) { % tr td % out.print(i); % /td td % out.print(aResult.elementAt(i)); % /td /tr % } % /table /body /html -Original Message- From: Milt Epstein [SMTP:[EMAIL PROTECTED]] Sent: Tuesday, March 13, 2001 3:27 PM To: [EMAIL PROTECTED] Subject:RE: getServletContext null pointer exception On Tue, 13 Mar 2001 [EMAIL PROTECTED] wrote: Yup, the jsp is in webapps/jdbctut/jdbctut.jsp This is where i get the err. if i use this stmt. == String value = getServletContext().getInitParameter("dbuser"); or if i use this stmt == System.out.println(getServletConfig().getServletContext().getInitParameter or if i use these stmts ServletConfig sc = getServletConfig(); == ServletContext sctx = sc.getServletContext(); System.out.println(sctx.getInitParameter("dbuser")); [ ... ] Ahhh, it looks to me like a ServletConfig problem -- probably the well known init ServletConfig problem. Do you override init(ServletConfig config) in your servlet? If so, you need to make sure that the first thing it does is call super.init(config). Or, as introduced in the 2.1 API, you can override a no-argument init() method. Scratch that -- I see that your servlet code is still included below, including the overriden init, and it looks like you do properly call super.init(config). But from what you indicated above, it looks like the ServletConfig or the ServletContext is null. I'm not sure why else that would be. How are you calling the servlet? Is that happening in the JSP? Maybe it has something to do with that. Not sure what else to suggest at this point. -Original Message- From: Milt Epstein [SMTP:[EMAIL PROTECTED]] Sent: Tuesday, March 13, 2001 2:23 PM To: [EMAIL PROTECTED] Subject:RE: getServletContext null pointer exception On Tue, 13 Mar 2001 [EMAIL PROTECTED] wrote: Thanks for your reply, This is the URL I am using http://localhost:8080/jdbctut/jdbctut.jsp The class file is going into the webapps/jdbctut/Web-inf/classes of the standard tom-cat installation. [ ... ] Hmmm. I didn't realize you were using a JSP. But since this is context-param's we're talking about, I don't think it should matter (how you set up init-param's varies slightly for servlets and JSPs). Where is the JSP file located? webapps/jdbctut/jdbctut.jsp? Can you give more info about what the problem is, what error you are getting? You say you are getting a null pointer exception. What does the stack trace look like on that? Does it indicate where the exception occurs (i.e. what line)? You might need to turn off the compiler to see the line numbers. -Original Message- From: Milt Epstein [SMTP:[EMAIL PROTECTED]] Sent: Saturday, March 10, 2001 9:26 AM To: [EMAIL PROTECTED] Subject:Re: getServletContext null pointer exception On Fri, 9 Mar 2001 [EMAIL PROTECTED] wrote: Hi, I have been unable to get at the servletcontext param values. I keep getting the null pointer exception. I have looked thru all the archives and quite a few sites trying all the recommendations, but to no avail. Must be doing something really stupid, or I've missed a basic concept somewhere. I am running
Re: getServletContext null pointer exception
On Fri, 9 Mar 2001 [EMAIL PROTECTED] wrote: Hi, I have been unable to get at the servletcontext param values. I keep getting the null pointer exception. I have looked thru all the archives and quite a few sites trying all the recommendations, but to no avail. Must be doing something really stupid, or I've missed a basic concept somewhere. I am running tomcat 3.2.1 Any hints would be gratefully appreciated. Thanks in advance. Chuck What URL are you using to access the servlet? Where (in the directory structure) is your servlet class located? Here's my web.xml and the code trying to get at it. web-app display-namejdbctut/display-name descriptionThis is version X.X of an application to perform/description context-param param-namedbuser/param-name param-valuefabdev/param-value descriptionUser name to logon to the Db./description /context-param servlet servlet-nameDataBaseSelect/servlet-name description This servlet /description servlet-classDataBaseSelect/servlet-class !-- Load this servlet at server startup time -- load-on-startup5/load-on-startup /servlet servlet-mapping servlet-nameDataBaseSelect/servlet-name url-patternjdbctut/url-pattern /servlet-mapping session-config session-timeout30/session-timeout!-- 30 minutes -- /session-config /web-app import java.sql.*; import java.util.Vector; import javax.servlet.http.*; import javax.servlet.*; import java.io.*; public class DataBaseSelect extends HttpServlet { private Vector result; private String url = "jdbc:informix-sqli://poldev:2005/fabdev:INFORMIXSERVER=poldev_713_tcp"; public void init( ServletConfig config ) throws javax.servlet.ServletException{ super.init( config ) ; // Essential!! } public DataBaseSelect() { result = new Vector(); } // constructor DataBaseSelect public String connect() { try { Class.forName("com.informix.jdbc.IfxDriver").newInstance(); return "Driver Loaded!"; } catch (Exception E) { return "Unable to load driver."; } } public String select() { //String value = getServletContext().getInitParameter("dbuser"); // System.out.println(getServletConfig().getServletContext().getInitParameter(" dbuser")); ServletConfig sc = getServletConfig(); ServletContext sctx = sc.getServletContext(); System.out.println(sctx.getInitParameter("dbuser")); try { Connection C = DriverManager.getConnection(url, "fabdev", "fabdev$"); Statement Stmt = C.createStatement(); ResultSet myResult = Stmt.executeQuery("SELECT lst_nm from person_profile ORDER BY lst_nm"); while (myResult.next()) { result.addElement(myResult.getString(1)); } // Clean up myResult.close(); Stmt.close(); C.close(); return "Connection Success!"; } catch (SQLException E) { return "SQLException: " + E.getMessage(); } } /** * Accessor for result **/ public Vector getResult() { return result; } /** * Mutator for result **/ public void setResult(Vector avector) { result = avector; } } // class DataBaseSelect Sure there's apathy in the world. But who cares. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, email: [EMAIL PROTECTED] Milt Epstein Research Programmer Software/Systems Development Group Computing and Communications Services Office (CCSO) University of Illinois at Urbana-Champaign (UIUC) [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, email: [EMAIL PROTECTED]
Re: getServletContext return nullpointer-exception
andreas ebbert wrote: Hi, I am trying to get a servlets context with getServletContext() but all I get is a NullPointerException. Can anybody help me? greetings, Andreas The usual reason for this is a failure to call super.init() in your servlet init method. Assuming your init is: public void init( ServletConfig config ){ super.init( config ) ; // Essential!! } -- WBB - [EMAIL PROTECTED] Java Cert mock exams http://www.lanw.com/java/javacert/ Author of Java Developer's Guide to Servlets and JSP ISBN 0-7821-2809-2