-1

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 attribiute 2) is that possible to see the result in servlet2 in the same mashin? becuse I just create another class wich name is Servlet2 on the same project and define the name and the path in web.xml. Is that right approch?

Aira
  • 5
  • 4

2 Answers2

0
  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.

Chetan Jadhav CD
  • 1,116
  • 8
  • 14
0

Servlets are created to handle requests sent by clients. I assume that your servlet2 class does a such service. If you declare a public static variable in a servlet, it is accessible by any class. Therefore you don't need to send your data to client from servlet1 and get the client sent them back to servlet2. If you have common variables to all servlets in a web server, you can use static variable. If the sole purpose of servlet2 is printing your data, it shouldn't be a servlet, just a java class would be fine.

Remember that only 1 servlet instance will be created for all requests. Therefore don't use instance variables to store client specific data. Try to use sessions.

This should help you.

Community
  • 1
  • 1
pahan
  • 567
  • 1
  • 8
  • 22