We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Unable to display user defined object content in jsp

smriti priyanka
on
Medium Priority
68 Views
Last Modified: 2020-06-05
I have a java userdefined Class CertListResponse java.lang.Object The methods inside it is:-Nested Class Summary
Nested Classes
Modifier and Type      Class and Description
static class       CertListResponse.CertificateDetails
Constructor Summary
Constructors
Constructor and Description
CertListResponse()
Method Summary
Methods
Modifier and Type      Method and Description
java.util.List<CertListResponse.CertificateDetails>      getCertificates()

Getter method used to get list of certificate details.
java.lang.String      getMsg()
Getter method used to get dsc response message.
java.lang.String      getStatus()
Getter method used to get transaction status.
void      setCertificates(java.util.List<CertListResponse.CertificateDetails> certificates)
Setter method used to set list of certificate details.
void      setMsg(java.lang.String msg)
Setter method used to set dsc response message.
void      setStatus(java.lang.String status)

Inside CertListResponse class the is getCertificate methods which Lists the details of all certificate i.e

Constructor Summary
Constructors
Constructor and Description
CertificateDetails(java.lang.String serialNumber, java.lang.String issuedTo, java.lang.String issuedBy, java.util.Date validUpto, java.lang.String aliasName, java.lang.String email, java.lang.String locality, java.lang.String state)
CertificateDetails(java.lang.String serialNumber, java.lang.String issuedTo, java.lang.String issuedBy, int certType, java.util.Date validUpto, java.lang.String cdpPoint, java.lang.String aliasName)
Method Summary
Methods
Modifier and Type      Method and Description
java.lang.String      getAliasName()
java.lang.String      getCdpPoint()
int      getCertType()
java.lang.String      getEmail()
java.lang.String      getIssuedBy()
java.lang.String      getIssuedTo()
java.lang.String      getLocality()
java.lang.String      getSerialNumber()
java.lang.String      getState()
java.util.Date      getValidUpto()
void      setAliasName(java.lang.String aliasName)
void      setCdpPoint(java.lang.String cdpPoint)
void      setCertType(int certType)
void      setEmail(java.lang.String email)
void      setIssuedBy(java.lang.String issuedBy)
void      setIssuedTo(java.lang.String issuedTo)
void      setLocality(java.lang.String locality)
void      setSerialNumber(java.lang.String serialNumber)
void      setState(java.lang.String state)
void      setValidUpto(java.util.Date validUpto)
java.lang.String      toString()
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait


I want to display  to display certificate details in my jsp page using c:for each but unable to display.Please find the screenshot of the code and output in the console.
Please give me solution asap.
codeSnippet.JPG
codesnippetREsponse.JPG
Comment
Watch Question

CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
The CertListResponse is not an array/list, so remove the first forEach loop (the forEach for the ${opt}).

Author

Commented:
@girionis what I will write there instead of that please tell.And my inner loop in jsp is correct or I still needs any correction.

Author

Commented:
@girionis After changing the code as you mentioned still I am not able to print the details of the CerificateDetails in the jsp page.Please find the screenshot of jsp page and console for better understanding.
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Remove it, do not replace it with another for loop.

Author

Commented:
@girionis please send me the code snippet .I am not getting you.

Author

Commented:
@ girionis after removing the first for loop also ,I m not able to get the certificateDetails content in the jsp page.while I am getting the object in the jsp page.please resolve I am not getting what's wrong in the page.
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Please post your code.

Author

Commented:
@girionis Please see my code .I am also sharing the screenshot of console.


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
   pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="app.eoffice.dsc.xml.response.CertListResponse"%>
<%@ page
   import="app.eoffice.dsc.xml.response.CertListResponse.CertificateDetails"%>
<%@ page import="java.util.ArrayList"%>


<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Import certificates installed in your system</title>
</head>

<body>
   <form>
      <input type="submit" value="submit option"
         onsubmit="return validate()" />
      <%
         CertListResponse opt = (CertListResponse) session.getAttribute("res");
      System.out.println("\n selected optiion is:" + opt);
      %>

      <div align="center">
         <table border="1">
            <caption></caption>
            <tr>
               <th>Issued By</th>
               <th>Issued To</th>
               <th>Serial No</th>
               <th>Certificate Type</th>
               <th>Expiry Date</th>

               <% ArrayList<CertificateDetails> a = new ArrayList<>();
               a = (ArrayList<CertificateDetails>) opt.getCertificates();
               System.out.println("\n The details in the certificate is:" + a);
               %>
               <c:forEach var="j" items="${a}">
                  <tr>
                     <td><c:out value="${j.issuedTo}" /></td>
                     <td><c:out value="${j.issuedBy}" /></td>
                     <td><c:out value="${j.serialNumber}" /></td>
                     <td><c:out value="${j.certType}" /></td>
                     <td><c:out value="${j.notAfter}" /></td>
                  </tr>
               </c:forEach>
         </table>
      </div>
   </form>
</body>
</html>
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Try this

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
   pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="app.eoffice.dsc.xml.response.CertListResponse"%>
<%@ page
   import="app.eoffice.dsc.xml.response.CertListResponse.CertificateDetails"%>
<%@ page import="java.util.ArrayList"%>


<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Import certificates installed in your system</title>
</head>

<body>
   <form>
      <input type="submit" value="submit option"
         onsubmit="return validate()" />
      <%
         CertListResponse opt = (CertListResponse) session.getAttribute("res");
      System.out.println("\n selected optiion is:" + opt);
      %>

      <div align="center">
         <table border="1">
            <caption></caption>
            <tr>
               <th>Issued By</th>
               <th>Issued To</th>
               <th>Serial No</th>
               <th>Certificate Type</th>
               <th>Expiry Date</th>
            </tr>
               <% ArrayList<CertificateDetails> a = new ArrayList<>();
               a = (ArrayList<CertificateDetails>) opt.getCertificates();
               System.out.println("\n The details in the certificate is:" + a);
               %>
               <c:forEach var="j" items="${a}">
                  <tr>
                     <td><c:out value="${j.issuedTo}" /></td>
                     <td><c:out value="${j.issuedBy}" /></td>
                     <td><c:out value="${j.serialNumber}" /></td>
                     <td><c:out value="${j.certType}" /></td>
                     <td><c:out value="${j.notAfter}" /></td>
                  </tr>
               </c:forEach>
         </table>
      </div>
   </form>
</body>
</html>


CERTIFIED EXPERT
Top Expert 2016

Commented:
a = (ArrayList<CertificateDetails>) opt.getCertificates();

Open in new window


a. why create an ArrayList then assign like that?
b. how do you know the List can be cast to an ArrayList?

Author

Commented:
@cehj becoz it return type is arraylist

Author

Commented:
@ girionis after running ur code also no display in jsp.what changes did u made please tell.please resolve the issue.
CERTIFIED EXPERT
Top Expert 2016

Commented:
Not according to your post:
java.util.List<CertListResponse.CertificateDetails>      getCertificates()
(So it should be cast to List, not ArrayList)

Do you not have the source code for that btw?

Author

Commented:
@cehj no source code I don't have .i was provided jar file.andits java doc
CERTIFIED EXPERT
Top Expert 2016

Commented:
OK. See my comment above. Get rid of the creation and correct the cast

Author

Commented:
@cehj ok
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
I do not think the problem is the cast, the message after the cast is shown in the console.

after running ur code also no display in jsp.what changes did u made please tell.please resolve the issue.
What do you see now?
CERTIFIED EXPERT
Top Expert 2016

Commented:
I do not think the problem is the cast, the message after the cast is shown in the console.
Right - i missed that. But even so, it should be cast to the correct type so it's a change that should be made anyway
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Yes it should, but the user has worse problems, make it work first and optimise later.
CERTIFIED EXPERT
Top Expert 2016

Commented:
<td><c:out value="${j.notAfter}" /></td>

Open in new window

Try commenting that out or change to
<td><c:out value="${j.validUpto}" /></td>

Open in new window

CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Nice catch.

Author

Commented:
@girionis and cehj still i am not getting anything in the jsp.but I am getting the object in the jsp but not able to display its content in jsp.I am getting object which is printed in the console as:-

selected optiion is:app.eoffice.dsc.xml.response.CertListResponse@5b09c569

 The details in the certificate is:[app.eoffice.dsc.xml.response.CertListResponse$CertificateDetails@7269129b] but not able to retreive its details in the jsp.Please help





<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
   pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="app.eoffice.dsc.xml.response.CertListResponse"%>
<%@ page
   import="app.eoffice.dsc.xml.response.CertListResponse.CertificateDetails"%>
<%@ page import="java.util.ArrayList"%>


<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Import certificates installed in your system</title>
</head>

<body>
   <form>
      <input type="submit" value="submit option"
         onsubmit="return validate()" />
      <%
         CertListResponse opt = (CertListResponse) session.getAttribute("res");
      System.out.println("\n selected optiion is:" + opt);
      %>

      <div align="center">
         <table border="1">
            <caption></caption>
            <tr>
               <th>Issued By</th>
               <th>Issued To</th>
               <th>Serial No</th>
               <th>Certificate Type</th>
               <th>Expiry Date</th>
            </tr>
               <% ArrayList<CertificateDetails> a = new ArrayList<>();
               a = (ArrayList<CertificateDetails>) opt.getCertificates();
               System.out.println("\n The details in the certificate is:" + a);
               %>
               <c:forEach var="j" items="${a}">
                  <tr>
                     <td><c:out value="${j.issuedTo}" /></td>
                     <td><c:out value="${j.issuedBy}" /></td>
                     <td><c:out value="${j.serialNumber}" /></td>
                     <td><c:out value="${j.certType}" /></td>
                     <td><c:out value="${j.validUpto}" /></td>
                  </tr>
               </c:forEach>
         </table>
      </div>
   </form>
</body>
</html>
CERTIFIED EXPERT
Top Expert 2016

Commented:
Make sure you're not still looking at the old JSP's output
java.util.List <CertificateDetails>a = (java.util.List<CertificateDetails>) opt.getCertificates();

Open in new window

should be what happens (though you should import java.util.List so you don't need the package name)

Author

Commented:
@cehj I am not looking at the old one.it's new one only
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Can you post your java classes?
CERTIFIED EXPERT
Top Expert 2016

Commented:
You're seeing what on the output page exactly?

Author

Commented:
@girionis I am posting the code of servlet ,rest I have jar files.

package com.webApp;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import app.eoffice.dsc.exception.DSCException;
import app.eoffice.dsc.xml.response.CertListResponse;

/**
 * Servlet implementation class Login
 */
@WebServlet("/Login")
public class Login extends HttpServlet {
   private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();
        // TODO Auto-generated constructor stub
    }

   /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    */
   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      // TODO Auto-generated method stub
      response.getWriter().append("Served at: ").append(request.getContextPath());
   }

   /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    */
   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      // TODO Auto-generated method stub
      doGet(request, response);
      //String name = request.getParameter("name");
      //String address = request.getParameter("address");
      DigiCall d=new DigiCall();
      try {
         CertListResponse dsc = d.dscMethod();
         System.out.println("value of dscMethod is in action is"+dsc);
         if (dsc!=null)
         {
            HttpSession s=request.getSession();
            s.setAttribute("res", dsc);
            response.sendRedirect("welcom.jsp");
         }
         //System.out.println("The value of the dsc method is "+dscMethod);
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (DSCException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   //   response.sendRedirect("welcome.jsp");
 catch (Exception e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }
}


Author

Commented:
@cehj Please find the screenshot of the output page:
CERTIFIED EXPERT
Top Expert 2016

Commented:
Of course, that would happen if the List were empty
System.out.println("\n The number of certs is:" + a.size());

Open in new window

Author

Commented:
@cehj but the object is coming na I have sent u the screenshot of the console also. Please check and tell what to do next.

Author

Commented:
I have sent the servlet class also ,please check and tell whether it is correct or not 

CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
The object is coming but the content might be empty. Try what CEHJ suggested.

Author

Commented:
@girionis ok
CERTIFIED EXPERT
Top Expert 2016

Commented:
Of course you need to guard against that eventuality in your JSP. You can't assume the List has elements

Author

Commented:
@girionis and cehj see when I am printing using System.out.println what I am getting in console ,Please seeI am attaching the screenshot.After seeing the screenshot please send me a code to be written in jsp so that i can display certificate properties i.e certtype,serial no in jsp.

The only thing i noticed is c:out and c:forEach not working in my jsp.

The JSTL jar I have attached is:-
1./WebAppDsc/WebContent/WEB-INF/lib/javax.servlet.jsp.jstl-api-1.2.2.jar
2./WebAppDsc/WebContent/WEB-INF/lib/jstl-impl.jar

PFA

Author

Commented:
how to print all the above mentioned which are printed through system.out.println in console to jsp.

Please send me the code to print all above mentioned in jsp .
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
What happens if you do this:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
   pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="app.eoffice.dsc.xml.response.CertListResponse"%>
<%@ page
   import="app.eoffice.dsc.xml.response.CertListResponse.CertificateDetails"%>
<%@ page import="java.util.ArrayList"%>


<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Import certificates installed in your system</title>
</head>

<body>
   <form>
      <input type="submit" value="submit option"
         onsubmit="return validate()" />
      <%
         CertListResponse opt = (CertListResponse) session.getAttribute("res");
      System.out.println("\n selected optiion is:" + opt);
      %>

      <div align="center">
         <table border="1">
            <caption></caption>
            <tr>
               <th>Issued By</th>
               <th>Issued To</th>
               <th>Serial No</th>
               <th>Certificate Type</th>
               <th>Expiry Date</th>
            </tr>
               <% ArrayList<CertificateDetails> a = new ArrayList<>();
               a = (ArrayList<CertificateDetails>) opt.getCertificates();
               System.out.println("\n The details in the certificate is:" + a);
               %>
               <c:forEach var="j" items="${a}">
                  <tr>
                     <td>1</td>
                  </tr>
               </c:forEach>
         </table>
      </div>
   </form>
</body>
</html>
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
are printed through system.out.println in console to jsp.
You need to use
println(a.get(i).getCertType());
student
CERTIFIED EXPERT
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
rrzstudent
CERTIFIED EXPERT

Commented:
I think I am confused about your code. If you just posted all your code we all  could work on it. Anyway, if that last EL doesn't work maybe try
${res.certificates[0].msg}

Open in new window

  I am just guessing  because I don't know your code.
CERTIFIED EXPERT
Top Expert 2016

Commented:
The only thing i noticed is c:out and c:forEach not working in my jsp.
If that's the case, test it by doing this:

               <c:forEach var="j" begin="1" end="5">
                  <tr>
                     <td><c:out value="${j}" /></td>
                  </tr>
               </c:forEach>

Open in new window

rrzstudent
CERTIFIED EXPERT

Commented:
Please try in your JSP
${res.certificates[0].serialNumber}

Open in new window

Author

Commented:
@ rrz ok I will try .
CERTIFIED EXPERT
Top Expert 2016

Commented:
And please let me know the output of the code i gave you

Author

Commented:
@cehj<c:forEach var="j" begin="1" end="5">
                  <tr>
                     <td><c:out value="${j}" /></td>
                  </tr>
               </c:forEach>

it working.
CERTIFIED EXPERT
Top Expert 2016

Commented:
OK - so that's good evidence that forEach should work for you
rrzstudent
CERTIFIED EXPERT

Commented:
As I posted in my comment above here, your problem is that you are creating a scripting variable and trying to use it in EL.
EL  sees scoped variables.
CERTIFIED EXPERT
Top Expert 2016

Commented:
So, since your forEach is working, combining that with what rrz has said, you should be OK with

               <c:forEach var="j" items="${res.certificates}">
                  <tr>
                     <td><c:out value="${j.issuedTo}" /></td>
                     <td><c:out value="${j.issuedBy}" /></td>
                     <td><c:out value="${j.serialNumber}" /></td>
                     <td><c:out value="${j.certType}" /></td>
                     <td><c:out value="${j.validUpto}" /></td>
                  </tr>
               </c:forEach>

Open in new window

rrzstudent
CERTIFIED EXPERT

Commented:
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
@ smriti priyanka  What's wrong with using?

println(a.get(i).getCertType());
​Have you tried it?
rrzstudent
CERTIFIED EXPERT

Commented:
@girionis,   JSP has an implicit  scripting object  named out. Maybe you mean to post  
out.println(a.get(i).getCertType());

Open in new window

CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Ah yes, you're right, you need to use the PrintWriter to do it. It's been years since I last did JSP, I forgot the syntax.

Author

Commented:
@ girionis,rrz and cehj thanks a lot everyone ,its working now.but  i don't understand earlier it was not working ,when i was trying it to  like this<%
         CertListResponse opt = (CertListResponse) session.getAttribute("res");
      System.out.println("\n selected optiion is:" + opt);
      %>


but when i directly called from servlet which i have set in the session its working,Thanks to rrz.


@ rrz please explain me why it was not working earlier and its working now.what was the problem with the earlier code i was not able to understand.And in which case we should take out the attribute from the session.It will be very helpful for me if you guide me.Thanks
@ cehj your code worked for me thanks.
<c:forEach var="j" items="${res.certificates}">
                  <tr>
                  <c:out value="${res.certificates}"/>
                     <td><c:out value="${res.certificates[0].serialNumber}" /></td>
                     <td><c:out value="${res.certificates[0].issuedBy}" /></td>
                     <td><c:out value="${res.certificates[0].issuedTo}" /></td>
                     <td><c:out value="${res.certificates[0].certType}" /></td>
                     <td><c:out value="${res.certificates[0].notAfter}" /></td>
                  </tr>
rrzstudent
CERTIFIED EXPERT

Commented:
I'm glad you got it working.  
i don't understand earlier it was not working ,when i was trying it to  like this<%
         CertListResponse opt = (CertListResponse) session.getAttribute("res");
      System.out.println("\n selected optiion is:" + opt);
      %>
 
It was working. It just wasn't working the way you wanted it to work.  
The  solution  was given by CEHJ  in your other question.  
https://www.experts-exchange.com/questions/29184186/how-to-convert-object-of-user-defined-class-into-string-object-and-print-its-values-in-the-jsp-page.html   
CEHJ posted
CertListResponse.toString needs to be implemented

If you don't provide one, then the  toString() method is used from its superclass ; java.lang.Object  
Perhaps you were  wondering why  
<c:forEach var="j" items="${a}">

Open in new window

 didn't  work.  I posted above here that  EL  sees scoped variables not scripting variables.
CERTIFIED EXPERT
Top Expert 2016

Commented:
@ cehj your code worked for me thanks.
Thanks - except that isn't the code i gave you - take another look ;)
If you use that code, you will only ever see one certificate

Author

Commented:
@cehj u r right.😄
CERTIFIED EXPERT
Top Expert 2016

Commented:
:) - plus it would crash if there were no certificates

Author

Commented:
@cehj what to write to display all certificates and what if no certificates.I am very new to JSTL and expression language in jsp.
CERTIFIED EXPERT
Top Expert 2016

Commented:
Well i gave you the correct code above. You might use a conditional expression to not display a table at all (since an empty table could be seen as a bit odd), but a message stating there are no certificates. At this stage i'd leave that and maybe add that later as an enhancement

Author

Commented:
@cehj how display all certificates in jsp ,here only first certificate is displayed .what to write in jsp to display all certificates.
CERTIFIED EXPERT
Top Expert 2016

Commented:
Please read my posts. As i said, i've already given you the code
rrzstudent
CERTIFIED EXPERT

Commented:
Actually  you could write it the following way.
               <c:forEach var="j" items="${res.certificates}">
                  <tr>
                     <td>${j.issuedTo}</td>
                     <td>${j.issuedBy}</td>
                     <td>${j.serialNumber}</td>
                     <td>${j.certType}</td>
                     <td>${j.validUpto}</td>
                  </tr>
               </c:forEach>

Open in new window

 The <c:out>  tag is not necessary. EL works in  template text.   You  would use it when  you wanted to use its escapeXml or default attributes.

Author

Commented:
@cehj thanks a lot.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.