I've list from db and put in json list with checkbox and the value. I need the value for insert to table db. So, how to get value from the selected checkbox in java? Thanks a lot.
In JSP File
function refreshList() {
var collection = "";
collection = collection
+ " <table><thead><th class='LIST'><span><label>Selection</span></label></th><th class='LIST'><span><label>List</span></label></th></thead><tbody>";
var isnew = $("input:radio[name=isnew ]:checked").val();
var string = "";
$.getJSON('${pageContext.request.contextPath}/master.do?reqcode=refreshList', {
'isnew ' : isnew
}, function(result) {
$.each(result,function(key,value) {
string = string + "<tr><td> "
+ "<input id='id' type='checkbox' value=" + key + "/></td>" + "<td> " + value
+ "</td></tr>";
});
collection = collection + string + " </tbody></table> ";
$('#collectionid').html(collection);
});
}
<div id="collectionid"></div>
You need to have a name attribute on the checkbox.
<input name="chkBox" type="checkbox" value="xxx" />
You can get the checkbox value from servlet request object as shown below:
String[] values = request.getParameterValues("chkBox");
Related
I created a linkedlist object as follows
importBuffer = new BufferedReader(new FileReader(importcsvFile));
while ((line = importBuffer.readLine()) != null) {
// use comma as separator
String[] importedFile = line.split(cvsSplitBy); //cap,comune,provincia,stato
System.out.println("Codice Azienda " + importedFile[0] + " , Codice Cliente=" + importedFile[1] + " , Regione Sociale=" + importedFile[2] + " , Indrizzo=" + importedFile[3] + " , comune=" + importedFile[4] + " , provincia=" + importedFile[5] + " , stato=" + importedFile[6] +"]");
counter++;
PublicDefinition.importList.add(importBuffer.toString());
List customers = select.select(importedFile[0],importedFile[1], importedFile[3]);
if(!customers.isEmpty())
{
System.out.println("selected Customer : " + customers.size());
buffureList = customers;
Object a=List.class.cast(customers);
PublicDefinition.testingList.add(buffureList.toString());
System.out.println("selected Customer : " + PublicDefinition.importList.get(0));
System.out.println("selected Customer : " + PublicDefinition.testingList.getFirst());
updateCustomer = customers;
if(customers.get(0)==importedFile[0])
System.out.println("Matched Codice Azienda");
select.updateTable(importedFile[1], importedFile[3], "10.34", "11.40"); //String CodiceCliente, String indrizzo, String latitude, String longitute
}
}
when I try to access the elements for the linkedlist using
System.out.println("selected Customer : " + PublicDefinition.importList.get(0));
I got the output:
selected Customer : java.io.BufferedReader#420dc55b
I think this is the memory reference, but I want to retrieve the value of the linkedlist
my select function is:
public List<Customer> select(String codiceAzienda, String codiceCliente, String indrizzo) {
return jdbcTemplate.query(
"SELECT * FROM customers WHERE CodiceAzienda= ?",
new Object[] { codiceAzienda},
(rs, rowNum) -> new Customer(rs.getLong("id"),
rs.getString("CodiceAzienda"), rs.getString("Indrizzo"), rs.getString("codice_cliente"), rs.getString("Indrizzo")));
}
You added the toString() value of the importBuffer object, not the actual contents. The default toString() implementation (which every object inherits from... Object) returns ClassName#HashCode. So your output isn't wrong, but your input is.
See Object.toString() in the javadoc
Go ahead and perform:
PublicDefinition.importList.add(importBuffer.readLine());
Instead of :
PublicDefinition.importList.add(importBuffer.toString());
Since you are trying to output the contents of the buffered reader instead of the ClassName#Hashcode contents.
Replace
PublicDefinition.importList.add(importBuffer.toString());
with
PublicDefinition.importList.add(importedFile);
You are accidentally adding the string representation of BufferReader object, not the list of import files which sounds like your intention.
I need to POST data and at the same time redirect to that URL in REST environment. I can do this for normal strings, but the requirement is to POST specific Object.
The way I do it for normal string is -
public Response homePage(#FormParam("username") String username,
#FormParam("passwordhash") String password) {
return Response.ok(PreparePOSTForm(username)).build();
}
private static String PreparePOSTForm(String username)
{
//Set a name for the form
String formID = "PostForm";
String url = "home";
//Build the form using the specified data to be posted.
StringBuilder strForm = new StringBuilder();
strForm.append("<form id=\"" + formID + "\" name=\"" +
formID + "\" action=\"" + url +
"\" method=\"POST\">");
strForm.append("<input type=\"hidden\" name=\"" + "username" +
"\" value=\"" + username + "\">");
strForm.append("</form>");
//Build the JavaScript which will do the Posting operation.
StringBuilder strScript = new StringBuilder();
strScript.append("<script language=\"javascript\">");
strScript.append("var v" + formID + " = document." +
formID + ";");
strScript.append("v" + formID + ".submit();");
strScript.append("</script>");
//Return the form and the script concatenated.
//(The order is important, Form then JavaScript)
return strForm.toString() + strScript.toString();
}
But this method is not sending Objects. I need a work around to send complex Objects. Please help me with this issue.
Thanks in advance.
I am working in a application where i have to make an html table in the java class and have to save that in database.I am creating that table in java but the how to generate dynamic row in that table .Using 3 lists.I am giving what i have done,
+"Interview LineUp"
+" <table border ='1'>"
+"<tr>"
+"<td>Interviewe</td>"
+"<td>Timing1</td>"
+"<td>Timing2</td> "
+"</tr> "
+"<tr>"
+"<td>name</td>"
+"<td>timing1</td> "
+"<td>timing2</td> "
+"</tr> "
+"</table>"
So this is the table i am using in the java class,and i have 3 lists which contains 3 set of information like name,timing1,timing2.Now i want that if there are 3 values in all the lists then 3 rows will be generating.
The lists are
List<String> interviewTimingToFrom1 = Arrays.asList(interviewTime1.split(","));
List<String> interviewTimingToFrom2 = Arrays.asList(interviewTime2.split(","));
List<String> listOfinterviewerName = Arrays.asList(intervierwName.split(","));
Like i am doing this
+"<tr>";
for(int k=0;k<listOfinterviewerName .size();k++){
+"<td>listOfinterviewerName .get(k)</td>"
+}
How to do that,in that java class ?? somebody please help .Thanks in advance
+"test" is not a valid Java statement. What are you adding the text to?
When building a String incrementally, you should always use a StringBuilder.
List<String> interviewTimingToFrom1 = Arrays.asList(interviewTime1.split(","));
List<String> interviewTimingToFrom2 = Arrays.asList(interviewTime2.split(","));
List<String> listOfinterviewerName = Arrays.asList(intervierwName.split(","));
StringBuilder buf = new StringBuilder();
buf.append("<html>" +
"<body>" +
"<table>" +
"<tr>" +
"<th>Interviewe</th>" +
"<th>Timing1</th>" +
"<th>Timing2</th>" +
"</tr>");
for (int i = 0; i < listOfinterviewerName.size(); i++) {
buf.append("<tr><td>")
.append(listOfinterviewerName.get(i))
.append("</td><td>")
.append(interviewTimingToFrom1.get(i))
.append("</td><td>")
.append(interviewTimingToFrom2.get(i))
.append("</td></tr>");
}
buf.append("</table>" +
"</body>" +
"</html>");
String html = buf.toString();
Of course, to guard against Cross-site scripting (XSS) attacks, you should escape the values.
I was not so clear about your question, but as per what I understood, the code below will work for you.
public static void main(String[] args) {
String s = ""+"Interview LineUp"
+" <table border ='1'>"
+"<tr>"
+"<td>Interviewe</td>"
+"<td>Timing1</td>"
+"<td>Timing2</td> "
+"</tr> "
;
String interviewTime1="11:30,12:30";
String interviewTime2="13:30,15:00";
String intervierwName="Adam,Smith";
List<String> interviewTimingToFrom1 = Arrays.asList(interviewTime1.split(","));
List<String> interviewTimingToFrom2 = Arrays.asList(interviewTime2.split(","));
List<String> listOfinterviewerName = Arrays.asList(intervierwName.split(","));
for(int i=0;i<interviewTimingToFrom1.size();i++)
{
s = s.concat( "<tr>"
+"<td>"+listOfinterviewerName.get(i)+"</td>"
+"<td>"+interviewTimingToFrom1.get(i)+"</td> "
+"<td>"+interviewTimingToFrom2.get(i)+"</td> "
+"</tr> ");
}
s=s.concat( "</table>");
System.out.println(s);
}
I have created an html string in which I need to set the background image.
protected void onPostExecute(HashMap<String,String> hPlaceDetails){
String backgroundImage = ??????
String name = hPlaceDetails.get("name");
String icon = hPlaceDetails.get("icon");
String vicinity = hPlaceDetails.get("vicinity");
String lat = hPlaceDetails.get("lat");
String lng = hPlaceDetails.get("lng");
String formatted_address = hPlaceDetails.get("formatted_address");
String formatted_phone = hPlaceDetails.get("formatted_phone");
String website = hPlaceDetails.get("website");
String rating = hPlaceDetails.get("rating");
String international_phone_number = hPlaceDetails.get("international_phone_number");
String url = hPlaceDetails.get("url");
String mimeType = "text/html";
String encoding = "utf-8";
String data = "<html>"+
"<body background="+backgroundImage+"><img style='float:left' src="+icon+" /><h1><center>"+name+"</center></h1>" +
"<br style='clear:both' />" +
"<hr />"+
"<p>Vicinity : " + vicinity + "</p>" +
"<p>Location : " + lat + "," + lng + "</p>" +
"<p>Address : " + formatted_address + "</p>" +
"<p>Phone : " + formatted_phone + "</p>" +
"<p>Website : " + website + "</p>" +
"<p>Rating : " + rating + "</p>" +
"<p>International Phone : " + international_phone_number + "</p>" +
"<p>URL : <a href='" + url + "'>" + url + "</p>" +
"</body></html>";
// Setting the data in WebView
mWvPlaceDetails.loadDataWithBaseURL("", data, mimeType, encoding, "");
}
Note: I have my background image (background9.png) in the location MyProject/res/drawable-xhdpi. Please suggest how I need to set
<body background="+backgroundImage+">
Instead of using \res\drawable-xhdp\background9.png as the path as you mentioned in your comment, you should use:
<body background='file:///android_res/drawable/background9.png'>
I've just tested this and it works perfectly.
Please add a tag for android to clarify your question.
Anyway, you need to get a reference for your parent layout, by using findViewById(R.id.yourActivitysParentLayout)
You need then to set it's background using a ResourceManager. From the top of my head, the method name you will need is yourparent.setBackgroungDrawable(drawable). The drawable can be obtained from a resource manager instance.
This can all be avoided by setting it's background in the xml if possible by using android:background="#drawable/background9" //sorry not sure if .png is needed. xhdp is not though.
Based on the help availed from stackoverflow I created the table with hiddenfields to pass the value to servlet, but in servlet I am unable to get the values of the input fields.
Here is my jQuery code to create table:
$("#linkInstr").click(function() {
var arr = new Array();
var cdid = $("#cboinstr option:selected");
var code = $("#cbocode option:selected");
$.get("trnDC?caseNo=21&insid="+cdid.text(), function(data) {
arr = data.split(",");
var contents = '<tr><td><input type="checkbox" id="chk_select'+counter+'" /></td><td><input type="hidden" id="txtCodeid'+counter+'" value="'+code.text()+'"/> ' + code.text()+ '</td><td><input type="hidden" id="txtInstrid'+counter+'" value="'+cdid.text()+'"/>' + cdid.text() + '</td><td>' + arr[0] + '</td><td>' + arr[1] + '</td><td>' + arr[2] + '</td></tr>';
alert(contents);
$("#tblDetails").append(contents);
counter++;
})
})
And here is my Servlet code:
int noOfRows = Integer.parseInt(request.getParameter("noOfRows"));
for (int i = 0; i < noOfRows; i++) {
int j = i + 1;
String codeid = request.getParameter("txtCodeid" + i);
throw new Exception(request.getParameter("txtCodeid" + i));
String instrId = request.getParameter("txtInstrid"+i);
st.executeUpdate("insert into trndcdtls values(" +ccode +"," +fyear_code +"," +Dcno +"," + j +",'" + codeid +"','" + instrId + "','"+status+"')");
}
The exception is throwed purposefully by myself to check any value is availble on request, but it shows nothing.
You need to give the input fields a name. This becomes the request parameter name.
Thus, instead of
<input id="foo">
<input id="bar">
you need
<input name="foo">
<input name="bar">
The ID is purely to identify the elements at the client side, not at the server side. You may add them, but they won't be sent to the server side. Only the name-value pairs of an input element will be sent as request parameters to the server side.
That said, please use PreparedStatement in your Java code to avoid SQL injection risks. Further on, it surprises me that you got the Java code compiled with the Exception in the middle of the flow. It would have given "unreachable code" error.