Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Session attribute on jstl

Tags:

jsp

jstl

I am trying to access a session attribute from a jsp page which is set and dispatched by a servlet, but I am getting the error message "jsp:attribute must be the subelement of a standard or custom action". What could be wrong, am I accessing it incorrectly? The following is the code snippet.


Servlet:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    HttpSession session = request.getSession(); 
    session.setAttribute("Questions", getQuestion());
    System.out.println(session.getAttribute("Questions"));
    RequestDispatcher req = request.getRequestDispatcher("DisplayQuestions.jsp");
    req.forward(request, response);
}

private QuestionBookDAO getQuestion(){
    QuestionBookDAO q = new QuestionBookDAO();
    q.setQuestion("First Question");
    q.setQuestionPaperID(100210);
    q.setSubTopic("Java");
    q.setTopic("Threads");
    return q;
}

I am able to set the session attribute successfully. But when I try to access the same in my jsp file (below), I am getting a runtime error

    <jsp:useBean id="Questions" type="com.cet.evaluation.QuestionBook" scope="session">
    <jsp:getProperty property="Questions" name="questionPaperID"/>
    <jsp:getProperty property="Questions" name="question"/>
    </jsp:useBean>

The bean QuestionBook contains two private variables questionPaperID and question I run the application on Tomcat and below is the error thrown.

type Exception report

message 

    description The server encountered an internal error () that prevented it from fulfilling this request.

    exception 

    org.apache.jasper.JasperException: /DisplayQuestions.jsp(15,11) jsp:attribute must be the subelement of a standard or custom action
        org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
        org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
        org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:88)
        org.apache.jasper.compiler.Parser.parseStandardAction(Parser.java:1160)
        org.apache.jasper.compiler.Parser.parseElements(Parser.java:1461)
        org.apache.jasper.compiler.Parser.parseBody(Parser.java:1670)
        org.apache.jasper.compiler.Parser.parseOptionalBody(Parser.java:1020)
            ....
like image 899
Vijay Selvaraj Avatar asked Feb 03 '11 17:02

Vijay Selvaraj


2 Answers

You should definitely avoid using <jsp:...> tags. They're relics from the past and should always be avoided now.

Use the JSTL.

Now, wether you use the JSTL or any other tag library, accessing to a bean property needs your bean to have this property. A property is not a private instance variable. It's an information accessible via a public getter (and setter, if the property is writable). To access the questionPaperID property, you thus need to have a

public SomeType getQuestionPaperID() {
    //...
}

method in your bean.

Once you have that, you can display the value of this property using this code :

<c:out value="${Questions.questionPaperID}" />

or, to specifically target the session scoped attributes (in case of conflicts between scopes) :

<c:out value="${sessionScope.Questions.questionPaperID}" />

Finally, I encourage you to name scope attributes as Java variables : starting with a lowercase letter.

like image 66
JB Nizet Avatar answered Sep 20 '22 09:09

JB Nizet


You don't need the jsp:useBean to set the model if you already have a controller which prepared the model.

Just access it plain by EL:

<p>${Questions.questionPaperID}</p>
<p>${Questions.question}</p>

or by JSTL <c:out> tag if you'd like to HTML-escape the values or when you're still working on legacy Servlet 2.3 containers or older when EL wasn't supported in template text yet:

<p><c:out value="${Questions.questionPaperID}" /></p>
<p><c:out value="${Questions.question}" /></p>

See also:

  • Our Servlets wiki page

Unrelated to the problem, the normal practice is by the way to start attribute name with a lowercase, like you do with normal variable names.

session.setAttribute("questions", questions);

and alter EL accordingly to use ${questions}.

Also note that you don't have any JSTL tag in your code. It's all plain JSP.

like image 42
BalusC Avatar answered Sep 20 '22 09:09

BalusC