You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@felix.apache.org by chihi asma <ch...@yahoo.fr> on 2009/06/04 11:41:26 UTC

Servlet dispatcher problem

 
hi,

i am trying to implement 2 servlets in a bundle using the httpservice. there is the code :
the first servlet:
public class FirstServlet extends HttpServlet {
                                                                     
   protected void doGet(HttpServletRequest req, HttpServletResponse res)
                                                                 
           throws ServletException, IOException {
                                                                             
       req.setAttribute("name", "sana ");
       RequestDispatcher dispat = req.getRequestDispatcher("/servlet2");
       dispatcher.forward(req,res);
                   
                                       
   }
                                               
}

the second servlet:

                     public class SecondServlet extends HttpServlet {
                       protected void doGet(HttpServletRequest req, HttpServletResponse res)
                                                                                                         throws ServletException, IOException {
                         res.setContentType("text/plain");
                         PrintWriter out = res.getWriter();
                                                   
                                                                                             
                         out.println(req.getAttribute("name"));
                                                                                                                             
                       }
                     }


but i get an error : null pointer exception in my webbrowser ( the problem is in line  dispatcher.forward(req,res); )


Can you help me ?

thanks in advance

Asma



      

Re: Servlet dispatcher problem

Posted by Filippo Diotalevi <fi...@gmail.com>.
On Thu, Jun 4, 2009 at 11:41 AM, chihi asma <ch...@yahoo.fr> wrote:
>
> hi,
> i am trying to implement 2 servlets in a bundle using the httpservice. there is the code :
> the first servlet:
> [...]
>
> the second servlet:
....


Hi,
the problem **might** be in the
req.getRequestDispatcher("/servlet2")

The javax.servlet specification supported by Apache Felix HTTP service
is 2.1. It's a quite old spec, and apparently there is no
ServletRequest.getRequestDispatcher method, as you can see comparing
http://java.sun.com/products/servlet/2.1/api/javax.servlet.ServletRequest.html#_top_
with
http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletRequest.html

To confirm that it is the problem,  can you check if the following
example is working?
(The FirstServlet is slightly different from your:)

----------------------------
public class FirstServlet extends HttpServlet {

	   protected void doGet(HttpServletRequest req, HttpServletResponse
res)
	           throws ServletException, IOException {
	
	       req.setAttribute("name", "sana ");
	       RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("/servlet2");
	       dispatcher.forward(req,res);
	   }
	}
----------------------------
public class SecondServlet extends HttpServlet
{
    protected void doGet(HttpServletRequest req, HttpServletResponse
res)  throws ServletException, IOException
    {
      res.setContentType("text/plain");
      PrintWriter out = res.getWriter();
      out.println(req.getAttribute("name"));
    }
  }
----------------------------
public class Activator implements BundleActivator{

	public void start(BundleContext context) throws Exception {
		 ServiceReference sr  =
context.getServiceReference(HttpService.class.getName());
		 if(sr != null) {
		    HttpService http = (HttpService)context.getService(sr);
		    if(http != null) {
		      http.registerServlet("/servlet1", new FirstServlet(), null, null);
		      http.registerServlet("/servlet2", new SecondServlet(), null, null);
		    }
		 }
	}

	public void stop(BundleContext context) throws Exception {
	}
}

-- 
Filippo Diotalevi

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org