-1

I have two servlet. The first (doGet) shows me the form and the second (doPost) processes the form

Here is my first servlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        Nodes nodes = nodes_dao.start(request);
        int id = nodes.getId_node();

        request.setAttribute("nodes", nodes);
        request.setAttribute("id", id);
        request.getRequestDispatcher(VUE).forward(request, response);
    }

And here is my second servlet

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        String question = null;
        String result = null;   

        question = request.getParameter("question");
        result = request.getParameter("result");
        Node_dao dao = new Node_dao();

        try
        {
            dao.insert_result(result);
            int left_id = dao.select_left_id(result);
            dao.insert_question(question, left_id);


        }

For example, how I can retrieve the id of the first servlet in the second?

Thanks

Biohazard401
  • 41
  • 1
  • 2
  • 11
  • You could use either session, request or servlet context sharing, see here: http://stackoverflow.com/questions/123657/how-can-i-share-a-variable-or-object-between-two-or-more-servlets – Piet van Dongen May 06 '14 at 14:49
  • Put it in a hidden input field in the form? Its not like you directly go from servlet A to servlet B, in between there is a client (browser) which submits the form data. At least that's what I think, its very vague what you post since there is also a forward in there... – Gimby May 06 '14 at 14:49
  • Why would anyone suggest he add a hidden input when he is clearly adding the id into the request attributes and then forwarding? There's not even an HTML form in the above code. – developerwjk May 06 '14 at 22:49
  • @developerwjk The question is confusing for those of us who haven't seen forwarding used in this manner before. Why would you want to do what OP did above? Also, where he said there is a "GET" being done and then a "POST" implied two separate requests, not forwarding. It was assumed that he left out the HTML for brevity. – KyleM May 07 '14 at 18:14
  • @KyleM, I agree. I'm a little confused too. – developerwjk May 07 '14 at 19:49

2 Answers2

0

You are already calling request.setAttribute("id", id); in th first servlet, then forwarding to the second. So all you are missing is to call int id = (int)request.getAttribute("id"); in the second servlet.

HOWEVER, there is a second problem. You cannot magically change the METHOD type by forwarding. If the original request was GET, it is still GET after the forward. So your second servlet needs to handle the request in a doGet not a doPost.

developerwjk
  • 8,619
  • 2
  • 17
  • 33
  • ok thanks, I knew this problem but in the first jsp it's a series of questions with method get. And in the second jsp, the user insert a new question and new result with a method post. So, i have one servlet with doGet and the second with doPost. There is no solution ? Thanks – Biohazard401 May 07 '14 at 16:22
-1

You could do this using cookies or httpsession. This link will be interesting for you: http://www.journaldev.com/1907/java-servlet-session-management-tutorial-with-examples-of-cookies-httpsession-and-url-rewriting