Friday, March 4, 2011

Difference Betwixt The Getrequestdispatcher Together With Getnameddispatcher Inwards Servletcontext?

The ServletContext course of educational activity provides 2 methods getRequestDispatcher(String url-pattern) and getNamedDispatcher(String name), which tin plough over notice last used to dispatch a asking to a especial servlet. The primary divergence betwixt getRequestDispatcher() together with getNamedDispatcher() method is that old accepts a URL blueprint or path spell after agrees amongst a Servlet mention defined inwards deployment descriptor or web.xml file, both render an event of RequestDispatcher class, which tin plough over notice farther piece of job to forwards or redirect a asking inwards Java spider web application. Let' run into roughly to a greater extent than especial most these 2 methods together with how they work.


getRequestDispatcher(String url-pattern)

This method is defined inwards ServletContext course of educational activity together with it returns a RequestDispatcher object that acts equally a wrapper for the resrouce located at the given URL blueprint or path, for example, if nosotros guide keep next servlet mapping inwards our deployment descriptor :

<servlet-mapping>
<servlet-name>InfoServlet</servlet-name>
<url-pattern>/info</url-pattern>
</servlet-mapping>

than

RequestDispatcher dispatch = request.getRequestDispatcher("/info");

volition render RequestDispatcher for InfoServlet, hither /info represents the url-pattern of the InfoServlet.

You tin plough over notice equally good piece of job RequestDispatcher to forward a asking to a JSP page, equally shown below.

RequestDispatcher dispatch = request.getRequestDispatcher("/index.jsp");

Here path to index.jsp is relative to the origin of the spider web application, that's why nosotros guide keep a forwards slash prefix: "/index.jsp" to forwards the request, only telephone band dispatch.forward(request, response) where asking together with response are HttpServletRequest together with HttpServletRespose object.

If you lot are interested to larn to a greater extent than most asking forwarding inwards Servlet, you lot tin plough over notice farther see books)
  • Difference betwixt ServletContext and ServletConfig inwards Servlet? (answer)
  • 6 Difference betwixt forward() together with sendRediret() in Servlet? (answer)
  • Difference betwixt jsp:include together with jsp:forward activity inwards JSP? (answer)
  • 10 Servlet together with JSP Interview Questions for Java Programmers (questions)
  • Difference betwixt GenericServlet together with HttpServlet inwards Servlet API? (answer)
  • How to care customer sessions inwards a Servlet application? (answer)
  • Difference betwixt include directive together with include activity inwards JSP? (answer)
  • 5 Free Courses to larn Servlet together with JSP in-depth (courses)
  • Difference betwixt the spider web server, application server, together with Servlet container? (answer)
  • Can you lot declare Constructor within the Servlet class? (answer)
  • Thanks for reading this article thence far. If you lot similar these Servlet Interview questions, together with thence delight portion it amongst your friends together with colleagues. If you lot guide keep whatever questions or feedback, together with thence delight drib a note. 

    P. S. - If you lot are novel into Servlet together with JSP together with looking for a gratis tutorial or online course of educational activity to larn basics, I propose you lot become through Java Servlets together with JSP - Build Java EE(JEE) app inwards 25 Steps - a gratis course of educational activity on Udemy. It's completely free, all you lot necessitate to exercise is exercise an Udemy trace organization human relationship to enroll inwards this course.

    No comments:

    Post a Comment