Session attribute not working with useBean value

613 Views Asked by At

I am currently creating a web application use java servlets and jspx pages. I have created a java class with a parameter when called with the class being LessonSelection(int owner). I know that you cannot use a bean with a parameter so I made an object of that class with the parameter and then added it to the session, then used that session attribute in the useBean.

I have an error coming up saying 'The value of the useBean class attribute ${selected} is invalid'

The useBean code is below.

<jsp:useBean class="${selected}" id="timetable" scope="session"/>

The java code is below.

HttpSession session = request.getSession(true);
session.setAttribute("username", user);
session.setAttribute("id", id);
selected = new LessonSelection(id);
session.setAttribute("selected", selected);
dispatcher = this.getServletContext().getRequestDispatcher("/LessonTimetableView.jspx");

If you need any more of the code that I wrote, just ask thanks.

EDIT 1:

I thought I would check whether it is actually added to the session attribute and it is added. It is printed as 'model.LessonSelection@1457de3'. It still shows the same error.

1

There are 1 best solutions below

8
On

The class at jsp:useBean must be the package + class of your attribute! Suppose your LessonSelection is located in the package com.test. The code would be as follow:

<jsp:useBean class="com.test.LessonSelection" id="timetable" scope="session"/>

Edit 1

Try this:

<jsp:useBean type="com.test.LessonSelection" id="timetable" scope="session"/>

It seens that using type, the JSP wont instantiate it for your, it'll just look from the bean of the given type in the given scope. See it here.