Request Dispatcher question

Hi all,
I have a simple question based on RequestDispatcher.
When we dispatch a file in our servlet, where should we place RequestDispatcher --- Within try block or after try, catch block as defined in this program
Please see the coding/program---------------
// import all necessary packages
public class SimpleServlet extends HttpServlet
public void doGet()throws ServletException,IOException
try
Here I am retrieving data from database and then store them into bean,and doing some coding.
This Servlet will call MyJsp.jsp file.
dispatch="/jsp/MyJsp.jsp";
Should we define RequestDispatcher here ?
catch
// OR here---------
RequestDispatcher dispatcher=request.getRequestDispatcher(dispatch)
dispatcher.forward(request,response);
} // end doGet() method
} // end class
Thanks
amitindia

Hi,
It`s better to use outside try{}catch{} block if you forward to a page based on some condition like..
try{
if(i.equals("soemthing")){
dispatch="/jsp/MyJsp.jsp";
else{
dispatch="/jsp/Error.jsp";
catch{}
RequestDispatcher dispatcher=request.getRequestDispatcher(dispatch)
dispatcher.forward(request,response);
OR if u`r going to forward to a single page whatever be the condition then you can use Dipatch inside the try{} block itself.
regards,
Saravanan

Similar Messages

  • Request dispatcher null

              The forward code we have is like
              public void forward(String url, HttpServletRequest request,
              HttpServletResponse response)
              throws ServletException, IOException
              RequestDispatcher dispatcher = getServletConfig().getServletContext().getRequestDispatcher(url);
              dispatcher.forward(request, response);
              My question is:
              As per the documents
              The pathname must begin with a "/" and is interpreted as relative to the current
              context root.
              if my URL is "http://www.abcz.com/<somepage>
              request dispatcher is null? and on one server (say A) that is a problem but other
              server(say B) when given the full URL behaves ok.
              on server A I am getting a null pointer exception , which on tracing happens to
              be the request dispatcher is null.(If http://www.abcz.com/<somepage> is URL) but
              no null pointer exception if url is "/<somepage>"
              On B its behaving fine when given the "http://www.abcz.com/<somepage> " as URL
              to the method.
              Both server A and B are running wl6. Is the difference in service pack cause of
              the problem?
              Internally does wl6 parses the URL to find a relative URL?
              Any help will be greatly appreciated.
              Thanks
              Rumpa
              

    And the reason for that is, RequestDispatcher is per application
              scope. And RequestDispatcher comes from the ServletContext which is
              the current application scope. So it has to be relative url. If you
              need to forward to an absolute url, you can use sendRedirect instead.
              Bhaskar
              "Matt Krevs" <[email protected]> wrote in message news:<[email protected]>...
              > as far as i know, you should always specify relative urls when using request
              > dispatchers
              >
              > eg
              >
              > if the entire url is
              > http://myserver/theurl
              > you would code as follows
              >
              > RequestDispatcher dispatcher =
              > getServletConfig().getServletContext().getRequestDispatcher("/theurl");
              > dispatcher.forward(request, response);
              >
              > You definitely should not use complete URLs when calling
              > getRequestDispatcher()
              >
              > what happens on the server that returns a null dispatcher if you manually
              > type the address into the browser? Does weblogic find the resource? If not
              > then you have something specified differently in your server that doesn
              > work.
              >
              >
              > "RUMPA GIRI" <[email protected]> wrote in message
              > news:[email protected]...
              > >
              > > The forward code we have is like
              > > /**********************************************/
              > >
              > > public void forward(String url, HttpServletRequest request,
              > > HttpServletResponse response)
              > > throws ServletException, IOException
              > > {
              > > RequestDispatcher dispatcher =
              > getServletConfig().getServletContext().getRequestDispatcher(url);
              > > dispatcher.forward(request, response);
              > > }
              > > /***********************************************/
              > >
              > > My question is:
              > > As per the documents
              > > The pathname must begin with a "/" and is interpreted as relative to the
              > current
              > > context root.
              > > if my URL is "http://www.abcz.com/<somepage>
              > > request dispatcher is null? and on one server (say A) that is a problem
              > but other
              > > server(say B) when given the full URL behaves ok.
              > > on server A I am getting a null pointer exception , which on tracing
              > happens to
              > > be the request dispatcher is null.(If http://www.abcz.com/<somepage> is
              > URL) but
              > > no null pointer exception if url is "/<somepage>"
              > > On B its behaving fine when given the "http://www.abcz.com/<somepage> " as
              > URL
              > > to the method.
              > > Both server A and B are running wl6. Is the difference in service pack
              > cause of
              > > the problem?
              > >
              > > Internally does wl6 parses the URL to find a relative URL?
              > >
              > > Any help will be greatly appreciated.
              > > Thanks
              > > Rumpa
              > >
              > >
              

  • Query Strings in Request Dispatcher Paths (PLT.16.1.1)

    I'm having trouble using query strings in a request dispatcher include to a servlet.
    I'm spitting debug out, and it looks like I'm calling:
    PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher("/myServlet?myparam=myparamValue");
    And myparam isn't being set for myServlet.
    The same URL ("http://myhost:myport/myPortal/myServlet?mayparam=mayparamValue)
    runs fine standalone....

    I'm not sure of the exact dates (either this month or the next). But
    JSR168 will be supported in the service pack.
    Subbu
    T-Bone said the following on 11/18/2003 09:23 AM:
    Subbu Allamaraju <[email protected]> wrote:
    There is a known bug in WLS's servlet container that results in this
    behavior. Please contact support if you need an expedited solution.Thanks, they said to wait for SP2 sometime end of November.
    Can you tell me when it will be available?
    Also, when will JSR-168 v1.0 be supported?
    Subbu
    T-Bone said the following on 10/26/2003 03:14 PM:
    I'm having trouble using query strings in a request dispatcher includeto a servlet.
    I'm spitting debug out, and it looks like I'm calling:
    PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher("/myServlet?myparam=myparamValue");
    And myparam isn't being set for myServlet.
    The same URL ("http://myhost:myport/myPortal/myServlet?mayparam=mayparamValue)
    runs fine standalone....

  • Urgent, creating xml files dynamically, request dispatcher problem

    hi all,
    Problem 1:
    There is a problem we are facing, while request dispatching.
    The files CBECBC.XML & CBECBC_Envelop.XML,
    both xml files. These files are created on request from the user, dynamically, and displayed on the browser,
    by including them in a jsp page.
    Here we face a problem when they are to be included into the jsp page.
    The error is the following :
    //////// Error ////////
    Could not find request dispatcher for the url CBECBC.xml
    Could not find request dispatcher for the url CBECBC_Envelop.xml
    The created files are stored in the "d:\pstudio35\desks\bank\BankWeb\".
    We are using " <jsp:include page="...."/> " (dynamic jsp include tag), to include the xml files.
    Problem 2:
    And could you just let me know how to include a xml file in a jsp file,
    so that the xml file is displayed in the exact format of xml(with the tags).
    I am using Pramati Studio 3.5
    Its urgent!!!
    Regards,
    Deepa Datar

    problem 2:
    either translate all < en > into < and > or display the XML in a textarea

  • Nested request dispatches when using RequestDispatcher in a Button action

    Hi all,
    I am new to JSF so I am probably missing something very simple, please help:
    The test case is simple: I created one visual web page with one button and one normal JSP file(named test.jsp) using Netbeans 5.5 with visual web pack on Sun Application Server 9 PE. The code action for the button is as follows:
    public String button1_action() throws Exception
            ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
            HttpServletRequest request = (HttpServletRequest) context.getRequest();
            HttpServletResponse response = (HttpServletResponse) context.getResponse();
            RequestDispatcher dispatcher = request.getRequestDispatcher("test.jsp");
            dispatcher.forward(request,response);
            return null;
        }Once the JSF page is loaded and the button is clicked, this exception occurs:
    ApplicationDispatcher[WebApplication1] Servlet.service() for servlet Faces Servlet threw exception
    javax.servlet.ServletException: WEB2651: Exceeded maximum depth for nested request dispatches: 20
    at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:837)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:697)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:532)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:465)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:353)
    at webapplication1.Page1.button1_action(Page1.java:242)
    Can anyone provide any advice on how to resolve this issue?

    Hi Rachel,
    As per the Servlet2.3 Javadoc:
    The pathname must begin with a "/" and is interpreted as relative to the current context root.
    If you make that change and include the complete path, then it would work fine.
    Ex:
    If your context looks like http://my.company.com/webapp1/ and if you were to have your forwardPortlet.jsp in a directory like:
    webapp1
      |
      |-> htdocs
             |
             |-> forwardPortlet.jsp
    then, your code would look like;
    RequestDiscpatcher d = request.getRequestDispatcher("/htdocs/forwardPortlet.jsp");
    HTH,
    thanks,
    harsha

  • Unable2download any content,site requests security questions.hav not set any up

    unable2download any content,site requests security questions.hav not set any up&amp;unable2proceed with purchases.
    please provide response asap.

    If this was purchased many months ago it may be because Apple changed its security set-up about six months ago at a rough guess. Go to and complete the questions posed at:
    iforgot.apple.com
    this leads you to https://iforgot.apple.com/iForgot/iForgot.html
    as I could not make the link work in draft mode of writing this response.

  • Request dispatch problem

    To all
    The problem is that my request dispatching is not forwarding the request and I haven't got a clue why. I have a upload servlet which is working fine as it parses the request and the forwards you to the same jsp page or the next jsp page according to what values it extracts. However I have set up a bean to display the contentType() of each request which is sent to it from the jsp pages, however nothing is displayed when the request is forwarded from the upload servlet. this is what is happening .....
    public class upload extends HttpServlet {
    public void doPost( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException {
    HttpServletRequest request = _req;
    HttpServletResponse response = _res;
    parseMultiForm pMF = new parseMultiForm( _req );
    Hashtable values = new Hashtable();
    String param;
    while ( ( param = pMF.getNextParameter()) != null) {
    String value = pMF.getParameter();
    values.put(param, value);
    if (values.containsKey("submit")) {
    String url = (String) values.get("reload");
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/" + url);
    dispatcher.forward(request, response);
    if (values.containsKey("continue")) {
    String url = (String) values.get("next");
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/" + url);
    dispatcher.forward(request, response);
    The above servlet forwards the request to a jsp which does the following....
    <jsp:useBean id="info" scope="session" class="InfoBean" />
    <jsp:setProperty name="info" property="request" value="<%= request %>" />
    However infoBean does not receive anything when the request is forwarded from the servlet??
    Can anyone see anything obviously wrong?? is it to do with the JSP asking for the <%= request> and not receiving it because the request dispatcher has altered it in some way?
    Thanks in advance anyone
    Andy.

    I have exactly the same problem. I can�t make the servlet pass the request object to the target jsp.
    Try not changing the request nor the response. Unfortunatelly due to the kind of job i do inside my servlet there's no way i can't touch the request. So if you can make it work with mi advice please let me know.

  • Unable to use Request Dispatcher; Duke $ Post;

    I am using a Controller Servlet to transfer control to a JSP using Request Dispatcher within doPost()
    RequestDispatcher rd;
    rd=req.getRequestDispatcher(URL);
    rd.forward(req,resp);
    I am seting a request object
    req.setAttribute("XXX",obj.func());
    prior to the Redirect;
    The page gets redirected to a JSP. I am unable to access req object in there.
    if i use
    String XXX = req.getAttribute("XXX");
    i get an error in the jsp as
    "req cannot be resolved"
    Do i need to import some directives? Please Reply

    The page gets redirected to a JSP. I am unable to
    access req object in there.
    if i use
    String XXX = req.getAttribute("XXX");
    i get an error in the jsp as
    "req cannot be resolved"
    Do i need to import some directives? Please ReplyThat's simple. Your req (which is the HttpServletRequest object) in the servlet becomes request in the JSP. That's always there. The compiler would not recognize req
    Use request.getAttribute("XXX"); in JSP
    Regards
    ***Annie***

  • Request dispatcher between 2 servers

    dear sear:
    can i use request dispatcher to go from one servers to another whith the same session.
    thanks,
    hamdi.

    Nope, Request dispatcher works with in its servlet container.
    May be you can you Request.sendRedirect()..
    not sure.

  • Passing Attribute in Request Dispatcher

    Hi,
    I am using servlet#1 to pass the value of an Attribute to servlet#2
    servlet #1 code
    ServletContext sc = getServletContext();
    request.setAttribute("name",value);
    RequestDispatcher rd = sc.getRequestDispatcher(url);
    rd.forward(request, response);
    servlet #2 code
    ServletContext sc = getServletContext();
    String string = (String) sc.getAttribute("name");
    when I run servlet #1 request dispatcher forwards request and response to servlet #2 but the Attribute value is not passed along
    what am I missing ?
    thanks VERY much for your response in advance and past responses !!
    Rick

    Hi rickdd,
    In servlet #2 you should get the value from
    servlet's Request object rather than ServletContext,
    because parameter is set with Request Object not with
    ServletContext.
    So modifiy the code as given below...
         /* Parameter is passed with servlet's Request Object.
          * So Next line needs to be commented.
         //ServletContext sc = getServletContext();
         //Here get the value from servlet's Request Object
         String string = request.getAttribute("name");
       This will work.
    Regards
    Goodieguy

  • Request dispatcher method--help needs

    Hi,
    I used request dispatcher method to redirect the from one servlet to another servlet.But when i use get method it is functioning well.
    But when i use post method it is giving error...
    I need to use post method...
    How can it possible..
    Thanks,
    Babu B

    you have code for request.RequestDispatcher.forward(""); method in both doPost and
    doGet methods for the servlet. However your initial form is not set with method
    to POST.
    By default the method is GET and it will execute code in doGet and not code in
    doPost() method. so in your first form use method as POST instead of keeping
    it to default i.e no method specified.
    let me know where are you using RequestDispatcher.
    //rohit

  • Request dispatcher vs redirect

    if there is the request dispatcher already,
              then what exactly is the point of having response.sendRedirect(url)
              is there any situation in which one should use sendRedirect over using the
              request dispatcher?
              thanks
              -Tao
              Tao Ge
              Software Engineer
              [email protected]
              (310) 883-1837
              NextEngine, Inc.
              401 Wilshire Blvd., Ninth Floor
              Santa Monica, California 90401
              This email is intended only for the use of the individual or entity to which
              it is addressed and contains information that is privileged and
              confidential. If the reader of this message is not the intended recipient
              or the employee or agent responsible for delivering the message to the
              intended recipient, you are hereby notified that any review, use, disclosure
              or distribution of this communication is strictly prohibited. If you have
              received this communication in error, please notify the sender immediately
              by reply email and destroy all copies of the original message.
              Thank you.
              

              1. You cannot forward to a file thats not this server. But you can use
              sendRedirect
              2. By using sendRedirect you are also informing browser regarding change of the
              location.
              3. When you post data to server and use of browser relaod posts data back to
              server in case of dispatcher.
              Thanks,
              Kumar.
              "Tao Y. Ge" wrote:
              > if there is the request dispatcher already,
              > then what exactly is the point of having response.sendRedirect(url)
              >
              > is there any situation in which one should use sendRedirect over using the
              > request dispatcher?
              >
              > thanks
              >
              > -Tao
              >
              > --
              >
              > Tao Ge
              > Software Engineer
              > [email protected]
              > (310) 883-1837
              >
              > NextEngine, Inc.
              > 401 Wilshire Blvd., Ninth Floor
              > Santa Monica, California 90401
              >
              > ---
              > This email is intended only for the use of the individual or entity to which
              > it is addressed and contains information that is privileged and
              > confidential. If the reader of this message is not the intended recipient
              > or the employee or agent responsible for delivering the message to the
              > intended recipient, you are hereby notified that any review, use, disclosure
              > or distribution of this communication is strictly prohibited. If you have
              > received this communication in error, please notify the sender immediately
              > by reply email and destroy all copies of the original message.
              > Thank you.
              

  • Can Request Dispatcher include 2 Jsp's ??

    HI ...
    can Request Dispatcher include 2 Jsp's.....in which
    second Jsp is accessing some parameters from First Jsp....
    From my point of view , both jsp's are get compiled seperately hence
    2nd Jsp is not getting parameters from 1st Jsp..
    Any other way to solve this problem....???
    rdTop = sc.getRequestDispatcher("/Reports/top.jsp");
    rdTop.include(req,res);
    rd1 = sc.getRequestDispatcher("/Reports/MenuTrail.jsp");
    rd1.include(req,res);
    Thanks

    No Actually ,i am using include becoz Top .jsp includes our menu,& menu trail also contains our menu...i am displaying both Jsp's thru
    servlet hence i have used Include.....
    Thanks

  • Request Dispatcher, where to difine it ?

    Hi all,
    I have a simple question based on RequestDispatcher.
    When we dispatch a file in our servlet, where should we place RequestDispatcher --- Within try block or after try, catch block as defined in this program
    Please see the coding/program---------------
    // import all necessary packages
    public class SimpleServlet extends HttpServlet
    public void doGet()throws ServletException,IOException
    try
    Here I am retrieving data from database and then store them into bean,and doing some coding.
    This Servlet will call MyJsp.jsp file.
    dispatch="/jsp/MyJsp.jsp";
    Should we define RequestDispatcher here ?
    catch
    // OR here---------
    RequestDispatcher dispatcher=request.getRequestDispatcher(dispatch)
    dispatcher.forward(request,response);
    } // end doGet() method
    } // end class
    Thanks
    amitindia

    Depends on what exceptions you are trying to catch! It could be placed typically outside.

  • Request dispatcher and jsp pages called from servlets

              Given the following webapp directory structure:
              root/
              jsp/
              ..all jsp files
              web-inf/
              classes/
              /servlets
                   ..all servlet files
              /com
              ..all other java class files
              I have the jsp directory coded as: jspdir = "/jsp/" (and have tried variations
              on this theme)
              I try to open a new jsp page with the following code in a servlet in the above
              servlet directory:
                   RequestDispatcher rd;
                   rd = getServletContext().getRequestDispatcher(jspdir + myjsp.jsp);
                   rd.forward(req, res);
              rd continues to return null.
              The jsp pages are not registered in the web.xml file for this webapp, if that
              makes any difference.
              Does anyone have any suggestions?     
              [problem.txt]
              

    I create a small sample and put these files into /jsp in my war context
              it works fine, please send me the example code you are using and the
              environment description of the deployed application
              I attached my samples, that work
              Filip
              ~
              Namaste - I bow to the divine in you
              ~
              Filip Hanik
              Software Architect
              [email protected]
              www.filip.net
              "Bill Blackmon" <[email protected]> wrote in message
              news:[email protected]...
              >
              > Yes, I am able to access the jsp pages with the url listed.
              >
              > "Filip Hanik" <[email protected]> wrote:
              > >The weblogic servlet engine, is by default mapping anything that ends
              with
              > >*.jsp to be a JSP.
              > >so there shouldn't be a problem. You didn't answer my first question,
              > >are you able to access your JSP through the browser?
              > >http://localhost:7001/root/jsp/myjsp.jsp
              > >
              > >Filip
              > >
              > >--
              > >~
              > >Namaste - I bow to the divine in you
              > >~
              > >Filip Hanik
              > >Software Architect
              > >[email protected]
              > >www.filip.net
              > >"Bill Blackmon" <[email protected]> wrote in message
              > >news:[email protected]...
              > >>
              > >> The code you are reading in the message here was pasted in and
              > >> looks bad, so maybe that's what you are referring to? The dispatcher
              code
              > >is called
              > >> in the service() method of the servlet.
              > >> I have a suspicion that the jsp directory needs to be defined someplace
              > >-
              > >weblogic.properties
              > >> or web.xml?, though I don't see this in any of the examples that come
              > >with
              > >the product.
              > >>
              > >>
              > >>
              > >> "Simon Ng" <[email protected]> wrote:
              > >> >
              > >> >Hello Bill,
              > >> > Did you put a <BR>
              > >> >return;
              > >> ><BR>
              > >> >statement right after the dispatcher.forward() statement? Well, I am
              > >assuming
              > >> >your
              > >> >code is in the doPost() or doGet() methods.
              > >> >
              > >> >Simon
              > >> >
              > >> >
              > >> >"Filip Hanik" <[email protected]> wrote:
              > >> >>if you do http://localhost:7001/root/jsp/myjsp.jsp
              > >> >>
              > >> >>do you get a result?
              > >> >>
              > >> >>make sure you have the right character case on the filename.
              > >> >>
              > >> >>Filip
              > >> >>
              > >> >>--
              > >> >>~
              > >> >>Namaste - I bow to the divine in you
              > >> >>~
              > >> >>Filip Hanik
              > >> >>Software Architect
              > >> >>[email protected]
              > >> >>www.filip.net
              > >> >>"Bill Blackmon" <[email protected]> wrote in message
              > >> >>news:[email protected]...
              > >> >>>
              > >> >>> Given the following webapp directory structure:
              > >> >>> root/
              > >> >>> jsp/
              > >> >>> ..all jsp files
              > >> >>> web-inf/
              > >> >>> classes/
              > >> >>> /servlets
              > >> >>> ..all servlet files
              > >> >>> /com
              > >> >>> ..all other java class files
              > >> >>>
              > >> >>>
              > >> >>> I have the jsp directory coded as: jspdir = "/jsp/" (and have tried
              > >> >>variations
              > >> >>> on this theme)
              > >> >>> I try to open a new jsp page with the following code in a servlet
              > >in
              > >> >the
              > >> >>above
              > >> >>> servlet directory:
              > >> >>> RequestDispatcher rd;
              > >> >>> rd = getServletContext().getRequestDispatcher(jspdir + myjsp.jsp);
              > >> >>> rd.forward(req, res);
              > >> >>>
              > >> >>> rd continues to return null.
              > >> >>>
              > >> >>> The jsp pages are not registered in the web.xml file for this
              webapp,
              > >> >>if
              > >> >>that
              > >> >>> makes any difference.
              > >> >>>
              > >> >>> Does anyone have any suggestions?
              > >> >>>
              > >> >>
              > >> >>
              > >> >
              > >>
              > >
              > >
              >
              [filipInclude.jsp]
              [filipForward.jsp]
              [filip.jsp]
              

Maybe you are looking for

  • Xorg problems.

    I have some problems with my xorg file, and X will not start.  I am running an Ati x1400 using the new catalyst drivers, here is a copy of the xorg log and my xorg.conf.  Any help would be greatly appreciated. Xorg log: X.Org X Server 1.4.2 Release D

  • IPhone went bad after software update - iPhone 5 is now stuck on Itunes connect and has vertical lines

    iPhone went bad after software update - iPhone 5 is now stuck on Itunes connect and has vertical lines. I found a video on youtube with similar issues - http://youtu.be/Xuuqxx18-bA EVERYTHING HAS BEEN TRIED: Restore, Reboot, Recycle, Replacing Cables

  • Problem parsing SimpleDateFormat date format to database

    I have a page where I am getting parameter values form another page a constucting a string from these values which i then want to insert into my SQL database. The field i am putting the data into is of type DATETIME and i am using the SimpleDateForma

  • What will happen if I clear my entire list of browsing history

    I keep having the error message that says, "not responding". I am afraid to clear the browsing history because I might lose everything I have saved. Please let me know what I need to do and how safe is it to delete the entire list of browsing histroy

  • Javax.smartcardio problems...

    HI, i am trying to develop a simple applet in java, which should read data from a smartcard... But when i want to read the terminals: CardTerminal terminal = (CardTerminal)factory.terminals().get(0); the full method: CardTerminal terminal = (CardTerm