I have the working Java code here to display a user's username upon login:
<%= "Welcome " + session.getAttribute("username")%>
If a user is not logged in and at my index.jsp page, it will display "Welcome null".
However, I want to display "Welcome Guest" instead.
Is there any way to do this?
You can do that with JSTL:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Welcome, <c:out value="${sessionScope.username}" default="guest" />
Use JSTL tag libraries. JSP scriplets are deprecated almost a decade back.
This is how you do it using JSTL:
Welcome, <c:out value = "${sessionScope.username == null ? 'Guest' : sessionScope.username}" />
See also:
Our JSTL tag Wiki.
How to avoid Java code in JSP files?
Related
I am putting together an application and would like to apply "Custom" EL Expressions to sections on the page when it is rendered. This will help me control what parts are displayed on the page given the status of the page. The problem is that the EL Expressions are not being evaluated and treated only as String values. For example
<c:if test="${form.conditionEL}" >
<input type="submit" value="Close">
</c:if>
The value of the ${field.conditionEL} might evaluate to something like "submission.status eq 'COMPLETE'".
So, is there a way to get that string value to be interpreted within the JSP page ?
Thanks for any help in making this happen. Let me know if I missed providing any pertinent information to help resolve.
Environment: Java 8, Tomcat 9, JSP 3.0, Spring
Regards,
Mike
I would simply add the submission status to the page context which let's you simply do:
<c:if test="${submissionStatus eq 'COMPLETE'}" >
<input type="submit" value="Close">
</c:if>
Check whether EL expressions are ignored in your page tag. I've also faced the same issue and this resolved mine.
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" isELIgnored="false" %>
I have a question, how to call a servlet from a jsp(chart.jsp) without using <jsp:include page="/servletURL" />because I tried before and I don't know if this is the right reason but it crashes when I use this code above.
I put in my doGet() method to retrieve information from DB and populate my dropdownlist (in chart.jsp) using JSTL+option and then redirect to my page (the same page), what I believe is that everytime the browser writes a new page using c:forEachtag it calls again my servlet and there is a never ending loop (Again, that's just my presumptions)
Here is my code to make it more clear:
my servlet:
ArrayList<Machine> foundMachines = MachineDB.getAllMachines();
request.getSession().setAttribute("foundMachineList", foundMachines);
RequestDispatcher rd = request.getRequestDispatcher("charts/chart.jsp");
rd.forward(request, response);
my jsp:
<jsp:include page="/searchServlet" />
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:forEach var="machine" items="${sessionScope.foundMachineList}">
<option value="${machine.machineId}">${Machine.machineName}
</option>
</c:forEach>
So my question is why my <jsp:include page="/servletURL" /> tag crashes my page and how to fix it. Any sugestion is welcome
Use
response.sendRedirect("//your servlet name");
Most probably is not related but change
${Machine.machineName} to ${machine.machineName}
I am working on a Java application eInvoice application. The purpose of app is to add eInvoices and then display the eInvoices I have added (Works totally fine upto here). However when I display invoices I want the company name to be hyperlinked, and after clicking on the link that particular invoice details should be displayed in a Jsp page. Please note I am passing information to servlet and then I am storing the info in java class using getters and Setters. Multiple records are stored in the Arraylist object. I cannot seem to find the way to do this. Any help is greatly appreciated.
<%
float bigTotal=0;
try
{
#SuppressWarnings("unchecked")
ArrayList<UserInvoice> myVal = (ArrayList<UserInvoice>)session.getAttribute("eInvoice");
out.write("<table border='0'>");
out.write("<th width='200'>Invoice No.</th><th width='300'>Client Name</th> <th width='200'>Total</th><th width='200'>Payment Due Date</th>");
for(UserInvoice invoices:myVal)
{
out.write("<tr>");
out.write("<td height='25'>");
out.write(""+invoices.getInvoiceNo());
out.write("</td>");
out.write("<td>");
out.write(invoices.getClientName());
out.write("</td>");
out.write("<td>");
out.write(String.valueOf(invoices.getTotal()));
out.write("</td>");
out.write("<td>");
out.write(invoices.getPayDueDate());
out.write("</td>");
out.write("</tr>");
bigTotal = bigTotal+invoices.getTotal();
}
out.write("</table>");
}
catch(Exception ex)
{
out.write("<h1>Looks like you haven't added any invoices yet.</h1>");
}
You can use JSTL foreach:
<%# page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<% request.setAttribute("eInvoices", eInvoices); %>
<table>
<c:forEach items="${eInvoices}" var="value">
<tr><td><c:out value="${value.name}"/></td></tr>
</c:forEach>
</table>
Use AJAX to get the details.
(when you click on the invoice name, an HTTP request is made to the page to get the details. These details are returned as HTML with a content-type of 'text/html')
Examples:
loading detail record through ajax
Shopify API call using AJAX and JSP
AJAX with JQuery
In my jsp page i am adding title attribute dynamically in anchor using this code.
<c:set var="titleAttributeValue" value="${(anchorListfields[3] != '') ? 'title=${anchorListfields[3]}' : ''}" scope="page" />
but rather than showing the value of title attribute in anchor it showing like this <span><span>Start my Business</span><i></i></span>
how can i achieve this where is my error in syntax
You codes seems to be working.
Ensure you have JSTL library in your /WEB-INF/lib.
Add <%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> directive to your page.
I am using glassfish 4
I am trying (and learning) to build a java web framework, and in the process of developing its' code generator based on the content of the database. In the view making process, I stumble in a difficulty, which I don't know how to solve it.
Firstly, I want all the pages to be created using the following index.jsp :
<body>
<%# include file="header.jsp" %>
<hr/>
<%# include file="body.jsp" %>
<hr/>
<%# include file="footer.jsp" %>
</body>
And, in the body.jsp, I want it to be like this :
<jsp:include page="${application_modul}" %>
Where application_modul is an attribute defined in its' controller this way :
request.setAttribute("application_modul","user_account\\view_user_account.jsp");
It can find the file correctly, but the processed jsp is not what I expected. Here :
<c:forEach items="[application.models.UserAccountModel#18a49e0, application.models.UserAccountModel#1f82982]" var="item" varStatus="status" >
<tr>
....
You can see the items attribute of jstl forEach, got its variable name (toString())...
Any Idea what the problem is????
I hope I describe my problem correctly
Many thanks!
PS :
I already create a quick fix for this, but not what I want it though. In the generated view_user_account.jsp, I do it like this :
<body>
<%# include file="header.jsp" %>
<hr/>
<c:forEach items="${row}" var="item" varStatus="status" >
<tr>
....
<hr/>
<%# include file="footer.jsp" %>
</body>
You can see that I create the whole file here...
EDITED:
PS : ${row} is an ArrayList populated with data from certain table
So, to summarize your problem in a single sentence, JSTL tags are not been parsed and they end up plain in generated HTML output?
You need to declare JSTL taglib in top of the JSP page where you're using JSTL tags to get them to run. For the JSTL core taglib, that'll be
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
I am not sure but, Try this...
index.jsp
<jsp:param name="parameterName" value="{parameterValue | <%= expression %>}" />