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
Related
I am generating a table depending on the user's selected choice by calling a servlet using AJAX call onchange of dropdown. The table is generating fine, but the problem is I want to page the table as well as it has a lot of rows. I have tried a lot of jquery plugins, but haven't been able to page the table as most plugins want table id which is only generated on ajax call.Please Help...!
Simply call the paging plugin in the success callback for the AJAX request, since at that point the table will exist and you should know the id of the table. If you don't know the id of the table at that point the easiest solution would be to modify the server-side code to pass that back as part of the response (as a property of a JSON object).
If that's not an option then you'll have to traverse the DOM in order to get a reference to the table, then get the id from that. I'd need to know the HTML structure of the page to tell you exactly how that would look.
You can use the this plugin
http://tablesorter.com/docs/
You can trigger the sorting externally by refering this url
http://tablesorter.com/docs/example-trigger-sort.html
I'm trying to set delete buttons on each row of my table in order to let users to delete some rows of the table. However i'm having a terrible time to remove this rows dynamic and remove the data from database... ( no idea how to use the button onclick from html with jsp )
I decided to use some javascript inside the Jsp which did the tricky to remove the row but now i can't delete the entry from the database...
I would like to know if there is anyway to delete data from the database using the javascript and what should be the easiest way to do it ?
If you want to use JavaScript then you'll need to send an AJAX request to your server and then remove the row from the view upon success of the previous action.
For an easy and simple approach to AJAX you can try PrototypeJS. However, if you'd like to have really fancy dynamic features you may be more interested in jQuery.
Without knowing more from your project, I'd suggest to use a Servlet in the server side to receive the AJAX request.
I would like to use Ajax with div and InnerHTML. Onclick to the Remove button I will one javascript function which will make a call to server with unique id to delete the record from database , when we get response back I will hide innerHTML representing that row based on div Id.
What is the proper but simple way to display and edit SQL table on JSP pages, having all the SQL and Java code in Servlet?
Coming from ASP.NET environment, I'm looking for GridView substitute - to display editable rows on JSP page but have all the SQL methods in Servlet file. My goal is to have 2 JSP pages: 1st for simple table display, 2nd as a CMS to edit these rows (save button for every record), and a Servlet with most of the Java code.
I would appreciate some simple example to display editable table in JSP, with Java/SQL code in Servlet. I've seen some bits and pieces online, but each one is using different approach. It would be best not to edit any server settings.
Thanks in advance.
You could evaluate DisplayTag with input elements in it.
As far as simple JSP is concerned, there is no such control as GridView in ASP.NET.
You can try JSF based controls like DataTable (http://www.mkyong.com/jsf2/jsf-2-datatable-example/). But it does not have any in-build editing functionality like GridView. Datatable will provide you enough control over it so as to implement your own edit/delete/add logic.
To start with JSF, refer this http://www.mkyong.com/tutorials/jsf-2-0-tutorials/ or http://www.vogella.com/articles/JavaServerFaces/article.html
I would suggest using jqGrid.
It is sort and simple plus all you have to do is mapping your CRUD url to controllers where you do the the SQL operations.
http://www.trirand.com/blog/jqgrid/jqgrid.html
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.
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.