Re: this.getServletConfig() returns null

2020-02-14 Thread Richard Monson-Haefel
That worked! Thank you!

On Fri, Feb 14, 2020 at 1:10 PM Mark Thomas  wrote:

> On 14/02/2020 18:29, Richard Monson-Haefel wrote:
> > Hi,
> >
> > I'm experimenting with using annotations.  I created a Servlet with
> > annotations and then attempt to get the init parameters in the doGet()
> > method, but I keep getting a null value when I use
> > this.getServletConfig().  If I save the ServletConfig in an instance
> > variable from the init() method it works as expected.  Shouldn't the
> > this.getServletConfig() return the configuration object instead of a
> null?
> > What am I missing?
>
> You need to call super.init(confg)
>
> Mark
>
>
> >
> > Here is a listing. The code is also attached. I've run it both with and
> > without a web.xml file (just the root element when present).
> > @WebServlet(
> > name="myservlet",
> > urlPatterns={"/"},
> > initParams={
> > @WebInitParam(name="name", value="Richard"),
> > @WebInitParam(name="greeting", value="Hola")
> > }
> > )
> > public class TheServlet extends HttpServlet {
> >
> >   ServletConfig myConfig;
> >
> >   public void init(ServletConfig config) throws ServletException{
> >myConfig = config;
> >   }
> >
> > protected void doGet(HttpServletRequest request, HttpServletResponse
> > response) throws ServletException, IOException {
> >
> > // Set content type
> >   response.setContentType("text/plain");
> >
> >// Get initialization parameters
> >
> >//ServletConfig config = this.getServletConfig();
> >//^^  The above returns null 
> >
> >ServletConfig config = myConfig;
> >//^^^The above works 
> >
> >if(config != null){
> >   String name = config.getInitParameter("name");
> >   String greeting = config.getInitParameter("greeting");
> >   response.getWriter().println(greeting + " " +name);
> >}else{
> >   response.getWriter().println("there is no config");
> >}
> >   }
> > }
> >
> > Thanks in advance!
> >
> > Richard
> >
>
>
> -
> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
> For additional commands, e-mail: users-h...@tomcat.apache.org
>
>

-- 
Richard Monson-Haefel
https://twitter.com/rmonson
https://www.linkedin.com/in/monsonhaefel/


Re: this.getServletConfig() returns null

2020-02-14 Thread Mark Thomas
On 14/02/2020 18:29, Richard Monson-Haefel wrote:
> Hi,
> 
> I'm experimenting with using annotations.  I created a Servlet with
> annotations and then attempt to get the init parameters in the doGet()
> method, but I keep getting a null value when I use
> this.getServletConfig().  If I save the ServletConfig in an instance
> variable from the init() method it works as expected.  Shouldn't the
> this.getServletConfig() return the configuration object instead of a null?
> What am I missing?

You need to call super.init(confg)

Mark


> 
> Here is a listing. The code is also attached. I've run it both with and
> without a web.xml file (just the root element when present).
> @WebServlet(
> name="myservlet",
> urlPatterns={"/"},
> initParams={
> @WebInitParam(name="name", value="Richard"),
> @WebInitParam(name="greeting", value="Hola")
> }
> )
> public class TheServlet extends HttpServlet {
> 
>   ServletConfig myConfig;
> 
>   public void init(ServletConfig config) throws ServletException{
>myConfig = config;
>   }
> 
> protected void doGet(HttpServletRequest request, HttpServletResponse
> response) throws ServletException, IOException {
> 
> // Set content type
>   response.setContentType("text/plain");
> 
>// Get initialization parameters
> 
>//ServletConfig config = this.getServletConfig();
>//^^  The above returns null 
> 
>ServletConfig config = myConfig;
>//^^^The above works 
> 
>if(config != null){
>   String name = config.getInitParameter("name");
>   String greeting = config.getInitParameter("greeting");
>   response.getWriter().println(greeting + " " +name);
>}else{
>   response.getWriter().println("there is no config");
>}
>   }
> }
> 
> Thanks in advance!
> 
> Richard
> 


-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org