javaarraysservletsweb-applications

Send array from one servlet to another servlet and print it?


First of all, I want to send parameters from html to Servlet and it works. Then I create an array from parameters and I want to send that array to another servlet. and just print it in Servlet2.

Here is my code:

    public void doGet(HttpServletRequest request,
                        HttpServletResponse response) throws ServletException, IOException {
                //System.out.println("XML servlet called!"); 
                response.setContentType("text/html");
                  response.getWriter();
                  //read value from selection
                  String videoname = request.getParameter("video");
                  String videoformat = request.getParameter("format");
                  String videoquality = request.getParameter("quality");
                  
                  //System.out.println("Name" + videoname);
                 //System.out.println("format" + videoformat);
                 //System.out.println("quality" + videoquality);
                 String [] chain1 = {"v1","f1","q1"};
                 String [] chain2 = {"v1","f1","q2"};
 if (videoname.equals(chain1[0]) && (videoformat.equals(chain1[1])) && (videoquality.equals(chain1[2])) ){
                
                 request.setAttribute("chain",chain1);
                
             }
            }else if (videoname.equals(chain2[0]) && (videoformat.equals(chain2[1])) && (videoquality.equals(chain2[2])) ){
                 request.setAttribute("chain",chain2);}
            
             
             RequestDispatcher dispatch = request.getRequestDispatcher("/Servlet2");
         dispatch.forward(request, response);

And in Second Servlet, my code is:

String value = (String)request.getAttribute("chain");
        System.out.println("Chain is" + value);

My problem is this is doesn't work. I have 2 problems. 1) how to send attribute 2) is that possible to see the result in servlet2 in the same mashin? because I just create another class which name is Servlet2 on the same project and define the name and the path in web.xml. Is that the right approach?


Solution

    1. How to send attribute? What you are doing to send the attribute (using request.setAttribute and then using dispatch.forward is correct.
    2. Assuming you have just created one new servlet named Servlet2 within the same project and have configured it correctly in web.xml, you should be able to get the attribute in that servlet's GET or POST method.

    I believe that you are running into issues because you are modifying the response, which should be done by Servlet2 and not Servlet1. Remove the following lines from your code

     response.setContentType("text/html");
     response.getWriter();
    

    ,since you are not handling the response in Servlet1. This should work, if not, modify your question and include the complete stack trace of the error that you get when you try to compile/run this.