1

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 <% %>

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Dhruv
  • 1,668
  • 9
  • 29
  • 40

1 Answers1

0

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.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Masterreportrequest.java is my servlet and Buildingmasterservice.java is my bean class. In servlet this is how i forward variables - request.setAttribute("buildmast", building_master); so should i call bean class or servlet in and then further how to write my Bean bean = (Bean) request.getAttribute("bean"); – Dhruv Oct 19 '11 at 19:47
  • 1
    You don't need `` at all when you're already using servlets. It would only duplicate and confuse everything. You have stored it as a request attribute with name `"buildmast"` in the servlet, so you should also get it as such: `BuildingMaster buildingMaster = (BuildingMaster) request.getAttribute("buildmast");`. – BalusC Oct 19 '11 at 19:53
  • Actually i had tried this but it show an error in this line saying "BuildingMaster cannot be resolved to a type" – Dhruv Oct 19 '11 at 20:26
  • You've to import it by `<%@page import="com.example.BuildingMaster" %>` in top of JSP. By the way, why not just doing the preparing job in the servlet? You keep fiddling with ugly and old fashioned *scriptlets* while you're already using a servlet which can just prepare exactly the data you need for the JSP. – BalusC Oct 19 '11 at 20:34