wood burning stoves 2.0*
The moose likes Servlets and the fly likes Why the form hidden field values can't be passed into servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Why the form hidden field values can Watch "Why the form hidden field values can New topic
Author

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

James Long
Greenhorn

Joined: Feb 02, 2001
Posts: 14
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
Travis Gibson
Ranch Hand

Joined: Oct 17, 2000
Posts: 100

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


Regards,<BR>Travis M. Gibson, SCJP<BR>Java Developer<BR>www.travismgibson.com<BR>travis@travismgibson.com
James Long
Greenhorn

Joined: Feb 02, 2001
Posts: 14
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

Joined: Feb 02, 2001
Posts: 14
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why the form hidden field values can't be passed into servlet?
 
Similar Threads
How to loop through data on a jsp form?
post form data when slection changed in list
JSP action help(Basic question)
Resubmitting Refresh Problem
Where to put HTML files