I have this code:
out.println("<form action=\"srvSearchResults\" method=\"post\">");
out.println("<tr>");
Book b = freeBooks.get(i);
out.println("<td><input type='submit' data-isbn=\""+b.getIsbn()+"\" name=\"ddd\" value=\""+b.getTitle()+"\" style=\"padding:0; border:none; background:none; cursor:pointer\" ></td>");
out.println("<td>" + b.getIsbn() + "</td>");
out.println("<td>" + b.getAuthorName() + "</td>");
out.println("<td>" + b.getPublishDate() + "</td>");
out.println("<td>" + "Free to borrow!" + "</td>");
out.println("</tr>");
out.println("</form>");
When I click on submit in generated code the servlet function is fired and by this row:
String isbn = request.getParameter("ddd");
I try to get value of data-isbn but it's null.
Any idea how can I get data-isbn attribute on server side afer submit?
When you submit an HTML form, only the values of the form fields are sent to the server, not all of the attributes. So the way it is written now, there is no way to get the value, because your server never received it.
If you want to get your ISBN, you can add an additional hidden input field that contains it:
out.println("<input type='hidden' name='isbn' value='" + b.getIsbn() + "'>");
You can put this right after the <form> open tag.
If you submit your form you can then use
String isbn = request.getParameter("isbn");
to get the ISBN.
As a side note: <form> is an invalid parent for <tr> elements. Therefor it is possible that the form doesn't behave the way you intend it to.
Related
I'm trying to get data from html in order from a web. Html code looks like:
<div class="text">
First Text
<br>
<br>
<div style="margin:20px; margin-top:5px; ">
<table cellpadding="5">
<tbody><tr>
<td class="alt2">
<div>
Written by <b>excedent</b>
</div>
<div style="font-style:italic">quote message</div>
</td>
</tr>
</tbody></table>
</div>Second Text<br>
<br>
<img class="img" src="https://developer.android.com/_static/images/android/touchicon-180.png"><br>
<br>
Third Text
</div>
What I want to do is create an Android layout scraping html, but I need to preserve the order of the elements. In this case:
TextView => First Text
TextView => Quote Message
TextView => Second Text
ImageView => img
TextView => Third Text
The problem comes when I try to get html values in order, using JSoup I get a String with "First Text Second Text Third Text" with Element.ownText, an then img at the end, resulting:
TextView => First Text Second Text Third Text
TextView => Quote Message
ImageView => img
What can I do to get that data in order?
Thanks in advance
You can parse the html into a list of html nodes. The list of nodes will preserve the DOM order and give what you want.
Check the parseFragment method :
This method will give you a list of nodes.
Try this.
String html = ""
+ "<div class=\"text\">"
+ " First Text"
+ " <br>"
+ " <br>"
+ " <div style=\"margin:20px; margin-top:5px; \">"
+ " <table cellpadding=\"5\">"
+ " <tbody><tr>"
+ " <td class=\"alt2\">"
+ " <div>"
+ " Written by <b>excedent</b>"
+ " </div>"
+ " <div style=\"font-style:italic\">quote message</div>"
+ " </td>"
+ " </tr></tbody>"
+ " </table>"
+ " </div>Second Text<br>"
+ " <br>"
+ " <img class=\"img\" src=\"https://developer.android.com/_static/images/android/touchicon-180.png\"><br>"
+ " <br>"
+ " Third Text"
+ " </div>";
Document doc = Jsoup.parse(html);
List<String> rootTexts = doc.select("div.text").first().textNodes().stream()
.map(node -> node.text().trim())
.filter(s -> !s.isEmpty())
.collect(Collectors.toList());
System.out.println(rootTexts);
OUTPUT:
[First Text, Second Text, Third Text]
This answer is a little late, but the correct way to do what you want to do is this. For your outermost <div>, instead of getting the child elements using Element.children(), you'll want to use Element.childNodes() instead.
Element.children() only returns child Elements, in which text is not included.
Element.childNodes() returns all child nodes, which includes TextNodes and Elements.
This solution works for me.
Im having trouble showing what I put in my ArrayList on JSP. The items in the list are taken from a Database in a Java class and then are shown in the JSP. Ive tried using the c:forEach but I still dont get it that much. Im new to JSP and Im trying not to use the scriplets. Thats why I do everything over my Java class. Also so that you got an idea on how it works, the admin chooses to see the whole database of the users, so it chooses on a option radio button tag, goes to a JSP page where it evaluates what the admin wants to see and forwards to the other page where the Arraylist must be shown. I call the function of the List in the JSP page that forwards to the JSP that shows the Database. I dont know if thats the correct way. Thanks!
This is the JAVA class
public List<administration> fullList() throws Exception{
List<administration> result = new ArrayList<administration>();
try{
Class.forName("com.mysql.jdbc.Driver");
connectMe = DriverManager.getConnection(url+dbName, userNameDB, passwordDB);
String query = "SELECT \n" +
" ControlAccess.UserName, \n" +
" ControlAccess.Pass, \n" +
" Users.First_Name,\n" +
" Users.Last_Name, \n" +
" UserInfo.Age, \n" +
" UserInfo.Country,\n" +
" UserInfo.Address,\n" +
" UserInfo.ZipCode,\n" +
" Sessions.Matrix1,\n" +
" Sessions.Matrix2,\n" +
" Sessions.Result,\n" +
" FilePath.LocationFiles\n" +
" FROM MatrixUsers.UserInfo \n" +
" INNER JOIN MatrixUsers.Users\n" +
" ON UserInfo.idUserInfo = Users.idUsers\n" +
" INNER JOIN MatrixUsers.ControlAccess \n" +
" ON ControlAccess.idControlAccess = UserInfo.idUserInfo\n" +
" INNER JOIN MatrixUsers.Sessions \n" +
" ON Sessions.idSessions = ControlAccess.idControlAccess\n" +
" INNER JOIN MatrixUsers.FilePath \n" +
" ON FilePath.idFilePath = Sessions.idSessions";
selectUsers = connectMe.prepareStatement(query);
selectUsers.executeQuery();
while(results.next()) {
administration admin = new administration();
admin.setUserName(results.getString("UserName"));
admin.setPassword(results.getString("Pass"));
admin.setFirstname(results.getString("First_Name"));
admin.setLastname(results.getString("Last_Name"));
admin.setAge(results.getInt("Age"));
admin.setCountry(results.getString("Country"));
admin.setAddress(results.getString("Address"));
admin.setZipcode(results.getInt("ZipCode"));
admin.setMatrix1(results.getString("Matrix1"));
admin.setMatrix2(results.getString("Matrix2"));
admin.setResult(results.getString("Result"));
admin.setLocation(results.getString("LocationFiles"));
result.add(admin);
}
results.close();
connectMe.close();
}catch(Exception e) {
e.printStackTrace();
}
return result;
}
This is how Im trying to show in the JSP:
<table>
<c:forEach var="admin" items="${admin.fullList()}">
<tr>
<td>${admin.username}" </td>
<td>${admin.password} </td>
<td>${admin.firstName} </td>
<td>${admin.lastName} </td>
<td>${admin.age} </td>
<td>${admin.country} </td>
<td>${admin.address} </td>
<td>${admin.zipcode} </td>
<td>${admin.Matrix1} </td>
<td>${admin.Matrix2} </td>
<td>${admin.Result} </td>
<td>${admin.location} </td>
</tr>
</c:forEach>
</table>
plz try like this
<c:if test="${not empty result}">
<c:forEach var="e" items="${result}" >
<c:out value="${e.username}"/>
</c:forEach>
</c:if>
here i am iterating the list and the username by using c:out.
I am new in Java and JQuery. I am using hidden field to hide my value but using Browser Inspector others can find my data. how can I hide values in Browser.
out.println("<form method='post' action='test.jsp'>");
out.println("<input type=hidden name=test1 value=" + test1 + " />");
out.println("<input type=hidden name=test2 value=" + test2 + " />");
out.println("<input type=hidden name=test3 value=" + test3 + " />");
out.println("<input type=hidden name=test4 value=" + test4 + " />");
out.println("<input type=submit value=Launch />");
out.println("</form>");
html renders the data on users browser, html standards offers you to hide data by only using hidden form fields, the initial purpose of hidden form field to allow session tracking.
with hidden form field user can view the content when he views the source,
if you want to hide data i suggest to use some kind of encryption.
I have an problem which in turn is causing a lot of headaches. I need to dynamically create buttons/images which link to JSF actionListener. Here is the code:
HTML:
<h:form>
<div class="carousel-container">
<div id="carousel">
<h:outputText value="#{courseBean.course}" escape="false"/>
</div>
</div>
</h:form>
what courseBean.course gets is the Overriden toString which returns the following:
#Override
public String toString() {
return "<div class=\"carousel-feature\"> "
+ "<h:commandLink id=\"" + courseID + "\" actionListener=\"#{courseBean.getCourseSelected}\">"
+ "<img class=\"carousel-image\" src=\"Images/testButton.jpg\"/>"
+ "<span style=\"display:bloack; position:absolute; top:20px; bottom:20px; left:0; right:0; "
+ "background:white; background:rgba(255, 255, 255, 0.25);\">" + courseName + "</span>"
+ "</h:commandLink> "
+ "<div class=\"carousel-caption\"> "
+ "</div>"
+ "</div>";
}//end method toString
The HTML is being rendered fine and image is being displayed in the carousel however when it is clicked actionListener is not being called which is the issue here.
edit: the actionListener only prints the courseID to the console nothing major.
Thank you for taking your time :)
That approach is wrong, if you do "view source" you will see the <h:commandLink in the source of your page (because it wont be processed by JSF life cycle at all) , while if you had a <h:commandLink in your xhtml page the generated html source will contain a <a href....> element
You better rethink of your original goal and ask a question on how to achieve it...
I have a gallery of images, and when I hover the mouse over any of the images, the image pulls back revealing some text.
When I have the following HTML code (at bottom of post) inside the HTML document, everything works fine.
However, when I put the exact same HTML code inside a Java servlet and have it returned to the page, everything looks normal, but the image pullback doesn't work anymore.
Any idea why that would occur? Perhaps I need to do some kind of refresh to make it work properly?
relevant code for one of the items in the gallery:
<li>
<div class="header"><p>Product 1 Shirt</p></div>
<div class="gallery_item">
<img src="gallery/thumb/gallery_01.jpg" width="214" height="194" class="cover" alt="" />
<p>Highlight 1</p>
<p>Highlight 2</p>
<p>Highlight 3</p>
More Info
Enlarge
</div>
<div class="p2"><p>Price: $10</p></div>
<div class="p2"><p>In Stock: Yes</p></div>
</li>
As requested: the servlet:
public void service(HttpServletRequest request,
HttpServletResponse response) throws IOException,
ServletException
{
PrintWriter out = response.getWriter();
response.setContentType("text/html");
String requestType = request.getParameter("type");
String result;
if(requestType.equals("getproductlist"))
{
Products products = Products.getProductsInstance();
String keywords = request.getParameter(("keywords"));
String organization = request.getParameter(("organization"));
String price = request.getParameter(("price"));
String sort = request.getParameter(("sort"));
result = products.getProducts(keywords, organization, price, sort);
//this next lines of html are actually what is returned from products.getProducts. I'm just putting it here for clarity. All the variables (name, h1, etc) are okay.
result += "<li>"
+ "<div class=\"header\"><p>"+ name +"</p></div>"
+ "<div class=\"gallery_item\">"
+ "<img src=\"gallery/thumb/gallery_01.jpg\" width=\"214\" height=\"194\" class=\"cover\" alt=\"\" />"
+ "<p>"+ h1 +"</p>"
+ "<p>"+ h2 +"</p>"
+ "<p>"+ h3 +"</p>"
+ "More Info"
+ "Enlarge "
+ ""
+ "</div>"
+ "<div class=\"p2\"><p>Price: "+ itemPrice +"</p></div>"
+ "<div class=\"p2\"><p>In Stock: "+ inStock +"</p></div> "
+ "</li>";
out.println(result);
out.close();
}