0

How to send/pass values from one servlet(consider it as one project) to another servlet(consider as another project). It's showing number format exception. Is it correct to pass values in sendredirect method or is there any other way

Example:

File: uzkpk2.java

         String a1=request.getParameter("a[0]");             
         aa1=Integer.parseInt(a1);


         String a2=request.getParameter("a[1]");             
         aa2=Integer.parseInt(a2);


         String a3=request.getParameter("a[2]");             
         aa3=Integer.parseInt(a3);


         String a4=request.getParameter("a[3]");             
         aa4=Integer.parseInt(a4);    


   response.sendRedirect("http://localhost:8080/CSP/czkpk1?y="+y+"&a1="+aa1+"&a2="+aa2+"&a3="+aa3+"&a4="+aa4);
    } 
    catch(Exception e)
    {
        out.println(e);
    }
}    

}


File: czkpk1.java

 aaa1=Integer.parseInt(request.getParameter("aa1"));
        aaa2=Integer.parseInt(request.getParameter("aa2"));
        aaa3=Integer.parseInt(request.getParameter("aa3"));
        aaa4=Integer.parseInt(request.getParameter("aa4")); 
2
  • Depends on what you're trying to do with those servlets. What exactly is your goal? Commented Feb 28, 2011 at 5:51
  • Looks duplicate of stackoverflow.com/questions/4321151/…. Commented Feb 28, 2011 at 5:53

2 Answers 2

1

You are using the wrong request parameter to get the value.

    aaa1=Integer.parseInt(request.getParameter("aa1"));
    aaa2=Integer.parseInt(request.getParameter("aa2"));
    aaa3=Integer.parseInt(request.getParameter("aa3"));
    aaa4=Integer.parseInt(request.getParameter("aa4")); 

Instead of this use

    aaa1=Integer.parseInt(request.getParameter("a1"));
    aaa2=Integer.parseInt(request.getParameter("a2"));
    aaa3=Integer.parseInt(request.getParameter("a3"));
    aaa4=Integer.parseInt(request.getParameter("a4")); 

since in czkpk1.java you are using the variable names instead of parameters passed in the url present in response.sendRedirect();

And one advice chech for only numeric values before parsing it into string.

The best way to do this is use concept of

Servlet Chaining.

Sign up to request clarification or add additional context in comments.

Comments

0

-> Write your value in request context as an attribute using request.setAttribute()

-> After forward the request to second servlet using RequestDispatcher.forward()

-> In second servlet read the value using request.getAttribute()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.