IT Community - Software Programming, Web Development and Technical Support

How to pass data (including JavaBeans) to a JSP from a servlet?

This is a discussion on How to pass data (including JavaBeans) to a JSP from a servlet? within the Java Server Pages (JSP) forums, part of the Web Development category; How to pass data (including JavaBeans) to a JSP from a servlet?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > Java Server Pages (JSP)

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-26-2007, 05:58 AM
anbuchezhians anbuchezhians is offline
D-Web Programmer
 
Join Date: Jul 2007
Posts: 77
anbuchezhians is on a distinguished road
Question How to pass data (including JavaBeans) to a JSP from a servlet?

How to pass data (including JavaBeans) to a JSP from a servlet?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-01-2007, 11:47 PM
Venkat Venkat is offline
D-Web Master
 
Join Date: Mar 2007
Posts: 350
Venkat is on a distinguished road
Default Re: How to pass data (including JavaBeans) to a JSP from a servlet?

hi
Invoking a JSP Page from a Servlet

You can invoke a JSP page from a servlet through functionality of the standard javax.servlet.RequestDispatcher interface. Complete the following steps in your code to use this mechanism:

1. Get a servlet context instance from the servlet instance:

ServletContext sc = this.getServletContext();

2. Get a request dispatcher from the servlet context instance, specifying the page-relative or application-relative path of the target JSP page as input to the getRequestDispatcher() method:

RequestDispatcher rd = sc.getRequestDispatcher("/jsp/mypage.jsp");

Prior to or during this step, you can optionally make data available to the JSP page through attributes of the HTTP request object. See "Passing Data Between a JSP Page and a Servlet" below for information.
3. Invoke the include() or forward() method of the request dispatcher, specifying the HTTP request and response objects as arguments. For example:

rd.include(request, response);

or:

rd.forward(request, response);

The functionality of these methods is similar to that of jsp:include and jsp:forward tags. The include() method only temporarily transfers control; execution returns to the invoking servlet afterward.

Note that the forward() method clears the output buffer.

Notes:

* The request and response objects would have been obtained earlier, using standard servlet functionality such as the doGet() method specified in the javax.servlet.http.HttpServlet class.

* This functionality was introduced in the servlet 2.1 specification.

Passing Data Between a JSP Page and a Servlet

The preceding section, "Invoking a JSP Page from a Servlet", notes that when you invoke a JSP page from a servlet through the request dispatcher, you can optionally pass data through the HTTP request object.

You can accomplish this using either of the following approaches:

* You can append a query string to the URL when you obtain the request dispatcher, using "?" syntax with name=value pairs. For example:

RequestDispatcher rd =
sc.getRequestDispatcher("/jsp/mypage.jsp?username=Smith");

In the target JSP page (or servlet), you can use the getParameter() method of the implicit request object to obtain the value of a parameter set in this way.
* You can use the setAttribute() method of the HTTP request object. For example:

request.setAttribute("username", "Smith");
RequestDispatcher rd = sc.getRequestDispatcher("/jsp/mypage.jsp");

In the target JSP page (or servlet), you can use the getAttribute() method of the implicit request object to obtain the value of a parameter set in this way.

Notes:

You can use the mechanisms discussed in this section instead of the jsparam tag to pass data from a JSP page to a servlet.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pass Complex Data from flash to .Net or any other server side script aramesh Flash Actionscript Programming 3 03-05-2008 10:04 PM
How to send data from servlet to javascript variables Pvinothkumar HTML, CSS and Javascript Coding Techniques 0 10-09-2007 11:05 PM
Servlet creation and servlet loading........ leoraja8 Java Programming 4 09-25-2007 12:22 AM
What is the Servlet Interface? Arun Java Programming 2 09-10-2007 04:35 AM
What is the main difference between pass-by-reference and pass-by-value? oxygen Java Programming 2 08-07-2007 01:15 AM


All times are GMT -7. The time now is 10:43 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0