Reputation: 9363
I have two pages (page1.jsp and page2.jsp). in page1.jsp i retrieve some values from database and display hyperlinks corresponding to those values. Now what i want is when i click any of those hyperlinks, in page2.jsp i should display other fields coressponding to the vlaue from the database. So what i essentially want is when a link is clicked the associated value must be passed to page2.jsp where i query the database to retrieve other fields.
Upvotes: 1
Views: 11118
Reputation: 1108632
when a link is clicked the associated value must be passed to page2.jsp where i query the database to retrieve other fields
So, you want to preprocess the HTTP request before displaying the JSP? For that you need a servlet.
Assuming that the links in page1.jsp
are displayed like follows:
<ul>
<c:forEach items="${ids}" var="id">
<li><a href="page2?id=${id}">View data for ID ${id}</a></li>
</c:forEach>
</ul>
Then you will be able to preprocess the HTTP request in the doGet()
method of a HttpServlet
which is listening on an <url-pattern>
of /page2
:
Long id = Long.valueOf(request.getParameter("id"));
Data data = dataDAO.find(id);
request.setAttribute("data", data); // Will be available as ${data} in JSP.
request.getRequestDispatcher("/WEB-INF/page2.jsp").forward(request, response);
In page2.jsp
you will be able to access the data
as follows:
<p>The data for ID ${param.id} is:</p>
<p>Field 1: ${data.field1}</p>
<p>Field 2: ${data.field2}</p>
Upvotes: 1
Reputation: 92274
lIf you are talking about a variable that can't be serialized as a string and passed as an http parameter, as suggested, then you need to store the variable within the session.
--page1.asp
<?
session.setAttribute( "val", record );
?>
--page2.asp
<?
//Cast this to your type
Object record = session.getAttribute("val");
// Clean it up now that you're done
session.removeAttribute( "val" )
?>
Upvotes: 0
Reputation: 12269
You can do that by either passing GET or POST variables to the corresponding jsp page and reading them using jsp:
using GET method:
<a href="page2.jsp?datatobesend=value">link</a>
If you want to do this dynamically you will have to add to that on click using javascript:
GET using jQuery js library:
<script>
$(document).ready(function(){
$('#mylink').click(function(){
$(this).attr('href', $(this).attr('href')+'?datatobesend=value');
});
});
</script>
<a href="page2.jsp" id="mylink">link</a>
There are many ways to use GET/POST to send data from one page to another, this is just as quick as I could write one up :-) Using form submissions are another place you should look into if this way doesn't work out for you.
Upvotes: 0