How do I repopulate a form using Java? - java

I have a form that is submitting to a servlet and processed with the doPost() method. Once the form is submitted, the user is taken to another page with a "Back" button (not the browser's back button). If the Back button is clicked, the form should repopulate with the original input. How can I accomplish this?

It is your responsibility to keep the information and repopulate the form manually. You can do this in one of two ways:
Either you push the content to
the client and read it back in when
necessary.
Or you can save the
form data for that client in the
server space.
I prefer the later by creating a model which represents the form data. When the form is processed, you can save a copy for that client. If the page is reloaded, you can check for the existence of a previous model. If it exists you use that data to populate your form. If it does not exist, you can use a default model or empty values.
There are other advantages of gathering your content into a model which will benefit you later on. For example, if one makes changes to a form and you have an 'Undo' button near the 'Submit' and 'Clear' buttons, you can easily revert to the last known state. In order to active the 'Undo' button you can easily do:
boolean isDirty = savedModel.equals(currentModel);
And enable/disable the 'Undo' button using the boolean rather than having tons of 'if/else' statements to see if a value was changed. Much cleaner.
Jeach!

If you use javascript:history.back() your browser should automatically take care of that for you.

You'll have to keep up with the data the user submitted in the form.
You could put that data on the in a url, on the request or in the session.
Make sure you escape any unsafe characters when putting them back into the form.
There are a lot of web frameworks out there that make this sort of thing easier to do.
Struts and Stripes are two that come to mind.

After original form is submitted you have several server-side options:
keep form values in session and populate them with the form called from 'Back' action;
add form values as parameters to 'Back' action url so they will be part of the request.

If the user submits the form, is the data put into a database? If so, simply query the database and repopulate the form... If not, consider saving the data to a database.

Related

Pass data from editable HTML table to servlet

So i have a JSP page, that contents editable table with checkbox in each row, the question is, how can i pass the data from selected rows to servlet?
Can i pass it as a parameter, or get html code in servlet to parse it and get selected data?
Thank you.
Your question needs some more specifics. But here is a perfect example of how to implement editable tables:
https://code.google.com/p/jquery-datatables-editable/
It is very easy to implement and basic knowledge of jQuery will help you play around as much as you like. It even shows examples of how to use data at Java servlets.
It depends on when you want pass the values to the servlet. If you want to make all the selections and then submit, a html form submit will be the best option. once you submit the form, the data will be available in request.getParameterValues('names'). If you want to send values immediately after user selects, then you can use ajax through an onchange event

How to retain form values in Struts2?

I have an action say actionA associated with a JSP page say A.jsp and another actionB associated with B.jsp. There's a form on A.jsp. User submits some data which is set in actionA properties. Now user navigates to B.jsp (or any other page for that matter) and then comes back again to A.jsp. The form data in the fields of A.jsp is lost when the user comes back. I want to retain that data.
I cannot use hidden fields since I have a lot of fields and cannot use sessions either. Is there a way in Struts2? If not can you suggest me any other way?
When you want to send data from a pagexto another, as you're working on a http protocol you have two ways, use parameters (hidden fields in post submit or url parameters in get) or you have to use session.
If you can't do neither, think about saving your A state on a database and pass only a key that let you retrieve the A state when you are back.
Sorry for the generic answer but it's all I can say if you don't give more details.

How to check if I arrived at a page from same page vs. another page

I have a Java Web App. One of the pages contains a form with 3 checkboxes and a table.
Depending on which checkboxes are checked, different info gets populated in the table.
There is no submit button. The form is submitted whenever the user checks or unchecks one of the boxes.
Theoretically, I want the user to be able to uncheck all the boxes, which will result in the table being empty. However, if the user arrives at the page from another page, or upon initial visit to the app, I want them to have one of the boxes checked by default, and the respective data displayed in the table.
You could differentiate the request to the same page (submission) from the requests comming from other pages (or direct url access) by the HTTP method.
Make your form use a POST method and check if the request method is GET (direct access or link from another page) or POST (form submission).
Better pass 3 different request parameters based on your 3 checkboxes to your JSP/Servlet and depending on the parameters modify the table accordingly.
if user is coming from another page, at the start of the doGet or doPost, set your request parameter to a default value.
you can use sessions to help determine where you a user is coming from. Before a page redirects to another page have the orignal page set some session attribute. Somewhere at the top of the page you can check the user's session attribute to see where they are coming from
i.e.
Page 1
session.putValue("referringPage", "Page1");
Page 2
if (session.getValue("referringPage") != "Page2") {
//do something
}
Edit:
Better method might just be to check the header using something like
request.getHeader("Referer"));

Retaining previous form data

I have a form that I could put on to one page, but for aesthetic reasons, I want to split into two. The original form, on submission, would go to a Servlet which would get the form data and insert it into a database. However, I don't know how to make this work when it is split into two different forms on two different pages. My forms are currently in html but I could change them to JSP if that is the solution. I do not want to use hidden fields; if there is no way to do it without hidden fields, then I will just put it on one page as my form has quite a few fields and hidden fields would mean basically doubling the amount of code. I also only want to use html or jsp for the forms; I don't want to use JavaBeans, and I want to avoid scripting in the jsp's. I have already done this by simply dividing the database logic into two and using the ID of the last inserted object as a hidden field, and the second form then uses that ID to update that item, but this is not an elegant solution and could cause a problem if one user submitted the first form and a different user submitted the second (the wrong item would be updated). Is this possible?
Well, this is one of the things the Session can be used for: store the data from the first page in the HttpSession and then in the second page retrieve the session data and save it to the database.
You can use javascript pagination (It'll save page loading time. Many online test applications adopt this approach)
a) Either on submission on each part of form data will send to server using ajax call.(partial submission)
b) Or the whole data will be maintained at client side only until whole form is completed and submitted successfully.
(depends on your application need)
You can logically relate each part of form with some unique id & session id combination. In addition, if you are not willing/required to store whole form data in session, you can have primary key in session. It'll make database update process easier for 2nd or next part of form data.

Adding elements to editable form using h:dataTable without submitting all the rows

Just wondering if it is possibly to dynamically add elements (in AJAX fashion) to a form using h:dataTable without always submitting the current contents? Normally one has to always send the full contents of the datatable, because the whole datatable is re-rendered. If the datatable gets big (say, 100 rows) this can radically slow down the page, as each row always gets posted.
Doing this with raw JS this is not a problem, but can it be avoided with JSF? Of course if we don't send all the row data with the AJAX request, it remains lightweight, but we will lose all the changes made to the form.
PrimeFaces has a datatable with ability to add rows without always submitting the current content.
You can see datatable ajax example.
Follow the instructions of getting started.
Using JSF, which re-renders the whole component when making changes on it, this is simply not possible.
As the whole component is re-rendered, all changes to it are lost except when they are submitted to the server before refresh.
JSF is designed in a way that this is not (easily) possible. One good hack to this would be to keep track of those rows that have been modified and then submit only those. This can be achieved by attaching a change listener to each field in the form.

Categories

Resources