|
Yes it is possible - the example code given in Websphere
(below) writes HTML out to the request object from the DataBean.
Hope this helps,
Ben Dudley
===================================================
Sylvian asked:
Hi,
You seems to be doing interesting stuff with JSP, I would like to ask you one question. Is it me or it is impossible for a bean to have access to the sessions, the request, the response, etc... objects? Regards, Sylvain. ===================================================== -----------------------------------------------------------------------------------------------------------
SERVLET that handles requests:
----------------------------------------------------------------------------------------------------------
import java.io.*;
import java.beans.Beans; import javax.servlet.*; import javax.servlet.http.*; import DataBean; /****************************************************************** * PopulateBeanServlet - This servlet creates an instance of a Bean * (DataBean), sets several of its parameters, sets the Bean instance * as an attribute in the request object, and invokes a JSP file to * format and display the Bean data. *******************************************************************/ public class PopulateBeanServlet extends HttpServlet { public void Service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { DataBean dataBean; // Create an instance of DataBean try { dataBean = (DataBean) Beans.instantiate(this.getClass().getClassLoader(), "DataBean"); } catch (Exception ex) { throw new ServletException("Can't create BEAN of class DataBean: " + ex.getMessage()); } String postcode = req.getParameter("postcode"); // Set some Bean properties
(content generation)
dataBean.setPostcode(postcode); // To send the Bean to a JSP file for content formatting and display // 1) Set the Bean as an attribute in the current request object ((com.sun.server.http.HttpServiceRequest) req).setAttribute("dataBean", dataBean); // 2) Use callPage to invoke the JSP file and pass the current request object ((com.sun.server.http.HttpServiceResponse) res).callPage("/DisplayData.jsp", req); } } /* end of class PopulateBeanServlet */ ----------------------------------------------------------------------------------------------------------------------------------
DataBean - writes to the request object
---------------------------------------------------------------------------------------------------------------------------------
import javax.servlet.http.*;
import java.io.PrintWriter; import java.io.IOException; public class DataBean extends HttpServlet {
String Prop1 = "DefaultValueofProp1";
public void service(HttpServletRequest req, HttpServletResponse res) throws IOException { PrintWriter writer = res.getWriter(); writer.println("<BR><B>This is output from DataBean</B><P>"); } public void setProp1(String Prop1) { this.Prop1 = Prop1; } public String getProp1() { return Prop1; } } ------------------------------------------------------------------------------------------------------------------
JSP output
----------------------------------------------------------------------------------------------------------------
DisplayData.jsp sample
<!-- This JSP file gets a Bean passed in a request object and displays the Bean properties --> <html> <head> <title>Bean Data Display</title> </head> <!-- Get the Bean using the BEAN tag --> <bean name="dataBean" type="DataBean" introspect="no" create="no" scope="request"> </bean> <body> <!-- There are three ways to access Bean properties --> <!-- Using a JSP scriptlet --> <% out.println("The value of Bean property 1 is " + dataBeans.getProp1()); %> <!-- Using a JSP expression --> <p>The value of Bean property 2 is <%= dataBean.getProp2() %> </p> <-- Using the INSERT tag --> <p>The value of Bean property 3 is <insert bean=dataBean property=prop3 default="No property value" > </insert></p> </body> </html> ----------------------------------------------------------------------------------------------------- |
- Bean Database connection Ben Dudley
- Re: Bean Database connection Chandrasekhar Naidu
- Re: Bean Database connection Shicoff Brian
- JRun and Apache Oliver Kaljuvee
- Re: Bean Database Connection Ben Dudley
- Re: Bean Database Connection Ben Dudley
- Re: Bean Database Connection Sylvain St-Germain
