I am trying to display a session only if it is not null in jsp file and I am having issues as shown
Syntax error on token "<", invalid Expression 59: 60: <% if(request.getAttribute("message") != null) 61:
this is my jsp file where I am checking if the session is not null but it is not working
<% if(request.getAttribute("message") != null)
{
<%=session.getAttribute("message")%>
}
%>
Please how do I display a session only if it is not null
You cannot use the <%= construct within a scriptlet block <% %>. You will have to write Java code to do this:
<% if(request.getAttribute("message") != null)
{
out.println(session.getAttribute("message"));
}
%>
Note that the <%= expression %> construct is shorthand for out.println(expression).
Related
I am creating a project where i am getting set data in JSP page from database.
if any field data value is null the jsp page is showing null but i do not want to show it on jsp page. please help. i am getting data from bean.
<%=p.getOffer()%>
<% String s = p.getOffer() %>
<% if (<%=s ==null) { %>
show nothing
If you are coding java inside a jsp, you need to use scriptlet tags(<% and %>). So if you are checking for conditions you need to open a scriptlet tag.
<%
String s = p.getOffer();
if (s != null && !s.equals("")) {
out.print(s);
} else { %>
<!-- s is either null or empty. Show nothing -->
<% }%>
What exactly do you want to show when the value is null? Anyway, your approach looks good:
<%=p.getOffer()%> // Here you print the value "offer". If you don't want to show it when it is null, remove this line
<% String s = p.getOffer() %>
<% if (<%=s ==null) { %> // the <%= is unnecessary. if(s==null) is enough
show nothing // show nothing, why not inverse the if and show something
Here another approach:
<%
String s= p.getOffer();
if(s != null){ %>
Offer: <%= s%>
<% }%>
That way you only print the offer when the variable is not null.
By the way: naming a String variable "s" is not recommended, call it "offer" or something to facilitate the reading.
I have the following code
<%
String projectId = request.getParameter("projectId");
%>
<iframe width="100%" id="uploadFrame"
src="testframe.jsp?projectId=<%=projectId %>"></iframe></body>
</html>
and in testframe.jsp I am setting session value as
<%
String projectId = request.getParameter("projectId");
request.getSession(true).setAttribute("prj",projectId);
%>
and finally in servlet, I am getting session value in doGet method as
String prjId = request.getSession(false).getAttribute("prj").toString();
Problem I am facing is sometimes session value is null in doGet method, not all the time, although request.getParameter("projectId") is not null in testframe.jsp
What could be the reason for this?
I tried to send a list from my servlet to a jsp page. This is the servlet code:
Query q = new Query("post").addSort("time", SortDirection.DESCENDING);
PreparedQuery pq = datastore.prepare(q);
QueryResultList<Entity> results = pq.asQueryResultList(fetchOptions);
for (Entity entity : results) {
System.out.println(entity.getProperty ("content"));
System.out.println(entity.getProperty ("time"));
}
req.setAttribute("postList",results);
req.getRequestDispatcher("/tublr.jsp").forward(req, resp);
The jsp code:
<%
QueryResultList<Entity> result = request.getAttribute("postList");
for (Entity entity : results) {
<b> IT WORRRKKKK !!! </b> <br>
}
%>
But I get an error
EDIT : I added
<%#page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%# page import="java.util.List,com.google.appengine.api.datastore.Query.SortDirection,com.google.appengine.api.datastore.*" %>
And now i get a new error
An error occurred at line: 37 in the jsp file: /tublr.jsp Type
mismatch: cannot convert from Object to QueryResultList .....
Caused by:
org.apache.jasper.JasperException: Unable to compile class for JSP:
I m do it for the school and we have to di it like this now , we have to use java in the jsp page.
1) You need to add import statements at top of the JSP.
Example:
<%# page import="java.util.List" %>
2) It is NOT good practice to have Java code directly embedded in JSP
Read more here on SO Wiki
Don't do any coding on JSP page. There is a JSTL library for this kind of stuff, and to iterate and display stuff you should use forEach tag:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
and for loop
<x:forEach select="${postList}" var="item">
... code
</x:forEach>
You forgot <% %> for the html code
<%
QueryResultList<Entity> result = request.getAttribute("postList");
for (Entity entity : results) {
%> <b> IT WORRRKKKK !!! </b> <br><%
}
%>
Have you imported QueryResultList in your jsp?
You need to cast list obtained from request.getAttribute("postList") to QueryResultList.
<%
QueryResultList<Entity> result =(QueryResultList)request.getAttribute("postList");
for (Entity entity : result) {
// Your code goes here You can use <%= %> to print values.
// <b> IT WORRRKKKK !!! </b> <br>
}
%>
For more about expression
I wanted to use a scriplet inside a java script function. I wanted to check for some attribute's vale and give an alert according to that. Following is the function in which the only scriplet statement gives an error.
function UploadMessage() {
<% if((String)request.getAttribute("SuccessMessage").compareTo("Uploaded successfully") == 0) { %>
alert("File Successfully uploaded !");
<%
} %>
}
Is there any way i can do this ? What is the problem here ?
NOTE : I have placed the above snippet in a jsp page
function UploadMessage() {
<% if(((String)request.getAttribute("SuccessMessage")).equals("Uploaded successfully")) { %>
alert("File Successfully uploaded !");
<%
} %>
}
Problem was -
The method compareTo(String) is undefined for the type
Object
Incompatible operand types String and int
How can I use session for facelets ?
What's the syntax...?
I would put a code like this
<% String loginSession = (String)session.getAttribute("login"); %>
<% if(loginSession != null){ %>
Welcome <%= session.getAttribute("firstName") %> !
<% }else{ %>
Guest
<% } %>
Thanks
#{sessionScope.login}
You can't have if-s in JSF (you can with JSTL, but it has complications). Instead you can choose to render or not a component:
<h:outputText value="Guest" rendered="#{sessionScope.login != null}" />