How to pass a table-row value to the servlet? - java

I have a table which has certain attributes and a button in the last column of each row.
<tr>
<td>B001</td>
<td>The Catcher In The Rye</td>
<td>199</td>
<td><select>
<option>1</option>
<option>2</option>
<option>3</option>
</select></td>
<td><button>Add To Cart</button></td>
</tr>
I have to get the entire row value in id, name, price, quantity respectively in the Servlet I am using. How do I do that?
Using forms we do something like -
String name = request.getParameter("LoginName");
But there is no id here for the table. So how to achieve the same so that I can retrieve a particular row's values?

Put some hidden fields inside your page to carry selected row data.
Set those hidden fields with the clicked row data using javascript when button is clicked.
And submit the form.
try this http://jsfiddle.net/LBJWQ/7/
This is JSP & Servlets 101. You should be reading up on HTML and Javascript as well.

Related

Unable to I to display the list value in dropdown which I am passing as list from my java class in my JSP.My code works perfectly if I am using table

I am passing list from my java class in my JSP where I want to display the list value in dropdown.
While debugging I checked that the list is containing desired values but not displaying in my JSP dropdown.
Though the values are coming if I am displaying in Table form.
Please, tell me where I am going wrong and how can I make my code to work?
Java Code:
// add the event list to the model
theModel.addAttribute("event", theEvents);
return "fundContributionCollectionListPage";
JSP Code:
<select name="eventSelected">
<c:forEach var="tempEvents" items="${event}">
<option value="tempEvents.eventName">${tempEvents.eventName}</option>
</c:forEach>
</select>

How to get all values from a select box(selected & not selected) in jsp

I have a HTML select box which have multi select enabled in my jsp. I'm populating options for that dynamically from other button. So user won't be selecting any values from that select box. I have to pass all the values of my selectbox to other jsp.
I'm navigating through jsp using form action submit. When doing that I cannot able to get all my select box values.
request.getParameterValues
This one giving only the selected values from that multi-select box. What I want is to get all values from that selectbox no matter selected or not.
Thanks;
Well simply loop through your select and get every option value, try the following:
var select = document.getElementById('mySelect');
for(var i=0; i<select.options.length;i++){
console.log(select.options[i].value);
}
This is a DEMO Fiddle.
For this you can use Web Storage for store the value and getting back that value form Web Storage.
For more detail you can visit below links.
Create a localStorage name/value pair
Example: for Web Storage
Code Below
you can store data in 1st Page (Html1.html) as below code :
localStorage.setItem("key", "value");
you can get the data on 2nd Page (Html2.html) as below code :
var valueFromFirstPage = localStorage.getItem("key");
You can store the list of values to the HttpSession and retrieve them wherever you need for the user session.
In your jsp ,
HttpSession session =request.getSession(false);
session.setAttribute("ListName", yourListHere);
to set the values to the session and to retrieve them,
session.getAttribute("ListName");
Related:
Storing and retrieving values from JSP to servlet using Sessions
In your first jsp, have a hidden field with all possible values for the select box, so that on the next jsp you will have the values by the given name, of-course name will be different for hidden field and the select box.
CSV hidden
first.jsp
<select name='chosen' multiple>
...
</select>
<input type='hidden' name='toChoose' value='1,2,3,4'/>
next.jsp
String[] chosen = request.getParameterValues("chosen");
String[] toChoose = request.getParameter("toChoose").split(",");
multiple hidden
first.jsp
<select name='chosen' multiple>
...
</select>
<input type='hidden' name='toChoose' value='1'/>
<input type='hidden' name='toChoose' value='2'/>
....
next.jsp
String[] chosen = request.getParameterValues("chosen");
String[] toChoose = request.getParameterValues("toChoose");

get text from dynamic table cell to to database

Alright so what I want to do is save text from my table to the database. The table is dynamically generated via a hibernate connection to the db. A sample of the html in the .jsp for the table:
display:column property="ticketId" href="/ossWeb/displayTicketEdit.do" paramId="ticketId" title="Ticket ID"/>
<display:column property="ATCNotes" title="ATC Notes" class="notes" />
<display:column property="issue" title="Issue"/>
<display:column property="status" title="Status"/>
Each property maps directly to a column of a view in the db.
I use this jQuery to allow any cell in the ATC Notes column to be clicked and turn into a text box:
$(".notes").live('click',(
function(){
var text = $(this).text();
$(this).text('');
$('<input type="text" />').appendTo($(this)).val(text).select().blur(
function(){
var newText = $(this).val();
$(this).parent().text(newText).find('input:text').remove();
});
}));
When you click off the cell, the text replaces what was already in the cell. Each row corresponds to a ticketId as can be seen in my first line of the jsp. Now my question is, how do I get that entered text to the db? I have a check box on each row, and for that, I can just assign it a value of "${ATCQueue.ticketId}" but there doesn't seem to be any similar way to differentiate each cell in the ATC Notes column.
I've heard of AJAX being used for something somewhat close to this, but honestly I'm pretty new to this and know next to nothing about AJAX.
I don't think you mandatorily need AJAX for this. You can write an action servlet and submit the form.
Or you can go for native AJAX using XMLHttpRequest/Response. Another way would be to use DWR framework.
1.First thing if you want to save bulk amount of data I i.e; multiple row and multiple column then you need to set a flag(a hidden field or
local storage in html 5) for modified rows.
2.Iterate through th rows of the table and generate xml or json .
3.yes you heard it right the Ajax, now send the data to your code behind through Ajax call.

dynamically update textbox value from database when combobox value is changed

Here is the situation. I have a drop down menu. The option sin this drop down menu are being populated by fetching some values from the database. To do this following is what i have done.. :-
<select name="product_list" onchange="selectProduct(this.value)">
<option value="none">Select one</option>
<%
List<String> options = new ArrayList<String>();
DynamicCombo comboBox = new DynamicCombo();
options = comboBox.generateComboBox();
Collections.sort(options);
int tempVar = 0;
while (tempVar < options.size()) {
out.print("<option value=\"");
out.print(options.get(tempVar));
out.print("\">");
out.print(options.get(tempVar));
out.print("</option>");
tempVar++;
}
%>
</select>
DynamicCombo is a class that has a method called 'generateComboBox()'. This method simply returns an array list containing all the values that are fetched from the database, which is what i need to show in my drop down box in the front end (jsp page). On my jsp page i simply iterate through this list and print it as options appropriately.
This works absolutely fine.
Now i have another text box on my form, say 'textbox1'. Now the requirement is that this text box value should be updated depending on what the user has selected from the above drop down box.
So for example if the user selects 'prod1'(which is a primary key in the backend database table) option from the drop down box, then the corresponding value ( the product name) should be fetched from the database table and should be updated in the textbox named 'textbox1'.
The other thing is this entire thing is contained in a form which is supposed to be finally submitted to the servlet for further processing.
So how can i achieve this.
i figured out the solution to my own problem. It might not be the most elegant way of doing it, but it does the job pretty well.
So as per my requirement, what i exactly wanted to do was.... insert a value (that will be fetched from the database) into a text box on my form depending on what the user chooses from the drop down box that is already present on my form.
To achieve this, i went about and thought if some how i could nest a form withing my main form, it'd solve my issue. But i discovered that nesting of forms is not allowed. So the next option i thought of was to some how submit the same form without the user clicking on the submit button and also handle it appropriately as an 'incomplete' submit (in the sense that the form is still to be submitted manually by the user by clicking on the submit button) on the server.
So i simply made use of the 'onChange' event of a drop down box. I created an additional hidden field on my form.I wrote a simple javascript function that would simply set the value of the hidden field to the string-"partial Submit" and would submit my main form (say named 'form1') as :-
document.getElementById("hidden_id").setAttribute("value","partial submit");
form1.submit;
The function that does the above will be called whenever (and everytime) the onchange event of the drop down box gets fired.
When the user finally clicks on the submit button on the form to submit the finally completed form, then another javascript function is called that simply sets the value of the hidden field on the form to the string, "final submit" and would submit the form as :-
document.getElementById("hidden_id").setAttribute("value","final submit");
form1.submit;
Now on my server, i checked for the value of this hidden field as :-
if(request.getParameter("hidden_id").equals("partial Submit"))
{
// make a database connection, pass the value user selected from the drop down box
// to a prepared statement that does the query for getting the 'productName' from
// the database, collect the returned string in a variable and set a
// request attribute with this returned value. This value can simply be used in the
// jsp to fill in the value part of the textbox1.
}
else
{
if(request.getParameter("hidden_id").equals("final Submit"))
{
// do the rest of the final processing that needs to be done when user finally
// submits the completed form.
}
else
{
// throw an exception to take care of the possibility that the user might send
// in a 3rd value as a value for the hidden field.
}
}
Since you havent provided the code for selectProduct(this.value) , i presume that it submits the jsp page as when you change the value in the drop down.
If that the case in the servelt, set the value that you want to show in jsp in request object
request.setAttribute("valuetodisplay" ,valuetodisplay);
and now in jsp
<input type="text" value ='<%= request.getAttribute("valuetodisplay")%>' />

Assign selected item of a dropdown list of jsp file to a java variable (say String selection)

I want to display the table data from database according to the selected item from a dropdown list in my jsp file, so all I need to do is assign that selected value to a variable of String type, how do i do that, suggest me the piece of code along with the code of dropdown list.
Assume you have a dropdown element in your form of your JSP page as below:
<select name='ele1'>
<option value='a'>A</option>
<option value='b'>B</option>
...
</select>
After submitting the form, you can get the element value from HttpServletRequest:
String selectedValue = request.getParameter("ele1");
You can use Ajax, check out the link http://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=580

Categories