passing date from jsp to servlet using getParameterValues - java

I am having error in servlet to pass the value from JSP..
JSP codes :
<table>
<tr>
<td>Day</td>
<td>Start</td>
<td>End</td>
<td>Date</td>
<td> </td>
</tr>
<tr>
<td><select name="availableDay">
<!--Listing days-->
</select></td>
<td><input type="time" name="availableStart"/></td>
<td><input type="time" name="availableEnd"/></td>
<td><input type="date" name="availableDate" /></td>
<td><input type="button" class="add" name="action" value="Add More"</td>
</tr>
</table>
I want to pass availableDate to servlet. Fyi, the row in JSP is dynamically generated. Thus, I pass using []. My servlet codes :
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
String[] presentationID = request.getParameterValues("selectavailability");
String[] availableDay = request.getParameterValues("availableDay");
String[] availableStart = request.getParameterValues("availableStart");
String[] availableEnd = request.getParameterValues("availableEnd");
String[] availableDate = request.getParameterValues("availableDate");
SimpleDateFormat availDate = new SimpleDateFormat("dd-MM-yyyy");
Date chosenDate = availDate.parse(availableDate);// THIS IS WHERE I AM GETTING ERROR
try {
if(availableDay != null && availableStart != null && availableEnd != null)
{
for (int i = 0; i < availableDay.length; i++)
{
AvailabilityBean available = new AvailabilityBean();
available.setLecturerID(request.getParameter("lecturerID"));
available.setAvailableDay(availableDay[i]);
available.setAvailableStart(availableStart[i]);
available.setAvailableEnd(availableEnd[i]);
available.setAvailableDate(availableDate[i]); //EFFECTED THIS LINE TOO
available = AddAvailableDAO.addavailable(available);
}
}
}
response.sendRedirect("addAvailability.jsp");
}
catch (Throwable theException) {
System.out.println("hhhhhhh"+theException);
}
}
For extra, AvailabilityBean:
private String availableID;
private String lecturerID;
private String availableDay;
private String availableStart;
private String availableEnd;
private Date availableDate;
private String presentationID;
Error : incompatible types: String[] cannot be converted to String.
Can you show me where is my mistake? And how can i solve this? Thank you

Here is your availableDate Variable which is an array of String.
String[] availableDate = request.getParameterValues("availableDate");
Now , you are using SimpleDateFormat Class to format your String data into Date.
Here parse(String text) will take a String Argument not a type of String[] (Array).
SimpleDateFormat availDate = new SimpleDateFormat("dd-MM-yyyy");
Date chosenDate = availDate.parse(availableDate);// availableDate is a String Array not a String.
So, It will raise an compile-time error incompatible types: String[] cannot be converted to String.
better you should try this
Date chosenDate = availDate.parse(availableDate[0]); //preferred index you may pass as per your requirement.
Note:- availableDate[0] will return a String Object available at index 0.

Here is Your JSP Code sample..
<td><select name="availableDay"> //only one value will get selected so. use getParameterValue() for this field too.
<!--Listing days-->
</select></td>
//this will return only a String not a String[] . So use getParameterValue();
<td><input type="time" name="availableStart"/></td>
//this will also return only a String not a String[] . So use getParameterValue();
<td><input type="time" name="availableEnd"/></td>
//this too will return only a String not a String[] . So use getParameterValue();
<td><input type="date" name="availableDate" /></td> //this too will return a String with getParameterValue();
So, try this .. Code.
String availableDay = request.getParameter("availableDay");
String availableStart = request.getParameter("availableStart");
String availableEnd = request.getParameter("availableEnd");
String availableDate = request.getParameter("availableDate");
instead of this one.
String[] availableDay = request.getParameterValues("availableDay");
String[] availableStart = request.getParameterValues("availableStart");
String[] availableEnd = request.getParameterValues("availableEnd");
String[] availableDate = request.getParameterValues("availableDate");

Related

How to target specific td in a HTML document with java

I want to target specific td inside a tr.
This is my code:
private void fletch(String name) throws IOException, JSONException {
final String iron = "img=2";
final String ui = "img=3";
final String hc = "img=10";
String url = "services.runescape.com/m=hiscore_oldschool/hiscorepersonal.ws?user1=";
if ( name.toLowerCase().indexOf(iron.toLowerCase()) != -1 ) {
url = "http://services.runescape.com/m=hiscore_oldschool_ironman/hiscorepersonal.ws?user1=";
}else if( name.toLowerCase().indexOf(ui.toLowerCase()) != -1 ){
url = "http://services.runescape.com/m=hiscore_oldschool_ultimate/hiscorepersonal.ws?user1=";
}else if( name.toLowerCase().indexOf(hc.toLowerCase()) != -1 ){
url = "http://services.runescape.com/m=hiscore_oldschool_hardcore_ironman/hiscorepersonal.ws?user1=";
}
String[] parts = name.split(">");
String part2 = parts[1];
String fin = part2.replaceAll("\\s","+");
url+=fin;
Document doc = Jsoup.connect(url)
.data("query", "Java")
.userAgent("Mozilla")
.cookie("auth", "token")
.timeout(3000)
.post();
//core part
Element table1 = doc.select("table").first();
String body = table1.toString();
Document docb = Jsoup.parseBodyFragment(body);
Element bbd = docb.body();
String hhk = bbd.toString();
//This is where i dont know how to target the td data.. Tried this (cant check code so came on here):
String overall = bbd.getElementsByTag("td").get(4).text();
Now this gives me this HTML code:
<table cellpadding="3" cellspacing="0" border=0 style="max-width: 355px;">
<tr><td colspan="5" align="center"><b>Personal scores for big kurwaaa</b></td></tr>
<tr>
<td colspan="2" style="text-align:left;padding-left:24px;"><b>Skill</b></td><td align="right"><b>Rank</b></td><td align="right"><b>Level</b></td><td align="right"><b>XP</b></td>
</tr>
<tr><td width="35"></td><td width="100"></td><td width="75"></td><td width="40"></td><td width="75"></td></tr>
<tr>
<td></td>
<td align="left"><a href="overall.ws?table=0&user=big+kurwaaa">
Overall
</a></td>
<td align="right">7,430</td>
<td align="right">466</td>
<td align="right">6,164,312</td>
</tr>
<tr>
<td align="right"><img class="miniimg" src="http://www.runescape.com/img/rsp777/hiscores/skill_icon_attack1.gif"></td>
<td align="left"><a href="overall.ws?table=1&user=big+kurwaaa">
Attack
</a></td>
<td align="right">14,475</td>
<td align="right">19</td>
<td align="right">4,304</td>
</tr>
I want to target the 3 td with data inside every tr. So for example:
<td align="right">7,430</td>
<td align="right">466</td>
<td align="right">6,164,312</td>
and so on from the "overall" tr to the last. Is there any way to do in a simple way that will give me the option to loop through the data and create a JSON/map?
Ps: new to java
If you want to get all the tr tags inside bbd use getElementsByTag.
It will return Elements, by which you can browse through all the tr tags by index (0 based index).If want to skip first 3 tr tags just start loop from index : 3, and so for td tags
Here is the demo code :
Elements trList = bbd.getElementsByTag("tr");
for (int i = 3; i < trList.size(); i++) {
System.out.println("----------------- TR START -----------------");
Elements tdList = trList.get(i).getElementsByTag("td");
for (int j = 2; j < tdList.size(); j++) {
System.out.println(tdList.get(j));
}
System.out.println("------------------ TR END ------------------");
}
String url = "yourUrl";
Document doc = Jsoup.connect(url).get();
Element table = doc.select("table[class=tableClass]").first();
Iterator<Element> iterator = table.select("td[align=right]").iterator();
iterator.next();//skip first
iterator.next();//skip second
System.out.println(iterator.next().text());

JSP form returns null values while inserting into database

I have this JSP form where i have some values inserted from the previous page.
<script language="javascript" type="text/javascript">
var i = 1;
function addRow() {
var tbl = document.getElementById('table1');
var lastRow = tbl.rows.length;
var iteration = lastRow - 1;
var row = tbl.insertRow(lastRow);
var firstCell = row.insertCell(0);
var el = document.createElement('input');
el.type = 'text';
el.name = 'name_' + i;
el.id = 'name_' + i;
el.size = 10;
el.maxlength = 10;
firstCell.appendChild(el);
// alert(i);
i++;
frm.h.value = i;
// alert(i);
}
</script>
<form action="CreateAssignments" method="post"
enctype="multipart/form-data">
<center>
<table id="table1">
<tr>
<td>
<p>Degree :</p>
</td>
<td><label><%=request.getParameter("degree")%> </label> <input type="hidden" name="degree"
value="<%=request.getParameter("degree")%>" /></td>
<td></td>
<td>
<p>Course :</p>
</td>
<td><label><%=request.getParameter("course")%> </label><input
type="hidden" name="course"
value="<%=request.getParameter("course")%>" /></td>
</tr>
<tr>
<td>
<p>Stream :</p>
</td>
<td><label><%=request.getParameter("stream")%> </label><input
type="hidden" name="stream"
value="<%=request.getParameter("stream")%>" /></td>
<td></td>
<td>
<p>Section :</p>
</td>
<td><label><%=request.getParameter("section")%> </label><input
type="hidden" name="section"
value="<%=request.getParameter("section")%>" /></td>
</tr>
<tr>
<td>
<p>Semester :</p>
</td>
<td><label><%=request.getParameter("semester")%></label><input
type="hidden" name="semester"
value="<%=request.getParameter("semester")%>" /></td>
<td></td>
<td>
<p>Paper Code :</p>
</td>
<td><label><%=request.getParameter("papercode")%></label><input
type="hidden" name="papercode"
value="<%=request.getParameter("papercode")%>" /></td>
</tr>
<tr>
<td><p>
<input type="button" value="Add Questions" onclick="addRow();" />
</p></td>
</tr>
<tr>
<td><p>Start Date:</p></td>
<td><p>
<input type="date" name="startdate" />
</p></td>
</tr>
<tr>
<td><p>End Date:</p></td>
<td><p>
<input type="date" name="enddate" />
</p></td>
</tr>
<tr>
<td><p>Assignment Name:</p></td>
<td><p>
<input type="text" name="assname" />
</p></td>
</tr>
<tr>
<td><p>Upload File</p></td>
<td><p>
<input type="file" name="fileupload" value="" />
</p></td>
</tr>
<tr>
<td><p></p></td>
<td></td>
<tr>
</table>
<p>
<input type="submit" value="Create" />
</p>
</center>
</form>
And this Java class to insert data into the database.
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
session = request.getSession(true);
String assignmentName = request.getParameter("assname");
String startDate = request.getParameter("startdate");
String endDate = request.getParameter("enddate");
String course = request.getParameter("course");
String degree = request.getParameter("degree");
System.out.println("degree: "+degree);
String semester = request.getParameter("semester");
String paperCode = request.getParameter("papercode");
String stream = request.getParameter("stream");
String fileName = uploadFile(request, response);
String question1=request.getParameter("name_1");
String question2=request.getParameter("name_2");
String question3=request.getParameter("name_3");
String question4=request.getParameter("name_4");
String question5=request.getParameter("name_5");
String question6=request.getParameter("name_6");
System.out.println("FILE NAME : " + fileName);
try {
PreparedStatement ps = connection.prepareStatement("insert into assignment_master values (?,?,?,?,?,?,?,?,?,?)");
ps.setString(1, "2");
ps.setString(2, assignmentName);
ps.setString(3, degree);
ps.setString(4, course);
ps.setString(5, stream);
ps.setString(6, semester);
ps.setString(7, (String) session.getAttribute("employeeId"));
ps.setString(8, fileName);
ps.setString(9, startDate);
ps.setString(10, endDate);
ps.executeUpdate();
PreparedStatement ps1 = connection
.prepareStatement("select max(assignment_id) assignmentId from assignment_master");
ResultSet rs = ps1.executeQuery();
PreparedStatement ps2 = connection.prepareStatement("insert into assignment_subject(?,?)");
ps2.setString(1, rs.getString("assignmentId"));
ps2.setString(2, paperCode);
ps2.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private String uploadFile(HttpServletRequest request, HttpServletResponse response) {
response.setContentType("text/html");
// PrintWriter out = response.getWriter();
// out.println("<html>");
// out.println("<body>");
String value = "";
FileItemStream fileItemStream = null;
boolean isMultiPart = ServletFileUpload.isMultipartContent(request);
if (isMultiPart) {
// out.println("Yes Multipart data");
ServletFileUpload upload = new ServletFileUpload();
try {
FileItemIterator itr = upload.getItemIterator(request);
while (itr.hasNext()) {
fileItemStream = itr.next();
if (fileItemStream.isFormField()) {
// do field related work
String fieldName = fileItemStream.getFieldName();
InputStream is = fileItemStream.openStream();
byte[] b = new byte[is.available()];
is.read(b);
value = new String(b);
// out.println(fieldName + " : " + value);
} else {
String path = getServletContext().getRealPath("/");
System.out.println("PATH : " + path);
if (FileUpload.processFile(path, fileItemStream, value)) {
System.out.println("File Uploaded Successfully");
// request.setAttribute("imagePath", path
// + fileItemStream.getName());
// RequestDispatcher gotoShowPage = request
// .getRequestDispatcher("showPage.jsp");
// gotoShowPage.forward(request, response);
} else {
// out.println("File Uploading failed ");
System.out.println("File Uploading failed ");
}
}
}
} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
}
} else {
// out.println("No Multipart data");
}
// out.println("</body>");
// out.println("</html>");
return fileItemStream.getName();
}
}
But when i submit this form and try to insert data, I get an error saying primary key cannot be null and when i debug i realise that actually all the fields from the form returns null values. And i also want to know to acesss data from the dynamic textboxes and insert them.
Thank you.
Your insert statement is incorrect.
insert into assignment_master values (?,?,?,?,?,?,?,?,?,?)
You have to name columns like these:
insert into assignment_master(assignment_id, assignment_name, degree, course, stream, semester, employee_id, file_name, start_date, end_date) values (?,?,?,?,?,?,?,?,?,?)
Same is true in second insert stament:
insert into assignment_subject(?,?)
has to have column names specified.
If you do not specify column names than all columns will be assigned their default values. See [3] for MySQL.
Assuming that you are using MySQL, see INSERT syntax: [1]
For PostgreSQL see [2].
[1] http://dev.mysql.com/doc/refman/5.7/en/insert.html
[2] https://www.postgresql.org/docs/current/static/sql-insert.html
[3] http://dev.mysql.com/doc/refman/5.7/en/insert.html#idm139777556167776
Whenever you upload the form data using Multipart, the request object is converted into a stream, stream of data to be specific.
In such a scenario, you do not get the fields as parameters. They come along with the file data. The code shown below - already present in your code shown handles this:
if (fileItemStream.isFormField()) {
// do field related work
String fieldName = fileItemStream.getFieldName();
InputStream is = fileItemStream.openStream();
byte[] b = new byte[is.available()];
is.read(b);
value = new String(b);
// out.println(fieldName + " : " + value);
}
You can modify this code to create a hashmap of parameters that are present in your form. The code can be like:
HashMap<String,String> myvaluemap = new HashMap();
if (fileItemStream.isFormField()) {
String fieldName = fileItemStream.getFieldName();
InputStream is = fileItemStream.openStream();
byte[] b = new byte[is.available()];
is.read(b);
value = new String(b);
myvaluemap.put(name,value);
}
I have it tested at my end. Let me know if still any further help is required.
For your reference, you can refer to:
this link. in the processing uploaded items section. It is not the exact thing, but provides the hint

html table td contents parsing using jsoup in android

I've with me some html table contents.And for my application I want to parse these html contents using JSOUP parsing in android.But I am new to this JSOUP method and I can't parse those html contents properly.
HTML data:
<table id="box-table-a" summary="Tracking Result">
<thead>
<tr>
<th width="20%">AWB / Ref. No.</th>
<th width="30%">Status</th>
<th width="30%">Date Time</th>
<th width="20%">Location</th>
</tr>
</thead>
<tbody>
<tr>
<td width="20%" nowrap="nowrap" class="click">Z45681583</td>
<td width="30%" nowrap="nowrap" class="click">
IN TRANSIT<div id='ntfylink' style='display:block; text-decoration:blink'><a href='#' class='topopup' name='modal' style='text-decoration:none'><font face='Verdana' color='#DF0000'><blink>Notify Me</blink></font></a></div>
</td>
<td width="30%">
Sat, Jan, 31, 2015 07:09 PM
</td>
<td width="20%">DELHI</td>
</tr>
</tbody>
</table>
from this table I need the"td" contents.
Any help would be greatly appreciated.
Everything is described clearly in source code below.
private static String test(String htmlFile) {
File input = null;
Document doc = null;
Elements tdEles = null;
Element table = null;
String tdContents = "";
try {
input = new File(htmlFile);
doc = Jsoup.parse(input, "ASCII", "");
doc.outputSettings().charset("ASCII");
doc.outputSettings().escapeMode(EscapeMode.base);
/** Get table with id = box-table-a **/
table = doc.getElementById("box-table-a");
if (table != null) {
/** Get td tag elements **/
tdEles = table.getElementsByTag("td");
/** Loop each of the td element and get the content by ownText() **/
if (tdEles != null && tdEles.size() > 0) {
for (Element e: tdEles) {
String ownText = e.ownText();
//Delimiter as "||"
if (ownText != null && ownText.length() > 0)
tdContents += ownText + "||";
}
if (tdContents.length() > 0) {
tdContents = tdContents.substring(0, tdContents.length() - 2);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return tdContents;
}
You can manipulate the String in your textview. All the TD contents is delimited by ||. Use String.split() to get each content if you want.
String[] data = tdContents.split("\\|\\|");

JSON.stringify(array) from jsp to servlet (loop using javax.json-api1.0. jar or javax.json-1.0.jar only)

HTML :
<table>
<tr>
<td>
<input type="hidden" value="flag1" />
</td>
<td>
<input type="text" value="orange" />
</td>
<td>
<input type="text" value="1.00" />
</td>
<td>
<input type="text" value="5" />
</td>
</tr>
<tr>
<td>
<input type="hidden" value="flag2" />
</td>
<td>
<input type="text" value="apple" />
</td>
<td>
<input type="text" value="2.00" />
</td>
<td>
<input type="text" value="5" />
</td>
</tr>
</table>
JS :
var array = $.map($('table tr'), function (val, i) {
var obj = {}, inputs = $(val).find('td input:not(:hidden)');
obj[inputs.filter(':first').val()] = $.map(inputs.not(':first'), function (val, i) {
return val.value;
});
return obj;
});
alert(JSON.stringify(array));
$(document).on("click","#save",function(){
$.post("servlet.html","data="+JSON.stringify(array)+"",function(response){
});
});
Java servlet contains this :
String data = request.getParameter("data");
data looks like this :
[{"flag1":["orange","1.00","5"]},{"flag2":["apple","2.00","5"]}]//this is get from table row data using stringify
i want to get on first loop using javax.json-api1.0.jar or javax.json-1.0.jar only :
on first loop :
flag1
Orange
1.00
5
on second loop :
flag2
Apple
2.00
5
Any help will be best.
You can do it with only json-api, but you must be sure of the structure of the input data.
If not, gson or jackson2 are easier to use.
If you want to use json-api, you could do something like :
First declare a JsonFactoryReader attribute in your servlet because you will create a reader per request.
JsonReaderFactory readerFactory = Json.createReaderFactory(null);
then in your service or doXXX method do :
String data = request.getParameter("data");
// create a reader for json data
JsonReader jr = readerFactory.createReader(new StringReader(data));
// tol level must be an array, and we count the iteration for eventual error messages
JsonArray ja = jr.readArray();
int iteration = 0;
for (JsonValue jv: ja) {
// sub elements must be dicts
if (jv.getValueType() != ValueType.OBJECT) {
throw new FormatException(iteration, jv);
}
for (Entry<String, JsonValue> e: ((JsonObject) jv).entrySet()) {
// the key
String key = e.getKey();
if (e.getValue().getValueType() != ValueType.ARRAY) {
throw new FormatException(iteration, e.getValue());
}
// the values, first is a string
JsonArray array = (JsonArray) e.getValue();
for (int i=0; i<array.size(); i++) {
System.out.println(array.get(0).getValueType());
}
String name = array.getString(0);
// next a float and an int
float fval;
int ival;
try {
fval = Float.valueOf(array.getString(1));
ival = Integer.valueOf(array.getString(2));
}
catch (NumberFormatException ex) {
throw new FormatException(iteration, array);
}
// Do your stuff with those values ...
// for instance Data data = new Data(key, name, fval, ival) ...
iteration++;
}
}
I propose you an exception class to handle format errors (at least you know why it breaked ...):
class FormatException extends ServletException {
FormatException(int i, JsonValue jv) {
super("Iteration " + String.valueOf(i) + " : " + jv.toString());
}
}

get and send data in String[] Array using servlet

int i = 0;
String[] pnumbers = new String[3];
String[] pqtys = new String[3];
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String pnumber = request.getParameter("pnumber");
DAO dao = new DAO();
Product p = dao.checkProduct(pnumber);
String qunty = request.getParameter("pqty");
while (i < 3) {
pnumbers[i] = p.getNumber();
pqtys[i] = p.getQty();
i++;
}
request.setAttribute("pnum" pnumbers);
}
I need to add 3 items coming by webpage in to this array. but ones you can add one item
<td>
<input type="text" name="pnumber" value="" />
</td>
<td>
<input type="text" name="pqty" value="" />
</td>
i need to add only 3 items to array after adding those 3 i need to get that values again to same page please help?
Once you set attribute in you Servlet like you have done.
request.setAttribute("pnum" pnumbers);
Forward control from Servlet to required JSP and You can access it in your JSP using
request.getAttribute(paramName)
Hint:
if(request.getAttribute("pnum")!=null){
String []strArray = (String []) request.getAttribute("pnum");
for(int i=0;i<strArray.length;i++){
out.println(strArray[i]);
}
}
Print the values at your required point.

Categories