• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

getRequestDispatcher()

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using this getRequestDispatcher() in my code to forward a request (from one servlet ) to a resource (another servlet). But it is not working fine and display 404 error. I have the following queries, can any body answer to them.

1. Are both the servlet must be mapped or registered in web.xml.?
2. Are the servlets must be in certain packeges or we can use them from within WEB-INF/classes directory. If we can use them then what will be the path to the getRequestDispatcher() in case of Context and Request. I know the problem is with the path.

Naveed


 
Ranch Hand
Posts: 250
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

1. Are both the servlet must be mapped or registered in web.xml.?

>>> YES

2. Are the servlets must be in certain packeges or we can use them from within WEB-INF/classes directory. If we can use them then what will be the path to the getRequestDispatcher() in case of Context and Request. I know the problem is with the path.

>>> YES you can keep them under WEB-INF/classes syntax will be like ---

getRequestDispatcher("/servlet/MyServlet");

You have to give "/servlet/MyServlet" mapping in web.xml .
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you provide us your DD and the code. We may look into it.

Thanks,
Pabak
 
sawan parihar
Ranch Hand
Posts: 250
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>servlets.FirstServlet</servlet-class>
</servlet>

<servlet>
<servlet-name>SecondServlet</servlet-name>
<servlet-class>servlets.SecondServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<url-pattern>/firstservlet/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>SecondServlet</servlet-name>
<url-pattern>/SecondServlet/*</url-pattern>
</servlet-mapping>


package servlets;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletContext;
import javax.servlet.RequestDispatcher;


public class FirstServlet extends HttpServlet
{


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

System.out.println("Servlet Called");


ServletConfig config = getServletConfig();
ServletContext cntx = config.getServletContext();



RequestDispatcher dispatch = cntx.getRequestDispatcher("/SecondServlet");
dispatch.forward(req,res);

}

}


package servlets;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletContext;
import javax.servlet.RequestDispatcher;


public class SecondServlet extends HttpServlet
{


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

System.out.println("Second Servlet Called");

}

}

Hope that helps
 
Naveed Ali
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My DD is,

<web-app>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>myclasses.dispatcher</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher1</servlet-name>
<servlet-class>myclasses.dispatcher1</servlet-class>
</servlet-mapping>
</web-app>



/*Here are both the servlets,*/
// First servlet
package myclasses;
/*
a bunch of import statements
*/
public class dispatcher extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
req.setAttribute("firstname","Jhon");
req.setAttribute("lastname","David");
RequestDispatcher yourname = req.getRequestDispatcher("dispatcher1");
if (yourname != null) yourname.forward(req,res);
}
}


// Second servlet

package myclasses;
/*
a bunch of import statements
*/
public class dispatcher1 extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String fname, lname;
fname = (String)req.getAttribute("firstname");
lname = (String)req.getAttribute("lastname");
PrintWriter out = response.getWriter();
out.println("<p>Hello, " + lname + " " + fname + "</p" );
}
}

myclasses directory is, WEB-INF/classes/my classes.
If i don't use dispatcher(comment out the forward()) in the first servlet and just print to
the browser then it prints. But when i use the dispatcher mechanism it gives 404 error.

naveed
 
Naveed Ali
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry i missed the second servlet in DD, it was cut & past mistake,
<web-app>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>myclasses.dispatcher</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/dispatcher</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>dispatcher1</servlet-name>
<servlet-class>myclasses.dispatcher1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher1</servlet-name>
<url-pattern>/dispatcher1</url-pattern>
</servlet-mapping>

</web-app>
 
I'm just a poor boy, I need no sympathy, because I'm easy come, easy go, little high, little low, little ad
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic