File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes how to print values on jsp page fetched from servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "how to print values on jsp page fetched from servlet" Watch "how to print values on jsp page fetched from servlet" New topic
Author

how to print values on jsp page fetched from servlet

Megha Singhal
Ranch Hand

Joined: Feb 28, 2012
Posts: 182

i have fetched the like following using result set in servlet


now i want put these variables in jsp page like
<tr>name="a"</tr>

how can i bring these variables from servlet page to jsp page so that data stored in these variables can print on jsp page.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 980
    
    2
by using request.getParameter();
Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196
Hi,

You have to put the object or values into request attribute in your servlet and then get the same request attribute on jsp page.

Servlet

request.setAttribute("NAME", a);
request.setAttribute("SECN", b);
request.setAttribute("TITLE", d);

JSP

<tr>name="<%= request.getAttribute("Name") %>"</tr>
<tr>secn="<%= request.getAttribute("SECN") %>"</tr>
<tr>title="<%= request.getAttribute("TITLE") %>"</tr>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60802
    
  65

Punit Jain wrote:by using request.getParameter();

Incorrect. That's for fetching request parameters.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60802
    
  65

Piyush Mangal wrote:
JSP

<tr>name="<%= request.getAttribute("Name") %>"</tr>
<tr>secn="<%= request.getAttribute("SECN") %>"</tr>
<tr>title="<%= request.getAttribute("TITLE") %>"</tr>

Bad practice! Scriptletes in a JSP has been discredited for 10 years! A whole decade! Do you not think it's time to catch up? :rolleyes:

Use the EL and JSTL, not Java scriptlets.

For example: <c:out value="${name}"/>
Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196
Bear Bibeault wrote:
Piyush Mangal wrote:
JSP

<tr>name="<%= request.getAttribute("Name") %>"</tr>
<tr>secn="<%= request.getAttribute("SECN") %>"</tr>
<tr>title="<%= request.getAttribute("TITLE") %>"</tr>

Bad practice! Scriptletes in a JSP has been discredited for 10 years! A whole decade! Do you not think it's time to catch up? :rolleyes:

Use the EL and JSTL, not Java scriptlets.

For example: <c:out value="${name}"/>



My Bad. Yes you are correct , we should not be using scriptlet any more.
pravin venkat
Greenhorn

Joined: Mar 30, 2012
Posts: 20
I think this would suffice. :)

Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196
pravin venkat wrote:JSTL Implementation



It is a bad practice to put sql statement in JSP itself. We should follow MVC architecture where JSP should only be used for displaying values only and not be responsible for fetching it from database directly.
pravin venkat
Greenhorn

Joined: Mar 30, 2012
Posts: 20
then, i think these two code abstracts will do the job..






Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196
pravin venkat wrote:then, i think these two code abstracts will do the job..








Ideally Servlet should be responsible for setting the attribute in Request or Session scope based on the use case and forward the request to jsp.
JSP should only use EL to display them on screen.

pravin venkat
Greenhorn

Joined: Mar 30, 2012
Posts: 20
Piyush Mangal wrote:
pravin venkat wrote:then, i think these two code abstracts will do the job..








Ideally Servlet should be responsible for setting the attribute in Request or Session scope based on the use case and forward the request to jsp.
JSP should only use EL to display them on screen.



would you be able to come up with other ways of doing this?
Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196


All I am saying is that the above code is incorrect and redundant. You can always set attributes in session scope through your controller and dispatch the request to JSP.

pravin venkat
Greenhorn

Joined: Mar 30, 2012
Posts: 20
Piyush Mangal wrote:

All I am saying is that the above code is incorrect and redundant. You can always set attributes in session scope through your controller and dispatch the request to JSP.



if possible will you be able to example code?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60802
    
  65

Piyush Mangal wrote:

All I am saying is that the above code is incorrect and redundant. You can always set attributes in session scope through your controller and dispatch the request to JSP.



Piyush is absolutely correct. The set of <c:set> tags are completely useless, and in fact, could be harmful. All they do is to copy scoped variables that are already in scope into new scoped variables. And on the session! No session storage is needed in this example.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60802
    
  65

pravin venkat wrote:if possible will you be able to example code?


If the scoped variable Name is present in request scope, it can be displayed with one of:

${Name}

or

<c:out value="${Name}"/>

depending upon whether HTML-encoding is needed or not (to protect against XSS attacks).

No <c:set> tags are needed.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60802
    
  65

P.S. Scoped variable names should follow the convention for Java variables, so "Name" is a poor choice. It should be name.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60802
    
  65

Piyush Mangal wrote:
It is a bad practice to put sql statement in JSP itself. We should follow MVC architecture where JSP should only be used for displaying values only and not be responsible for fetching it from database directly.


Piyush is correct on this as well. Even the JSTL Specification says not to use the SQL tags in production code.
pravin venkat
Greenhorn

Joined: Mar 30, 2012
Posts: 20
Bear Bibeault wrote:
pravin venkat wrote:if possible will you be able to example code?


If the scoped variable Name is present in request scope, it can be displayed with one of:

${Name}

or

<c:out value="${Name}"/>

depending upon whether HTML-encoding is needed or not (to protect against XSS attacks).

No <c:set> tags are needed.


thank you. now i understand :)
pravin venkat
Greenhorn

Joined: Mar 30, 2012
Posts: 20
Piyush Mangal wrote:

All I am saying is that the above code is incorrect and redundant. You can always set attributes in session scope through your controller and dispatch the request to JSP.



thank you, i understand it now :)
Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196
Hi Pravin, Please find below the sample code. Please note that this is not complete code and is only for your understanding.

JSP




Servlet





pravin venkat
Greenhorn

Joined: Mar 30, 2012
Posts: 20
Piyush Mangal wrote:Hi Pravin, Please find below the sample code. Please note that this is not complete code and is only for your understanding.

JSP




Servlet







thank you, Piyush :thumbup:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to print values on jsp page fetched from servlet
 
Similar Threads
Looping through records while looping through record
I M NOT ABLE TO DEFINE VARIABLES IN JSP.
Using ResultSet
how to use jsp array in javascript?
resultset