I am trying to call a HashMap value in jsp page but got error there and error says
There was an unexpected error (type=Internal Server Error, status=500).
For input string: "res"
My code;
<c:forEach items="${onlineExamList}" var="item"
varStatus="loop">
<div>
<b>${item.question1}</b><br>
<div class="radio">
<label><input type="radio" value="a"
name="answers[${loop.index}]">${item.option1}</label>
</div>
<div class="radio">
<label><input type="radio" value="b"
name="answers[${loop.index}]">${item.option2}</label>
</div>
<div class="radio">
<label><input type="radio" value="c"
name="answers[${loop.index}]">${item.option3}</label>
</div>
<div class="radio">
<label><input type="radio" value="d"
name="answers[${loop.index}]">${item.option4}</label>
</div>
<input type="text" name="rightAnswer"
value="${item.rightAnswer}">
//problem in this line
<c:if test="${result != null}">
<br>
<br>
<b>Your answer: ${result.get("res"+loop.index).get(1)}</b>
<br>
</c:if>
</div>
<hr />
</c:forEach>
And this is how i have set hashmap from controller
Map<String, List<String>> mapResult = new HashMap<String, List<String>>();
int totalScore = 0;
for (int i = 0; i < answers.answers.size(); i++) {
List<String> result = new ArrayList<>();
String res = "Wrong";
if (answers.answers.get(i).equals(answers.rightAnswer.get(i))) {
res = "Correct";
totalScore+=10;
}
result.add(res);
result.add(answers.answers.get(i));
result.add(answers.rightAnswer.get(i));
mapResult.put("res" + i, result);
}
ra.addFlashAttribute("result", mapResult);
ra.addFlashAttribute("score", totalScore);
Same things printed on java page
for (int i = 0; i < 10; i++) {
System.out.println(mapResult.get("res" + i).get(0));
System.out.println(mapResult.get("res" + i).get(1));
System.out.println(mapResult.get("res" + i).get(2));
System.out.println("...................................");;
}
How can I print hashmap value in jsp page?
Please check try this.
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:forEach items="${currentLoggedInUsersMap}" var="entry">
Key = ${entry.key}, value = ${entry.value}<br>
</c:forEach>
I solve the problem by concating the string using concat like this
${result.get("res".concat(loop.index)).get(1)}
The mapResult object is a map containing string arrays
Hashmap<String, List<String>>
you may need to first get the Array from the map object and then iterate over it.
this may help?
<table>
<c:forEach var="results" items="${resultsMap['res' + loop.index]}" varStatus="loop">
<tr>
<c:forEach var="answer" items="${results}" varStatus="status">
<td>
<td>$answer</td>
</td>
</c:forEach>
</tr>
</c:forEach>
</table>
Related
I am trying to make a basic shop-billing JSP project. now when i type random product quantity with checkbox it giving error like below image. but when i checked-value with serial wise, it is not giving the error, it giving me the result what i desiring. I have also check it is empty or not. but can't able to get out of this error. how can i get rid out of it?
Code:
<%--
Document : order
Created on : Jun 15, 2019, 3:11:06 PM
Author : Riddhi
--%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#page import="java.sql.*" %>
<!DOCTYPE html>
<html lang="en">
<%# include file="header.jsp" %>
<h2 class="text-center"> ShopBilling </h2>
<p><br/></p>
<%
String Host = "jdbc:mysql://localhost:3306/shopbilling";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
PreparedStatement ps=null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(Host, "root", "");
StringBuilder sb = new StringBuilder();
%>
<div class="row justify-content-center">
<div class="col-md-6">
<div class="row">
<div class="col-md-6"><h3>Product Details</h3></div>
<div class="col-md-6 text-right">
Back to Home
</div>
</div>
<p></p>
<form action="" method="post">
<%
String products[] = request.getParameterValues("products");
String items[] = request.getParameterValues("item_no");
int sum=0;
if (products!= null && items!= null && products.length != 0 && items.length != 0) {
for (int i = 0; i < products.length; i++) {
%> <%
statement = connection.createStatement();
String u=request.getParameter("u");
int num=Integer.parseInt(products[i]);
String Data = "select * from products_tbl where id='"+num+"'";
rs = statement.executeQuery(Data);
while (rs.next()) {
%>
<input type="hidden" name="id" value='<%=rs.getString("id")%>'/>
<div class="form-group">
<label for="product_name">Product Name: <%=rs.getString("product_name")%></label>
<input type="hidden" class="form-control" id="product_name" name="product_name" value='<%=rs.getString("product_name")%>'>
</div>
<div class="form-group">
<label for="product_price">Product Single Price: <%=rs.getString("product_price")%></label>
<input type="hidden" class="form-control" id="product_price" name="product_price" value='<%=rs.getString("product_price")%>'>
</div>
<div class="form-group">
<label for="product_name">Item Quantity: <% out.println(items[i]); %></label>
<input type="hidden" class="form-control" id="item_no" name="item_no" value='items[j]'>
</div>
<div class="form-group">
<label for="product_name">Product Total Price: <%
int num1=Integer.parseInt(items[i]);
int propri=Integer.parseInt(rs.getString("product_price"));
out.println(num1 * propri); int gtotal= num1 * propri; %></label>
<input type="hidden" class="form-control" id="item_no" name="item_no" value='items[j]'>
</div>
<hr>
<% sum= sum + gtotal; %>
<%
}
}
}
%>
<div class="form-group">
<label for="product_name">Grand Total: <%
out.println(sum); %></label>
<input type="hidden" class="form-control" id="item_no" name="item_no" value='items[j]'>
</div>
<button type="print" onclick="window.print();" class="btn btn-warning">Print</button>
</form>
</div>
</div>
<%# include file="footer.jsp" %>
</html>
I think the problem in your code is with <input type="text" name="item_no" /> because every row in your above code has checkbox , when you submit your form only selected checkbox is passed , but all <input type="text" name="item_no" /> is passed even the null where you didn't give any value , so when you iterate you get null value with that as well .Now ,to solve this do like below :
//give value to your check-box i.e id of that row
<input type="checkbox" name="products" value ='<%=rs.getString("id")%>' />
//pass that id with your input type i.e item_no_1 ..etc
<input type="text" name='item_no_<%=rs.getString("id")%>' />
And then to get only particular value of Product Quantity where you have selected Checkbox do like below :
String products[] = request.getParameterValues("products");
if (products!= null && items!= null && products.length != 0 && items.length != 0) {
for (int i = 0; i < products.length; i++) {
//your code
<div class="form-group">
<label for="product_name">Product Total Price: <%
//getting item value of that selected checkbox
int item_no=Integer.parseInt(request.getParameter("item_no_" + products[i]));
int propri=Integer.parseInt(rs.getString("product_price"));
out.println(num1 * propri); int gtotal= num1 * propri; %></label>
<input type="hidden" class="form-control" id="item_no" name="item_no" value='items[j]'>
</div>
when i search specific word only first page is classified. it shows pages and posts well on first page.
but when i go to page 2 or next page, seaching keyword doesn't apply on
is this address problem?
i guess this is sql or Paging.java problem because when i print log of page at BDAO it shows page well which i clicked.
also I don't know how can i transfer keyWord &keyField for that..!
I use oracle DB.
<%
String keyWord = (String)request.getParameter("keyWord");
String keyField = (String)request.getParameter("keyField");
%>
<script>
function searchCheck(frm){
//검색
if(frm.keyWord.value ==""){
alert("검색 단어를 입력하세요.");
frm.keyWord.focus();
return;
}
frm.submit();
}
function PageMove(page){
var keyWord = '<%=keyWord%>'
var keyField = '<%=keyField%>'
console.log(keyWord);
if(keyWord !=''){
location.href = "list.do?page="+page+"&keyWord=" + keyWord + "&keyField=" + keyField;
}
location.href = "list.do?page="+page;
}
</script>
</head>
<body>
<table width="800" cellpadding="0" cellspacing="0" border="1">
<tr>
<td>번호</td>
<td>이름</td>
<td>제목</td>
<td>날짜</td>
<td>히트</td>
</tr>
<c:forEach items="${list}" var="dto">
<tr>
<td>${dto.bId}</td>
<td>${dto.bName}</td>
<td>
<c:forEach begin="1" end="${dto.bIndent}">-</c:forEach>
${dto.bTitle}</td>
<td>${dto.bDate}</td>
<td>${dto.bHit}</td>
</tr>
</c:forEach>
<tr>
<td colspan="5">
<form action="list.do" method="post" name="search">
<select name="keyField">
<option value="bTitle">글 제목</option>
<option value="bContent">글 내용</option>
<option value="bName">작성자</option>
</select>
<input type="text" name="keyWord">
<input type="button" value="검색" onclick="searchCheck(form)">
</form>
</td>
</tr>
<tr>
<td colspan="5"> 글작성 </td>
</tr>
</table>
<div class="toolbar-bottom">
<div class="toolbar mt-lg">
<div class="sorter">
<ul class="pagination">
<li>맨앞으로</li>
<li>앞으로</li>
<c:forEach var="i" begin="${paging.startPageNo}" end="${paging.endPageNo}" step="1">
<c:choose>
<c:when test="${i eq paging.pageNo}">
<li class="active">${i}</li>
</c:when>
<c:otherwise>
<li>${i}</li>
</c:otherwise>
</c:choose>
</c:forEach>
<li>뒤로</li>
<li>맨뒤로</li>
</ul>
</div>
</div>
</div>
You never seem to be passing the keyword or keyfield when you call pageMove(). You might as well look up their values inside the function instead of having them as parameters:
function PageMove(page){
var keyWord = document.getElementById("keyWord").value;
var keyField = document.getElementById("keyField").value;
location.href = "list.do?page=" + page + "&keyWord=" + keyWord + "&keyField=" + keyField;
}
I'm trying to extract the e-mail adress and the phone number from a linkedin profile using jsoup, each of these informations is in a table. I have written a code to extract them but it doesn't work, the code should work on any linkedin profile. Any help or guidance would be much appreciated.
public static void main(String[] args) {
try {
String url = "https://fr.linkedin.com/";
// fetch the document over HTTP
Document doc = Jsoup.connect(url).get();
// get the page title
String title = doc.title();
System.out.println("Nom & Prénom: " + title);
// first method
Elements table = doc.select("div[class=more-info defer-load]").select("table");
Iterator < Element > iterator = table.select("ul li a").iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next().text());
}
// second method
for (Element tablee: doc.select("div[class=more-info defer-load]").select("table")) {
for (Element row: tablee.select("tr")) {
Elements tds = row.select("td");
if (tds.size() > 0) {
System.out.println(tds.get(0).text() + ":" + tds.get(1).text());
}
}
}
}
}
here is an example of the html code that i'm trying to extract (taken from a linkedin profile)
<table summary="Coordonnées en ligne">
<tr>
<th>E-mail</th>
<td>
<div id="email">
<div id="email-view">
<ul>
<li>
adam1adam#gmail.com
</li>
</ul>
</div>
</div>
</td>
</tr>
<tr class="no-contact-info-data">
<th>Messagerie instantanée</th>
<td>
<div id="im" class="editable-item">
</div>
</td>
</tr>
<tr class="address-book">
<th>Carnet d’adresses</th>
<td>
<span class="address-book">
<a title="Une nouvelle fenêtre s’ouvrira" class="address-book-edit" href="/editContact?editContact=&contactMemberID=368674763">Ajouter</a> des coordonnées.
</span>
</td>
</tr>
</table>
<table summary="Coordonnées">
<tr>
<th>Téléphone</th>
<td>
<div id="phone" class="editable-item">
<div id="phone-view">
<ul>
<li>0021653191431 (Mobile)</li>
</ul>
</div>
</div>
</td>
</tr>
<tr class="no-contact-info-data">
<th>Adresse</th>
<td>
<div id="address" class="editable-item">
<div id="address-view">
<ul>
</ul>
</div>
</div>
</td>
</tr>
</table>
To scrape email and phone number, use css selectors to target the element identifiers.
String email = doc.select("div#email-view > ul > li > a").attr("href");
System.out.println(email);
String phone = doc.select("div#phone-view > ul > li").text();
System.out.println(phone);
See CSS Selectors for more information.
Output
mailto:adam1adam#gmail.com
0021653191431 (Mobile)
I have a dropdown which consist the language names. I am setting the value and displaying name of the dropdown by using a hashmap.
<form action="TextTranslation" method="post" class="form" role="form" >
<div class="row">
<div id = "imageView" class="col-lg-8 center-block ">
<div class="btn-group">
<select name="country">
<%
Map<String,String> langCode = x.getCountryList();
for( Object key :langCode.keySet() )
{%>
<option value="<%=(String)key%>"><%=langCode.get(key) %> </option>
<%
System.out.println((String)key);
}
String name = request.getParameter("country");
request.setAttribute("code", name);
%>
</select>
</div>
<input type="submit" class= "btn btn-image" value="Translate">
Search Text
</div>
</div>
</form>
Values are passed correctly to dropbox as it print all the values in console. the set attribute is accessed in the particular servlet. But it gives a null value. Do you have any idea?Thank you in advance
UPDATED
<select name="country">
<%
Map<String,String> langCode = x.getCountryList();
for( Object key :langCode.keySet() )
{%>
<option value="<%=(String)key%>"><%=langCode.get(key) %> /option>
<% System.out.println((String)key);
}
String name = request.getParameter("country");
%>
</select>
<input type="hidden" name="code" value = <%= name%>/> .
In the servlet I used,
request.getParameter("code");
update your jsp likewise,
<form...>
...
<input type="hidden" name="code" value = <%= name%>/>
....
</form>
then get it from your servlet likewise,
request.getParameter("code"); // will return value of code
NOTE :
Remove from your jsp-code if above solution you gonna implement then,
request.setAttribute("code", name);
js file:
why does the size of dropdown1 is coming as 1?
function savethechanges(){
for(var i=0;i<count;i++)
{
dropdown1[i]=document.getElementById("sel"+i).value;
kot[i]=document.getElementById("kot"+i).value;
item[i]=document.getElementById("itemcode"+i).value;
if(dropdown1[i] == 0){
document.detailsview.action="BillCB.jsp?method=" + "11" + "&itemcode=" +item[i]+ "&kot=" +kot[i]+ "&itemStatus1=" +dropdown1[i]+ "&billno=" +billno;
}
else if(dropdown1[i] == 1){
document.detailsview.action="BillCB.jsp?method="+"9"+"&itemcode="+item[i]+"&kot="+kot[i]+"&itemStatus1="+dropdown1[i]+ "&billno="+billno;
}
else{
document.detailsview.action= "BillCB.jsp?method="+"10"+"&itemcode="+item[i]+"&kot="+kot[i]+"&itemStatus1="+dropdown1[i]+ "&billno="+billno;
}
}
}
JSP file:
while retrieving here the dropdown1 length is 1.but for kot and item length is 18
please provide your solution!
case 11:
gotMethod = true;
billdetails_be.billno = Integer.valueOf(request.getParameter("billno"));
String[] kotCB2=request.getParameterValues("kot");
String[] itemCB2=request.getParameterValues("itemcode");
String[] statCB2=request.getParameterValues("itemStatus1");
int[] kotarr2=new int[kotCB2.length];
int[] itemarr2=new int[itemCB2.length];
int[] statarr2=new int[statCB2.length];
System.out.println("IN AVAILABLE:length of array is:"+statCB2.length);
System.out.println("IN AVAILABLE:length of array is:"+kotCB2.length);
for(int i=1;i<itemarr2.length;i++)
{
kotarr2[i]=Integer.parseInt(kotCB2[i]);
}
for(int i=1;i<itemarr2.length;i++)
{
itemarr2[i]=Integer.parseInt(itemCB2[i]);
}
for(int i=1;i<itemarr2.length;i++)
{
statarr2[i]=Integer.parseInt(statCB2[i]);
}
for(int i=1;i<itemarr2.length;i++)
{
int kotint2=kotarr2[i];
int itemint2=itemarr2[i];
int statint2=statarr2[i];
System.out.println( i+"the value of kot in available"+ kotint2);
int availablebill = websrv.availablebill(billdetails_be.billno, kotint2, itemint2,statint2);
}
design page(jsp)
<%
for(int i=0;i<count;i++){
%>
<TR>
<TD> <input id="itemcode<%=i%>" type="hidden" name="itemcode" value=" <%=billdetails_be.get(i).itemcode%>"></TD>
<TD><%=billdetails_be.get(i).itemdescription%></TD>
<TD> <input id="quantity<%=i%>" type="text" name="quantity" style="width: 30px;" readOnly="readonly" value="<%=billdetails_be.get(i).quantity%>" >
<input type="submit" id="inc<%=i%>" onclick= "doIt1(1,<%=i%>);" value="+" style="width:20px; height:20px; border-radius:10px; padding:0 0; " />
<input type="submit" id="dec<%=i%>" onclick="doIt1(-1,<%=i%>);" value="-" style="width:20px; height:20px; border-radius:10px; padding:0 0; "/>
</TD>
<TD> <%=billdetails_be.get(i).price%></TD>
<TD> <%=billdetails_be.get(i).itemstatusdescription%></TD>
<td>
<select name="statusi" id="sel<%=i%>">
<option value="0">Available</option>
<option value="1">Unavailable</option>
<option value="2">Delete</option>
</select>
</td>
<td> <input id ="kot<%=i%>" type="text" style="border: 0px solid #000000;" name="kot" style="cursor: text" readonly="readonly" value="<%=billdetails_be.get(i).kotno%>"></td>
<TD> <input id="myquantity<%=i%>" type="hidden" name="quantity1" value=" <%=billdetails_be.get(i).quantity%>"></TD>
</TR>
<%
}
%>
<td><input style="width: 150px; " class="btn btn-green btn-large" type="submit" name="save" id="save" value="save" class="button" onclick="savetheChanges(<%=count%>);"> </td>
PROBLEM
1.this is in js file ,from which i am passing the value to BillCB.jsp
document.detailsview.action="BillCB.jsp?method=" + "11" + "&itemcode=" +itemcode1[i]+ "&kot=" +kot1[i]+ "&itemStatus1=" +selection[i]+ "&billno=" +billno;
2.this is in BillCB.jsp file (method :11)
String[] statCB2=request.getParameterValues("itemStatus1");
int[] statarr2=new int[statCB2.length];
System.out.println("IN AVAILABLE:length of STATUS array is:"+statCB2.length);
In AVAILABLE:length of STATUS array is:1-this is the problem.
I haven't gone through your code but you might try this solution
Use JSON.stringify method to stringify the array data then send the stringified data to server using below line.
data=JSON.stringify("yourArrayName")
then send "data" as a parameter to JSP
On server side convert it back to array data.