Asif Rajwani wrote:
> Hi,
> I am starting a new project and I have a very simple question.
>
> 1. Can I call the servlet from the client side which would instantiate the
> bean based on few session variables, pick up the JSP page siting on the server
> and send the jsp page back to the client as if JSP was called directly. If it
> is possible can you tell me briefly how do I do it.
>
This is exactly how I write my JSP-based applications.
Basically, any form in my app is submitted to a servlet, which does whatever
functional logic
is required, assembles the results into beans, stashes them in the request or session
(depending on how long the information needs to last), and forwards control to the JSP
page.
Thus, my servlet might have some code like this:
MyBean myBean = new MyBean(....); // Set up a bean with the answers
request.setAttribute("myBean", myBean);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/nextpage.jsp");
rd.forward(request, response);
return;
In the JSP page named "nextpage.jsp", all I have to do to access this bean is declare
it:
<jsp:useBean id="myBean" scope="request" class="MyBean" />
and the bean contains all the values put there by the servlet. In this particular
case, I
used request scope because the information was only needed to display this page. If
the
information was needed for a longer period of time, you can have your servlet store
objects
into the user's session (with session.putValue()) instead, and use scope="session"
instead of
scope="request" in the useBean element. The important thing is that both the name and
the
scope match.
This whole approach encourages you to separate your business logic (what the
application
does) from the presentation logic (how the application looks and feels). This
separation
makes it easier to change one or the other aspect without having to mess with them
both,
which can happen (for example) if you embed the business logic directly in your JSP
pages.
>
> 2. Is there any good and comprehensive resource available about JSP (book
> etc)
>
I haven't seen much yet, although I understand that several books are in progress.
One of
the books in progress has several of the initial chapters posted at JavaSoft's web
site, at:
http://java.sun.com/products/jsp/docs.html
although their form handling example sends the data to a JSP page directly, instead of
to a
servlet.
>
> Thanks.
>
> Asif
Craig McClanahan
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html