• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why the form hidden field values can't be passed into servlet?

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,
The problem I have is the following in detail.
I have one page to let user to select the table name in the database, the user Go the second page (ModifyForm.jsp) to modify Employee data and using POST method to submit to ModifyServlet to Update the Employee data in the database table. Then ModifyServlet return back to the ModifyForm.jsp and let the user to continue to modify the Employee data.
The database table name from the firstPage's (there is a form) request parameter (at the First Time) or from the ModifyServlet's request attribute (not the first time access the ModifyForm.jsp).
My ModifyForm.jsp is like the following:
========================================
String tableName = null;
if(request.getAttribute("tableName") != null){
tableName =request.getAttribute("tableName");
}else if(request.getParameter("tableName")){
tableName = request.getParameter("tableName");
}else{
tableName = "Employee"; <%-- Default table name --%>
}

<FORM name="modifyForm" action="/MyApplication/servlet/ModifyServlet" method="post" >
Imployee ID: <INPUT type="text" name="empID" value="">
Last Name: <INPUT type="text" name="lname" value="">
First Name: <INPUT type="text" name="fname" value="">
Address: <INPUT type="text" name="address" value="">
<INPUT type="hidden" name="tableName" value="<%= tableName %>">
<INPUT type="submit" name="action" value="Modify">
</Form>
==============================================
There is a hidden field in this form called "tableName",
which is used to update the database "Employee" table data.
After the user change the address, fill in their name
in the above form and click the "SUBMIT" button.
ModifyServlet is called, after it update the database table successfully, the ModifyServlet called the following method to redirect back to the ModifyForm page in order for user to modify the another Employee data
==========================================
...... // Update database table
String tableName = request.getParameter("tableName");
.....
request.setAttribute("tableName", tableName);
getServletConfig().getServletContext().getRequestDispatcher
.("/jsp/ModifyForm.jsp").forward(req,res);
======================================

The problem is that if I first time to click "Modify" button,
it works fine.
But if I continue to click the "Modify" button in the returned ModifyForm page from the ModifyServlet. In the broswer URL address keep the same as I first submit the form. i.e.: http://localhost:8080/MyApplication/servlet/ModifyServlet
Sometimes I continue to click the "Modify" button in the returned ModifyForm page for 10 or more times, there is no problem. But sometimes if I clicked the second time or the fifth time on the "Modify" button in the returned ModifyForm.jsp page. Error.jsp is shown. I found there was no hidden field values in the ModifyForm passed to ModifyServlet from ModifyForm.
In this case my ModifyServlet throw a Exception called MyException, JSP error page will catch it and tell me that the hidden field value for table name passed from the ModifyForm to ModifyServlet is null.
In error.jsp page there is a BACK button, If BACK button is clicked, the web broswer can go back to the previous page. But after the above Exception is throwed, I clicked the BACK button, and the web browser give the following messages:
=============================================================
Warning: Page has Expired The page you requested was created using information you submitted in a form.
This page is no longer available. As a security precaution, Internet Explorer does not automatically resubmit your information for you.
To resubmit your information and view this Web page, click the Refresh button.
==============================================================
The above jsp error page is not always to be present because the hidden fields value can be passed into ModifyServlet properly. But sometimes jsp error page will be shown just second time I clicked the "Modify" button. But even it will never happen.
I don't know why hidden field didn't be passed to the Servlet.
May it possible not be passed from the ModifyServlet to ModifyForm.jsp back through calling request.setAttribute("tableName", tableName)?

Thank you for any one who can help me to solve this problem.
What do I need to change?
Best Regards

W. Long
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Howdy,
You coded the following:
String tableName = null;
if(request.getAttribute("tableName") != null){
tableName =request.getAttribute("tableName");
}else if(request.getParameter("tableName")){
tableName = request.getParameter("tableName");
}else{
tableName = "Employee"; <%-- Default table name --%>
}
Part of your problem is that: request.getAttribute("tableName")
returns an Object not a String. Without a (String) cast this code will cause an exception since you have String tableName setup as a String type.
Hope this helps...
Regards,
Travis M. Gibson, SCJP
Java Developer www.travismgibson.com
travis@travismgibson.com

 
James Long
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your reply
String casting for tableName from request.getAttribute(..),is no problem now, but the above problem still exist.
In the current situation I only wanted to use the hidden field
to pass the "tableName" to the ModifyServlet. Currently
I think my problem is from the browser's cache size.
But one doubt in my mind is that sometimes I just repeated the second time to click "Modify" button in the ModifyForm.jsp page
which return the ModifyServlet, that error occured. In this case,
what caused this problem?
Any suggestion?
Thank you again.
J Long
 
James Long
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your reply
String casting for tableName from request.getAttribute(..),is no problem now, but the above problem still exist.
In the current situation I only wanted to use the hidden field
to pass the "tableName" to the ModifyServlet. Currently
I think my problem is from the browser's cache size.
But one doubt in my mind is that sometimes I just repeated the second time to click "Modify" button in the ModifyForm.jsp page
which return the ModifyServlet, that error occured. In this case,
what caused this problem?
Any suggestion?
Thank you again.
J Long
 
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic