Avatar billede fredand Forsker
03. januar 2003 - 09:42 Der er 1 kommentar og
1 løsning

RequestDispatcher gives null??

Hello!

I have a problem with a RequestDispatcher.

I have servlet (servlet1) is supposed to forward a request to a other servlet (servlet2) with a method below but when my other servlet2 is executed and I try to extract the parameters in the forwarded request added from a form, send to servlet1, the parameters is null.

I think this should be possible, but I can't get it right. If you take a look at the doc at: http://java.sun.com/j2ee/sdk_1.2.1/techdocs/api/javax/servlet/RequestDispatcher.html

Below my method in servlet1.
   
    public void forward(String url) //url the path to servlet2
    {
            ServletContext servletContext = getServletContext();
            RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher( url );
            try
            {
                String[] myparam = (String[])request.getParameterValues("myparam");

                if(counties == null)
                {
                    System.out.println("NULL");
                }
                else
                {
                    //This always works!! With other words I can read "myparam " in servlet1
                    System.out.println("NOT NULL");
                    for(int i = 0; i < myparam.length; i++)
                    {
                        System.out.println("->" + myparam[i]);
                    }
                }
                requestDispatcher.forward(request, response);

            }
            catch(ServletException se)
            {
                se.printStackTrace();
            }
            catch(IOException ioe)
            {
                ioe.printStackTrace();
            }
    }
   
Below the code in servlet2 where i try to read "myparam" that should be forwarded in the request.

        String[] myparam = (String[])request.getParameterValues("myparam");

        if(myparam == null)
        {
            //This always happens!!!
            System.out.println("NULL");
        }
        else
        {
            System.out.println("NOT NULL ");
            for(int i = 0; i < myparam.length; i++)
            {
                System.out.println(myparam[i]);
            }
        }
       
Please Help!!

Best regards
Fredrik

Ps
I use orion as server 1.5.2
Avatar billede soelvpil Nybegynder
04. januar 2003 - 21:54 #1
Allow me to quote from the document you were linking to:

For a RequestDispatcher obtained via getRequestDispatcher(), the ServletRequest object has its path elements and parameters adjusted to match the path of the target resource.

I believe you have to manually append the requestparameters to the URL you are forwarding to. I would suggest capturing all parameternames and values from the url, and append them to the url you are forwarding to.
Avatar billede fredand Forsker
07. januar 2003 - 16:23 #2
Hello Soelvpil!

Thanks for your replay.

I have done som reaserch and made the question I bit more detailed. Please take a look at my new better question.

Best regards

Fredrik
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester