简体   繁体   中英

how to assign a value to a variable fetched from java bean over JSP?

I am working on JSP and servlets. I need to fetch values from java bean and assign it some other variable over JSP.

Usually I fetch the value in html tags as ${abcd.variable_name}

but this thing can't be used it we want to get some value in <% %>

That depends on where the bean is stored. If it is stored in the request scope as a request attribute, just get it back as request attribute:

<%
    Bean bean = (Bean) request.getAttribute("bean");
    // ...
%>

Or if it's stored in the session scope as a session attribute, just get it back as session attribute:

<%
    Bean bean = (Bean) session.getAttribute("bean");
    // ...
%>

Or if it's stored in the application scope as an application attribute, just get it back as application attribute:

<%
    Bean bean = (Bean) application.getAttribute("bean");
    // ...
%>

However, you're doing the desired job at the wrong place. It has to be done in a normal Java class like a servlet or at least the action class of the MVC framework you're using, if any.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM