Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

RequestDispather  RSS feed

 
Venkatesh Kulkarni
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Iam developing an application in which selecting an option from the combo box in a jsp page would invoke a servlet. The servlet updates some database tables and returns the control back to the jsp page (through RequestDispatch).
My problem is, after the servelt dispatches to the old jsp, the jsp is getting refreshed and thus losing all the field data.
Please help me solve the problem. its very urgent.
 
Caine Lai
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An HTML option box has no state. There is no way for it to know which option should be selected. When you reload that page, it is simply outputting the HTML that was originally there.

You must pass an attribute to the JSP page from the servlet that will indicate which value should be selected in the option box.

// A quick and dirty example.
// Servlet that handles your JSP request.
String optionSelected = request.getParameter("myOptionBox");
request.setAttribute("optionSelected", optionSelected);

// A quick and dirty example.
// JSP code that selects the correct option in the option box.
<select name="myOptionBox">
<c:forEach items="${ optionData }" var="option">
<option value="${ option }" <c:if test="${ option == optionSelected }">selected</c:if>>${ option }</option>
</c:forEach>
</select>

Hope this helps. You might check java.sun.com for some JSP and Servlet tutorials.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!