I am really new to JSP and I am trying to figure out how to show my data from an ArrayList in my HTML?
Could I do something like this:
<article>
<p><%= myArrayList.elementAt(0).toString() %></p>
</article>
Or do I use an out.printnln?
Any guidance is much appreciated. And the question really applies to more than just an ArrayList, it is more of that I have String data retrieved my java classes, and I need to display it in my HTML.
As always, thanks!
1 Yes it should work that way. Yet you only show the first element, you need to put that inside a loop to show everything
<% for(Object obj : myArrayList) { %>
<article>
<p><%= obj.toString() %></p>
</article>
<% } %>
2 out.println is the same than <%=. Better use the latter to show clearly the HTML code, out.println would be for complicated expressions (to avoid opening and closing <%= too often for readability.
<c:forEach items="${myArrayList}" var="product" varStatus="loop">
<td>
${product.id}
${product.name}
</td>
</c:forEach>
will call the getName(), and getId() methods and output them to html.
Related
In have this transfer from a servlet to the jsp. Since the AllPost is a list and I go through it with for, I do not know whether it's a short way close to what I give here.
Meanwhile I solved the issue inside the class, i.e. before the value coming to the servlet & jsp, as below.
So, at this point I do not know whether it makes sense to try further.
Thank you in advance!
<c:forEach items="${AllPost}" var ="p">
<tr>
<%! int len = Integer.valueOf(${p.detail.length()}); %>
<%
if(len<25){
${p.detail}.concat("....");
}
%>
<td>${p.detail.substring(0,25)}...</td>
The 2nd option code from routine class:
ResultSet rs = DB.getPreparedStatement(SQL).executeQuery();
while(rs.next()){
News n = new News(rs.getInt(1),...);
if(Integer.valueOf(rs.getString(1.length())<25){
String sz = n.getDetail().concat(".........................");
n.setDetail(sz);
}
<%! int len = Integer.valueOf(${p.detail.length()}); %>
Here you're mixing EL and scriptlets, it's not possible to use both. Not only that, you're using JSTL in your code as well, but still resorting to scriptlets? In almost all cases, if you're using JSTL & EL, you don't need to use any scriptlets. Scriptlets is an oldschool way of using JSPs.
Your code for the first option should look something like this (without scriptlets):
<c:forEach items="${AllPost}" var ="p">
<tr>
<c:if test="${fn:length(p.detail) lt 25}">
<td>${p.detail}...</td>
</c:if>
<c:if test="${fn:length(p.detail) gt 25}">
<td>${fn:substring(p.detail, 0, 25)}...</td>
</c:if>
</tr>
</c:forEach>
make sure to include this at the top of the page for jstl functions (if you haven't already)
<%# taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
String optionselected =request.getParameter("digram");
out.print(optionselected);`
if(optionselected.equals("1"))
<img src="C:\Users\jitutjs\workspace\dispImpl\Traing.jpg"/>
else
if(optionselected.equals("2"))
<img src="C:\Users\jitutjs\workspace\dispImpl\WebContent\Circle.jpg"/>
else
out.print("no diagarm");
%>
<img src="..." /> is not related to java, it is a html tag, all that is inside the scriplets is treated as java code. If you want to use scriplets, you must do it like this:
<%
String optionselected =request.getParameter("digram");
out.print(optionselected);`
if(optionselected.equals("1"))
%>
<img src="C:\Users\jitutjs\workspace\dispImpl\Traing.jpg"/>
<%
else
if(optionselected.equals("2"))
%>
<img src="C:\Users\jitutjs\workspace\dispImpl\WebContent\Circle.jpg"/>
<%
else
out.print("no diagarm");
%>
P.S. My advice is to not use scriplets, they are old and deprecated. It is better to take a look at JSTL tag library.
img tag should be out of scriplest. Also, You have given the src value in wrong way, you can use absolute path of your web-app as <img src="/Circle.jpg"/> as this may vary when application is deployed elsewhere.
I have a jsp page in which I am trying to print some DTOs from a list using their toString() method. Currently, my code looks like this:
<c:forEach items="${sessionScope.users}" var="user">
<li><input type="radio" name="users" value="user">
<c:out value="${user}"/>
</input></li>
</c:forEach>
The problem is that toString() contains \n instead of <br> for newlines. (I even tried to hard-code the <br> tags, but they were printed as literals.) I am sure this would be much easier using scriptlets, but the powers that be don't want to see any Java code in our view. Has anyone successfully done this?
I don't think a multiline toString() is a great idea. Why don't you print out the fields of user by calling them such as:
<c:out value="${user.name}"/> <br/>
<c:out value="${user.age}"/> <br/>
I am trying to pass String array from one jsp to another. I am using JSTL in my JSP.
In my first JSP i am doing like this
<c:if test="${fn:length(empBean.additionalEmailAddr) gt 0}">
<c:forEach begin="0" end="${fn:length(empBean.additionalEmailAddr) - 1}" var="ind" >
<input type="hidden" name="inbdAdditionalEmailAddr" value="${empBean.additionalEmailAddr[ind]}"/>
</c:forEach>
</c:if>
and trying to access the values in another jsp as follows
<%
String[] inbdAddEmlAddr = request.getParameter("inbdAdditionalEmailAddr");
%>
and i am planning to use JSTL to print the array values.
In the second jsp i am getting type mismatch error. Please help.
Is this the right approach ? Any help is appreciated
Thanks
request.getParameter() returns a String which the code attempts to assign to a String[], causing the exception.
Use request.getParameterValues('inbdAdditionalEmailAddr'); to retrieve parameters as an array.
See the documentation.
I have a JSTL loop where I'm trying to check to see if a given variable is empty or not with a dynamic variable name. When I use c:set with page scope, the variable is not accessible to the if statement. However, when I set it using <% pageCotnext.setAttribute(...); %>, the variable is available.
<%
pageContext.setAttribute("alphaParA", "test");
pageContext.setAttribute("alphaParF", "test");
int i = 0;
%>
<ul class="alphadex_links">
<c:forEach var="i" begin="0" end="25" step="1" varStatus="status">
<c:set var="currentLetter" scope="page">&#${i+65}</c:set>
<c:set var="currentPar" scope="page">alphaPar${currentLetter}</c:set>
<% pageContext.setAttribute("currentPar", "alphaPar" + (char)('A' + i++)); %>
<li>
<c:choose>
<c:when test="${not empty pageScope[currentPar]}">
The test is always fails when I remove the pageContext.setAttribute block, however it succeeds for A and F as it should when the block is in. I'm very confused and can't find help anywhere.
It fails because HTML doesn't run at the moment JSTL runs. You're effectively passing a Java String A to it instead of the desired character A which would be represented as such based on the HTML entity A when the HTML is retrieved and parsed by the webbrowser after Java/JSP/JSTL has done its job. Please note that your HTML entity is missing the closing semicolon, but this isn't the cause of your concrete problem.
As to the concrete functional requirement, sorry, you're out of luck with EL. It doesn't support char. Your best bet is to deal with strings like this:
<c:forEach items="${fn:split('A,B,C,D,E,F,G,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z', ',')}" var="currentLetter">
<c:set var="currentPar" value="alphaPar${currentLetter}" />
${pageScope[currentPar]}
</c:forEach>
If necessary, just autogenerate the letters as String[] in Java end and set it as application attribute.