am i implementing the post redirect get approach in java web. i have this index.jsp which i can add information to database.
<form action="servlet" method="post>
<input type="text" placeholder="itemname"/>
<input type="text" placeholder="itemprice"/>
<input type="submit" value="add item"/>
</form>
and in servlet i process the username and password
//returns a boolean if success or not
if(processItem(itemname,itemprice)){
response.sendRedirect("secondservlet?ADD=success");
}
and in the secondservlet
if(request.getParameter("ADD").equals("SUCCESS"))
request.getRequestDispatcher("success.jsp").forward(request,response);
am i doing it right?
am i doing it right?
You are sending a POST, then redirecting, which results in a GET. If that is what you were trying to do, then yes, you are doing it right.
Note that your second servlet should probably check if getParameter(..)
returns null
. You might get to the second servlet from some other call that doesn't include any request parameters.