Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get array list size in java jsp?

I have a form that ask for user to enter ID. This form is send to a servlet which checks database to see if user exist. If the user exists then it sends me back their ordered items. The ordered items are returned as an array list. Then this array list is redirect to jsp file to display it on the webpage. The user can have more then one order, therefore the array list size can vary. How do I get the array list size so i can display each item in the array list? I do not want to use JSTL.

index.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script>
    /*function signin(id, id2) {
     document.getElementById(id).style.display = 'block';
     document.getElementById(id2).style.display = 'none';
     //document.getElementById(id3).style.display = 'none';
     }*/
</script> 

<form id="Signin" method="post" action="FindUser">
    <h2>Login </h2>
    <input type="text" name="txtCustID" 
           placeholder="UserID"><br>
    <br><input type="submit" value="Find">
</form>
<%--!<form id="Signup" method="post" action="FindUser" 
      style="display:none;">
    <h2>Sign Up </h2>
    <input type="text" name="UserId" 
           placeholder="User ID"><br>
    <br><input type="text" name="FirstaName" 
               placeholder="First Name"><br>
    <br><input type="text" name="LastName" 
               placeholder="Last Name"><br>
    <br><input type="text" name="Street" 
               placeholder="Street"><br>
    <br><input type="text" name="City" 
               placeholder="City"><br>
    <br><input type="submit" value="Sign Up">
</form> --%>
</body>

FindUser.java:

public class FindUser extends HttpServlet {

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse
    response) throws ServletException, IOException {


String sID = request.getParameter("txtCustID");

String url = ("admin/UserFound.jsp");
try {
    Users one = UserDAO.findUser(sID);
    request.setAttribute("theCustomer", one);
    if (one.getFirstName().equals("none")) {
        url = "admin/UserNotFound.jsp";
    }
    ArrayList user_order = UserDAO.findOrder(sID);
    request.setAttribute("theOrder", user_order);
    response.sendRedirect(url);
    //RequestDispatcher rd = request.getRequestDispatcher(url);
    //rd.forward(request, response);

} catch (ClassNotFoundException e) {
    System.err.print("Failed to load Driver");
    System.err.print(e);
} catch (SQLException e) {
    System.err.print("SQL Error" + e);
    System.err.print("SQL State: " + e.getSQLState());
    System.err.print("Error Code: " + e.getErrorCode());
} catch (Exception e) {
    System.err.println(e);
}
}

UserFound.jsp:

<%-- 
Document   : CustomerFound
Created on : Nov 15, 2014, 9:40:47 PM
Author     : mississauga
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>


<p>First word is: ${theOrder[x].getBoxes()}</P>

<%--<form id="Signin" method="post" action="FindUser" 
       style="display:none;">
     <h2>Login </h2>
     <input type="text" name="txtCustID" 
            placeholder="UserID"><br>
     <br><input type="submit" value="Find">
 </form> --%>
</body>
</html>

}
like image 540
Suji Avatar asked Nov 16 '14 22:11

Suji


People also ask

How to get arraylist size in jsp?

Use fn:length function.

How to get size of list in jsp?

Is it possible at jsp to get the list size using jstl? or shall i use <% %> to get the size like jsp 1.0? If you want to avoid sciptlets, a little wrapper around your list, with a getSize() method could do the trick. Or you could even put the list size in a request atribute.

How to get list in jsp?

Query query = em. createQuery("FROM Item item WHERE item.name = ?"); query. setParameter(1, "abc"); List<Item> items = query. getResultList();


Video Answer


2 Answers

Use fn:length function.

Declare fn namespace on the begining of the JSP file

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

Later in the code

${fn:length(collection)}
like image 174
Grzegorz Żur Avatar answered Oct 26 '22 20:10

Grzegorz Żur


Newer versions of EL accept using the methods defined in the class of the object in EL expressions, so you can simply call the size() method:

<span>Size: ${list.size()}</span>
like image 21
Lucas Basquerotto Avatar answered Oct 26 '22 20:10

Lucas Basquerotto