Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to print values on jsp page fetched from servlet

 
Megha Singhal
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
by using request.getParameter();
 
Piyush Mangal
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64835
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Punit Jain wrote:by using request.getParameter();

Incorrect. That's for fetching request parameters.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64835
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this would suffice. :)

 
Piyush Mangal
Ranch Hand
Posts: 196
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then, i think these two code abstracts will do the job..






 
Piyush Mangal
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 196
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64835
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64835
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64835
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64835
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic