javax.servlet.jsp.JspException: No collection found - java

This is my arraylist for index 17
In this code may have data list or may not. Data is dispay when there are records. Not handled for no records
Java code:
//To avoid arrayindexoutofbound exception length checked
if (resultList.length >17) {
statisticsDetails.setqNames(resultList[17]);
//System.out.println(resultList[17]);
String[] qName=resultList[17].split("\\^");
List<StatisticsDetails> statisticsDetailsList = new ArrayList<StatisticsDetails>();
for (String queueName:qName ){
StatisticsDetails details = new StatisticsDetails();
String[] splitQueues = queueName.split("=");
details.setqKey(splitQueues[0]);
details.setqCount(splitQueues[1]);
statisticsDetailsList.add(details);
}
statisticsDetails.setqNamesList(statisticsDetailsList);
}
Jsp code:
<logic:iterate id="iteratorId" name="statistics.statisticsDetails" property="qNamesList">
<tr>
<td class="col-sm-6 col-md-6 col-lg-6"><bean:write name="iteratorId" property="qKey" /></td>
<td class="col-sm-6 col-md-6 col-lg-6"><bean:write name="iteratorId" property="qCount" /></td>
</tr>
</logic:iterate>
how to avoid arraybound error and JSP to handle when no data found

Use a null check using JSTL(Do not forget to import the tag library):
import core library:
<%# taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
and then use:
<c:if test="${not empty statistics.statisticsDetails}">
//keep your code here to iterate the list
</c:if>
Or use:
<%# taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
and then:
<c:if test="${fn:length(statistics.statisticsDetails) > 0}">
//keep your code here to iterate the list
</c:if>

Please check all mapping is correct or not in the struts-config.xml. In my case, i found incorrect type was mentioned for form and then it was giving the error. Type means package name for the form. in tag.

Related

Why wouldn't the table show in the local host (jsp file)?

I wrote a jsp file for the web.And I use tomcat 8.5.
However, the table didn't come out as I want it to be.
the part came out right but the part is totally empty even no frames.
Here is the code below.
<%# page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%# page import="java.sql.*,enrollBean.*, Attendance.*"%>
<!DOCTYPE html>
<html><head><title> </title></head>
<body>
<%# include file="top_project.jsp"%>
<% if (session_id == null) response.sendRedirect("login.jsp"); %>
String c_id= request.getParameter("c_id");
<table width="75%" align="center" border> <br>
<tr>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
<th>6</th>
<th>7</th>
<th>8</th>
</tr>
<br>
<jsp:useBean id="att" class="AttendanceBean.AttendanceBean"/>
<jsp:useBean id="enrollMgr" class="enrollBean.EnrollMgr" />
<%
Vector vlist = enrollMgr.getEnrollList(session_id);
int counter = vlist.size();
Vector vlist2 = att.getAttendanceList(session_id,<%=en.getCId()%>);
int counter = vlist2.size();
%>
<tr>
<td align="center"><%=en.getCId()%></td>
<td align="center"><%=en.getCIdNo()%></td>
<td align="center"><%=en.getCName()%></td>
<td align="center"><%=en.getCUnit()%></td>
<td align="center"><%=en.getCId()%></td>
<td align="center"><%=en.getCIdNo()%></td>
<td align="center"><%=en.getCName()%></td>
<td align="center"><%=en.getCUnit()%></td>
</tr>
</table>
</body>
</html>
Please help me with fixing this error.
At least you have the following errors in JSP / HTML source:
String c_id= request.getParameter("c_id"); should be enclosed with <% ... %>.
<br> is not allowed between <table> and <tr>.
<br> is not allowed between </tr> and <tr>.
Is the variable en introduced in top_project.jsp?

java, JSTL and getting values by key

I would like to know why I am not able to get values from an outer hashtable using the keys in jstl, the keys are integer values, the values are inner hashtables and I am working on some legacy code hence the reason for the Hashtables, which are returned from a stored procedure call in this form.
Oddly in the inner hashtable as I process it in my forEach loop I am able to get those values out... ${data['NAME']} actually does work.
I am able to loop over the entire outer hashtable using JSTL forEach loop and its ok but if I try to get a value like ${missing_ciphers[1]} or even ${missing_ciphers['1']} then nothing is returned.
The hashtable when printed looks like this:
{4={SOURCE=D, NAME=D}, 3={SOURCE=C, NAME=C}, 2={SOURCE=B, NAME=B}, 1={SOURCE=A, NAME=A}}
I am able to loop over it ok using the following code but the list is not in the order I want it to be in so I wanted to use the loop counter to get objects out by their key but this doesnt seem to return anything (the inline styles will be moved into a css file once I have this working as I want...):
<c:forEach items="${missing_ciphers}" var="ciphers" varStatus="cipher_loop">
<c:set var="data" value="${ciphers.value}">
</c:set>
<tr style="border-left: none; border-right: none;" class="${cipher_loop.index % 2 == 0 ? 'even' : 'odd'}">
<td><span style="font-weight: bold;">${data['SOURCE']}</span></td>
<td><span style="font-weight: bold;">${data['NAME']}</span></td>
</tr>
</c:forEach>
Can anyone help me understand what is happening here and why I am not able to get a value out using the ${missing_ciphers[1]} type syntax?
I should add that the following code does print the inner hashtable into the tomcat console:
<% System.out.println("val: " + ((Hashtable)request.getAttribute("missing_ciphers")).get(1)); %>
I have even tried the following just to see if it was down to the type of the key this still doesnt get from the outer hashtable:
<c:forEach items="${missing_ciphers}" var="ciphers" varStatus="cipher_loop">
<c:set var="counter" value="${cipher_loop.index + 1}" />
<fmt:parseNumber var="fmt_counter" integerOnly="true" type="number" value="${counter}" />
<c:out value="${fmt_counter}" />
<c:set var="data" value="${missing_ciphers[fmt_counter]}"></c:set>
<tr style="border-left: none; border-right: none;" class="${cipher_loop.index % 2 == 0 ? 'even' : 'odd'}">
<td><span style="font-weight: bold;">${data['SOURCE']}</span></td>
<td><span style="font-weight: bold;">${data['NAME']}</span></td>
</tr>
</c:forEach>
Thanks
You should be using variable name assigned to varStatus - 'cipher_loop' and not varStatus itself.
Also you should use the index property or the count property to get the current index. (index starts at 0 and count at 1 by default)
${missing_ciphers[varStatus]}
should be
${missing_ciphers[cipher_loop.count]}
Edit
What is the type of your key in your outer hastable? Integer?
The type of varStatus.index (or counter) is long and you will have to explicitly set it as integer to use it as a key of your outer table
The following code works for me
<jsp:directive.page import="java.util.*"/>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
Hashtable h = new Hashtable();
Hashtable h1 = new Hashtable();h1.put("SOURCE","D");h1.put("NAME","D");
Hashtable h2 = new Hashtable();h2.put("SOURCE","C");h2.put("NAME","C");
Hashtable h3 = new Hashtable();h3.put("SOURCE","B");h3.put("NAME","B");
Hashtable h4 = new Hashtable();h4.put("SOURCE","A");h4.put("NAME","A");
h.put(4,h1);h.put(3,h2);h.put(2,h3);h.put(1,h1);
request.setAttribute("missing_ciphers",h);
%>
<c:forEach items="${missing_ciphers}" var="ciphers" varStatus="cipher_loop">
<c:set var="counter" value="${cipher_loop.index + 1}" />
<c:out value = "${counter}"/>
<c:set var = 'counter' value = '<%=new Integer(pageContext.findAttribute("counter").toString())%>'/>
<c:set var="data" value="${missing_ciphers[counter]}"></c:set>
<tr>
<td><span style="font-weight: bold;">${data['SOURCE']}</span></td>
<td><span style="font-weight: bold;">${data['NAME']}</span></td>
</tr>
</c:forEach>
OTOH, if the keys in your table is of type long, then you can use the varstatus.index property directly
<jsp:directive.page import="java.util.*"/>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
Hashtable h = new Hashtable();
Hashtable h1 = new Hashtable();h1.put("SOURCE","D");h1.put("NAME","D");
Hashtable h2 = new Hashtable();h2.put("SOURCE","C");h2.put("NAME","C");
Hashtable h3 = new Hashtable();h3.put("SOURCE","B");h3.put("NAME","B");
Hashtable h4 = new Hashtable();h4.put("SOURCE","A");h4.put("NAME","A");
h.put(new Long(4),h1);h.put(new Long(3),h2);h.put(new Long(2),h3);h.put(new Long(1),h1);
request.setAttribute("missing_ciphers",h);
%>
test
<<c:forEach items="${missing_ciphers}" var="ciphers" varStatus="cipher_loop">
<c:set var="counter" value="${cipher_loop.index + 1}" />
<c:set var="data" value="${missing_ciphers[counter]}"></c:set>
${data}
<tr>
<td><span style="font-weight: bold;">${data['SOURCE']}</span></td>
<td><span style="font-weight: bold;">${data['NAME']}</span></td>
</tr>
</c:forEach>

how to get values from jsp in ${clue.text}

I define a class of Clue :
Class Clue{
...
String text;
..
getter/setter method
}
I want to get ${clue.text} from jsp to use in the scope "<%...%>" to just show subSequence(0, 10),like this:
<c:forEach items="${clues}" var = "clue">
<tr>
<td>${clue.weibo.user.name}</td>
<td>
<%
String str = ${clue.weibo.text};
%>
<%=str.subSequence(0, 10) %>
</td>
</tr>
</c:forEach>
How can I do?
To access A JSTL variable in a scriplet:
<%
String str = (String)pageContext.getAttribute("clue.weibo.text", PageContext.REQUEST_SCOPE);
%>
However, a better way is to use JSTL functions to manipulate the string.
<%# taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<c:forEach items="${clues}" var = "clue">
<tr>
<td>${clue.weibo.user.name}</td>
<td>
${fn:substring(clue.weibo.text, 0, 10)}
</td>
</tr>
</c:forEach>

jsp if variable starts with

I need view if variable starts with keys "ef", like ef1, efabc, efanythink... and if yes, show error message, i past here mix of php and jsp, of course incorrect and with errors, i no understand jsp:
<c:if test="${empty channel.getChannelName()}">
<%
if (string_starts_with(${channelName}, 'ef')) { header("location:http://google.com"); }
or show this div of error
<div class="error"> This Channel url Portected!</div>
Original file: http://pastebin.com/ach8PXY9
<%# taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<%# taglib uri = "http://java.sun.com/jsp/jstl/functions" prefix = "fn" %>
<c:if test='${fn:startsWith(channel.channelName, "ef")}'>
...
</c:if>
<c:if test='${not fn:startsWith(channel.channelName, "ef")}'>
...
</c:if>
OR you can use
'<c:choose> <c:when>'
Theres a JSTL method for that!
<c:choose>
<c:when test="${fn:startsWith(channel.getChannelName(), "ef")}">
<script type="text/javascript">window.location.replace("http://google.com/");</script></c:when>
<c:otherwise>
<div class="error"> This Channel url Portected!</div>
</c:otherwise>
</c:choose>
http://docs.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/fn/tld-summary.html
also possible:
if (channelName != null && channelName.indexOf("ef") == 0) {
%><div class="error"> This Channel url Portected!</div><%
}

Displaying array values in jsp

I have following two array in my code
List<Double> centralityList = (List<Double>) request
.getAttribute("centralityList");
List<String> labelList = (List<String>) request
.getAttribute("labelList");.
Now I have six string values and corresponding 6 double values of the string in these two array. My question is how to display them in tabular format in my JSP?Is this possible
For Example:
label list contains [a,b,c,d,e]
centrality list contains- [4,6,8,9,0]
So now I want to display output like:
label list centrality list
a 4
b 6
c 8.
. .
etc
Yes of course you can. You can use scriplets but they are not recommended. Instead use JSTL.
Try this out:
Have this at top of your JSP:
<%# taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
And code for displaying data
<c:forEach begin="0" end="${fn:length(centralityList) - 1}" var="index">
<tr>
<td><c:out value="${centralityList[index]}"/></td>
<td><c:out value="${labelList[index]}"/></td>
</tr>
</c:forEach>
try this code
<%
List<Double> centralityList = (List<Double>) request
.getAttribute("centralityList");
List<String> labelList = (List<String>) request
.getAttribute("labelList");
String myString="";
%>
<table>
<tr><td>
<%
for(int i = 0; i < labelList.size(); i++)
{
out.println((String)labelList.get(i));
}
%>
</td><td>
<%
for(int i = 0; i < centralityList.size(); i++)
{
out.println((double)centralityList.get(i));
}
%>
</td>
</table>
You can achieve this eaisly by using JSTL which is even more easy and far better way but as in your code I didn't find any evidence of using JSTL , so this is the way for now
You can use JSTL tags and iterate through this-
<c:forEach var="Array" items="userNames">
// do something with the element
</c:forEach>
Use this in your jsp page
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

Categories