1

Suppose I got class:

import java.io.Serializable;


public class UrlEntry implements Serializable
{
    private String shortUrl;
    private String longUrl;
    private long clicks;


    public UrlEntry(String shortUrl, String longUrl, long clicks)
    {
        this.shortUrl = shortUrl;
        this.longUrl = longUrl;
        this.clicks = clicks;
    }


    public String getShortUrl()
    {
        return shortUrl;
    }
    public void setShortUrl(String shortUrl)
    {
        this.shortUrl = shortUrl;
    }
    public String getLongUrl()
    {
        return longUrl;
    }
    public void setLongUrl(String longUrl)
    {
        this.longUrl = longUrl;
    }
    public long getClicks()
    {
        return clicks;
    }
    public void setClicks(long clicks)
    {
        this.clicks = clicks;
    }

    public UrlEntry(){}


}

This is code from controller servlet

HttpSession session =  request.getSession(true);
ArrayList<UrlEntry> list = new ArrayList<UrlEntry>();
list.add(new UrlEntry("abc","site1.com",1));
list.add(new UrlEntry("def","site2.com",2));
list.add(new UrlEntry("ghi","site3.com",3));
session.setAttribute("urls", list);                     
response.sendRedirect("index.jsp");

This is part of code from index.jsp this works well

<%=request.getSession().getAttribute("urls")%>

but this doesn't work:

<%=(ArrayList<UrlEntry>)request.getSession().getAttribute("urls")%>

with error

UrlEntry cannot be resolved to a type
<%=(ArrayList<UrlEntry>)request.getSession().getAttribute("urls")%>

What am I doing wrong? Should I declare UrlEntry as serializable? Maybe some problems with the constructor?

kurumkan
  • 2,635
  • 3
  • 31
  • 55

2 Answers2

4

You will have to first import the UrlEntry class in the jsp page.

<%@page import="packageName.UrlEntry"%>
Shailesh Yadav
  • 1,061
  • 1
  • 15
  • 30
3

The error is raised by the JSP compiler. It tells you that you need to add an import directive for UrlEntry to the JSP page.

wero
  • 32,544
  • 3
  • 59
  • 84