JSP: Test if a variable from pageContext.findAttribute("") exists - java

I set this context.setAttribute("isGood", isGood) in a servlet to be able to get the value on a JSP file with pageContext.findAttribute("isGood").
Sometimes context.setAttribute("isGood", isGood) could not be set so it doesn't exist and my JSP gives me an error because it can't get it.
How can I test if pageContext.findAttribute("isGood") exists in my JSP?
Thanks.

You can use conditional check tag as:
<c:if test="${pageContext.getAttribute("isGood") != null }">
</c:if>

Related

How to check a request parameter in JSP using JSTL, especially <c:set> tag?

I'm trying to parse a request param in my JSP using tags: <c:if> and <c:set>
The parameter is named result, so my variable in jsp is requestScope["result"]
I have two issues:
I wanna check two cases: param is null or not,
I used the following code
<c:if test='${not empty requestScope["result"]}'>
<c:set var = "result" value = '${requestScope["result"] }'/>
</c:if>
<c:if test='${empty requestScope["result"]}'>
<c:set var = "result" value = ' not available'/>
</c:if>
In order to set the result from request in result variable or "not available" value if it is null
This code always shows not available but when i delete the second test, it shows the result corrctly
Also I tried with '${param.result != null}' test, it gives the same result.
Thank you in advance
I have similiar code and this way works for me:
<c:set var="result" value="${(requestScope['result'] == null || requestScope['result'] eq '') ? 'not available' : ${requestScope['result']}}"/>

In JSP Spring Framework, is it possible to create a c:if statement logically based on a user role and another condition?

For example, I want a field to show up if it's past 12 PM and the user role is teacher.
EDIT: I'm asking if there is a way to get the role of the user in JSP. What's the proper way to retrieve that?
Can I write it like this:
<c:if test ="${(Bean.after12PM) and (hasAnyRole('teacher'))}">
//some code
</c:if>
Is it the syntax for the jstl if or the value which is passed from a class or a object , not sure what your looking for
<c:set var="salary" scope="session" value="${2000*2}"/>
<c:if test="${salary > 2000}">
<p>My salary is: <c:out value="${salary}"/><p>
</c:if>
hope this helps
<c:set property="currentTime" value="${System.CurrentTimeMillis()}"/>
<c:set property="midnight" value="SET YOUR CUSTOM MIDNIGHT"/>
<c:set property="role" value="SET YOUR CUSTOM ROLE"/>
<c:if test="${currentTime > midnight && role=="teacher"}">
.....
</c:if>
Should be something like this, check for the correct "and" sintaxe inside that "test" it is been a while I can't recall it exactly.
Remember you can set any of those variables "midnight, role, currentTime" on your controller and set it on session or send it through request

passing string array as hidden value from one jsp to another jsp

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.

Accessing a javabean from a jsp page

I've a session bean with some string properties.
How could I access it and its properties from a jsp page, checking if one of them is equal or not to a given string?
I think I've to use <c:choose> and <bean:write> tag, but how? An example?
<c:if test="${beanName.beanProp == 'the string'}">
It's equal.
</c:if>

How to compare 2 variables with <c:if>

I am using jsp + spring mvc, and on jsp page I have some list - catList with idand name and also have some variable test. I am trying to compare cat.id and test, but can't because every time have runtime syntax errors:
<c:forEach var="cat" items="${catList}" varStatus="i">
<c:out value="${cat.id}"/>
<%-- comparison and some action--%>
</c:forEach>
tried:
<c:if test="${category.id == test}" >
<c:if test="${category.id eq test}" >
<c:if test="${category.id eq ${test}}">
Update: I resolved this problem, simply it was error with server redeploy
All JSTL tags (as well as XML and HTML tags) require an opening tag and a closing tag. The opening tag defines where the tag body starts (and also allows you to define attributes). The closing tag defines where the tag body ends.
In the following code sample, the closing tag is on the last line.
<c:if test="${category.id == test}" >
<b>Test passed!</b>
</c:if>
As you can see, it has the same name as the opening tag, and starts with a / (and has no attributes).
The first two opening tags that you included in your question should work.

Categories