Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Values not Displaying for Expression Language

I have a web app that displays movie schedule details (retrieved from a MySQL database) when the user clicks on the movie poster.

Bean:

import java.sql.Date;
import java.sql.Time;

public class Schedule {

private String[] malls;
private Integer[] cinemas;
private Double[] prices;
private Date[] dates;
private Time[] times;

// getters and setters
}

Servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int movieId = request.getParameter("movieid") != null ? Integer.parseInt(request.getParameter("movieid")) : 0;
    
if(movieId != 0) {  
    DatabaseManipulator dm = new DatabaseManipulator();     
    ...
    
    // get schedule details from database
    String[] malls = dm.getMallNames(movieId);
    Integer[] cinemas = dm.getCinemaNumbers(movieId);
    Double[] prices = dm.getMoviePrices(movieId);
    Date[] dates = dm.getShowDates(movieId);
    Time[] times = dm.getShowTimes(movieId);
    
    // assemble bean objects
    Schedule schedule = ScheduleAssembler.getInstance(malls, cinemas, prices, dates, times);
    
    // returns new session if it does not exist
    HttpSession session = request.getSession(true);
    
    // bind objects to session
    session.setAttribute("schedule", schedule);
    session.setAttribute("times", times); // for schedule row count
            
    // redirect to view schedule page
    response.sendRedirect("view-schedule.jsp");

} else {
    // redirect when servlet is illegally accessed
    response.sendRedirect("index.jsp");
}
}

JSP:

<%@ page import="java.sql.*" %>
...
<body>
...
<strong>VIEW MOVIE SCHEDULE</strong>
... 
<table id="schedule">
<tr><td class="titlebg" colspan="5">MOVIE SCHEDULE</td></tr>
<tr>
    <td class="catbg">Mall</td>
    <td class="catbg">Cinema</td>
    <td class="catbg">Price</td>
    <td class="catbg">Date</td>
    <td class="catbg">Time</td>
</tr>

<%
Time[] times = (Time[]) session.getAttribute("times");

int rowCount = times.length;

for(int ctr = 0; ctr < rowCount; ctr++) { %>        
<tr>
    <td>${schedule.malls[ctr]}</td>
    <td class="cinema">${schedule.cinemas[ctr]}</td>
    <td>PHP ${schedule.prices[ctr]}</td>
    <td>${schedule.dates[ctr]}</td>
    <td>${schedule.times[ctr]}</td>
</tr>
<% } %>
</table>
</body>

Q U E S T I O N:
enter image description here
It is adding the desired number of rows to the schedule table (based on the available showtimes in the database), but the values in the EL are not appearing.

Test println()'s in the Servlet are appropriately getting the array values and hardcoded array indices per table data (schedule.malls[0] instead of ctr) works as it should be.

Why are the values not displaying when placed in a for-loop?

like image 252
silver Avatar asked Nov 01 '22 15:11

silver


2 Answers

The problem is that ctr is not an implicit object and is not in any of the scopes (request, session, etc.), so it is not in the scope of the EL expressions.

To fix it you have basically two options:

OPTION #1 (deprecated)

Use scriptlets (don't forget to import the class Schedule at the beginning of your JSP):

<%
Time[] times = (Time[]) session.getAttribute("times");

int rowCount = times.length;

for(int ctr = 0; ctr < rowCount; ctr++) { %>        
<tr>
    <td><%= ((Schedule)session.getAttribute("schedule")).malls[ctr] %></td>
    <td class="cinema"><%= ((Schedule)session.getAttribute("schedule")).cinemas[ctr] %></td>
    <td>PHP <%= ((Schedule)session.getAttribute("schedule"))..prices[ctr] %></td>
    <td><%= ((Schedule)session.getAttribute("schedule")).dates[ctr] %></td>
    <td><%= ((Schedule)session.getAttribute("schedule")).times[ctr] %></td>
</tr>
<% } %>

OPTION #2 (the politically correct one)

You'll need to refactorize that Schedulle class and use JSLT tags, something like:

<c:forEach var="rowItem" items="${rowList}" >
<tr>
    <td>${rowItem.mall}</td>
    <td class="cinema">${rowItem.cinema}</td>
    <td>PHP ${rowItem.price}</td>
    <td>${rowItem.date}</td>
    <td>${rowItem.time}</td>
</tr>
</c:forEach>

Don't forget to declare the taglib at the beggining of your JSP:

<% taglib prefix="c" uri="http://java.sun.com/jsp/jslt/core" %>

I haven't tested this as I have no way to debug JSPs right now, this is for you to get an idea of your options.

like image 158
morgano Avatar answered Nov 15 '22 04:11

morgano


This is basically morgano's OPTION #1. He correctly pointed out the EL is not able to read the ctr I declared in the scriptlet, so his answer is my accepted answer.

This is just to show how I went about the Option #1 approach:

<%@ page import="com.mypackage.model.Schedule" %>
...
<%
Schedule schedule = session.getAttribute("schedule") != null ? (Schedule) session.getAttribute("schedule") : null;

if(schedule != null) {  
    int rowCount = schedule.getTimes().length;

    for(int ctr = 0; ctr < rowCount; ctr++) {
%>
<tr>
    <td><%=schedule.getMalls()[ctr] %></td>
    <td class="cinema"><%=schedule.getCinemas()[ctr] %></td>
    <td>PHP <%=schedule.getPrices()[ctr] %></td>
    <td><%=schedule.getDates()[ctr] %></td>
    <td><%=schedule.getTimes()[ctr] %></td>
</tr>
<% }

} else {
    // redirect on illegal access
    response.sendRedirect("index.jsp");     
}
%>
like image 33
silver Avatar answered Nov 15 '22 03:11

silver