aspose file tools*
The moose likes JSP and the fly likes Knowning which link was clicked (take 2) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Knowning which link was clicked (take 2)" Watch "Knowning which link was clicked (take 2)" New topic
Author

Knowning which link was clicked (take 2)

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1083
My previous posting may have been difficult to understand on how to know what link was clicked, but what I'm trying to figure out is the following.
Say you have a hyperlink list that's built in a loop. The number of items in the list can vary based on the array that you use to build the list. The text for each link is created based on a database lookup.
Each element in the list is some "Master" information. Clicking on any of the links in the list would take you to another JSP page, where I want to display "Detail" information for the link clicked in the previous JSP page.
Therefore, my (basic?) question is how can you send the next JSP page specific information about the link that was clicked to call it?
The Master list might have record numbers in it or something like that (but these get built dynamically when the list is built). When the user clicks one of these links and we go to the next JSP page, I'll need to somehow get this record number (or other hyperlink text based on the link clicked) and do a database lookup so I can display the detail.
In all cases, the links will have text based on records in a database built in a loop. Clicking these links should take me to another page where I can tell exactly what link was clicked.
Is this possible?
Thanks in advance for any ideas.
-- Mike
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Mike
It sounds like what you need is a controller servlet. In your JSP you build your list of links that all reference the same page but for each link include something in the query string (the part after the ?) to differentiate between the links.
You could then have a list of links like this:
<a href="controller?nextPage=page1&id=id">page 1</a>
<a href="controller?nextPage=page2&id=id">page 2</a>
<a href="controller?nextPage=page3&id=id">page 3</a>
... and so on
Then in your controller servlet you check the values sent in the query to determine what page is actually invoked. In the examples above the values of nextPage and id would be determined in your JSP that creates the list initially.
Imagine you have a list of employees in a database that you want to allow people to edit or delete. In a JSP you could create a list of them by getting the name and employee id from the database then create a list like this:
John Smith <a href="controller?nextPage=edit&id=1">edit</a> <a href="controller?nextPage=delete&id=1">edit</a>
<p>
Sally Jones<a href="controller?nextPage=edit&id=2">edit</a> <a href="controller?nextPage=delete&id=2">edit</a>
<p>
Roger Rabbit<a href="controller?nextPage=edit&id=3">edit</a> <a href="controller?nextPage=delete&id=3">edit</a>
...
Then in your controller it checks the value of the nextPage parameter and then puts the id into a session variable and forwards to the appropriate page.
Is that what you're looking for? Hope it helps.


Dave
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1083
I'm not sure this idea will work, but maybe I'm not quite understanding you.
My technical problem as I see it is that the links will be built in a loop based on what's in the array, not text I can manually add to the link (in the JSP page) to differentiate it for some other page.
Your example used text manually inserted into each link. That's fine, but not the situation I have.
In my case, each link in my case would be named (that is, the link's text you click on) based on an array's contents -- at runtime.
Therefore, I need a way to pass a *variable* to the next JSP page (the variable would hold dynamically-built link's text). Then, in the next page, I can use the link's text to do a lookup on a table or for display ("You picked record 3", for example -- where "3" was the hyperlink's text and the message appeared in the next page).
There could also be hundreds of items in the list all built dynamically at runtime.
It sounds like this is a difficult task and I'll need to look at another approach.
Do you agree?
Thanks in advance.
sim sim
Ranch Hand

Joined: Jun 05, 2002
Posts: 55
HI Mike,
Dave is very much right and that will work.
You are generating some links dynamically based on a DB result. Instead of jumping from that JSP to another JSP, jump to controller servlet which will decide based on ur link, to which JSP it has to take you .
Lets say, the DB result u are getting is 1, 2, 3...and so on
the links you probably would be creating are
<a href= 'MyControlerServlet?id='+rs.getString(1)<a href= 'MyControlerServlet?id='+rs.getString(2)>
<a href= 'MyControlerServlet?id='+rs.getString(3)>
in the servlet you can verify whether the req is coming from link 1 or 2 or 3 or whatever id it may be, based on which you can forward to rquired jsp.
Hope this is clear
Regards,
Sim Sim
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1083
Yes, I thnk I understand.
I'll experiement with a servlet and post my findings.
Thank you.
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1083
These examples all assumed you could pass a static number to a servlet.
However, in my case, the links are built in a loop using a loop variable i.
For example, if the i=0 element of the array that is used to build this hyper link list has a value "1", then the hyperlink would say "1" (underlined in blue, of course).
However, by the time the entire list is built, i, the loop variable, is now some possibly huge number.
So, when they click on "1" (the hyperlink), I don't have a variable 'i' can send anywhere. The only text with the correct reference is the hyperlink text itself.
The hyperlink text is built in a loop using syntax like this:
objectArray[i].getDBfield1Data().toUpperCase()
When they click on this link, i is useless since it's already been incremented to create an arbitrary number of other list items.
Even a servlet doesn't appear to solve this problem.
Sorry if I'm dense, but all the examples provided don't seem to address the fundamental problem I'm having.
Thanks in advance... <s>
-- Mike
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Mike
Lets do this step by step, I'm not sure I understand what it is your doing.
The test of the hyperlink can be different from the actual value of the href in the link itself, so the underlined text that the user clicks can be something meaningful while th actual href can contain whatever values you need to send to the controller.
IT sounds like what you might need is some sort of HashMap or HashTable to hold the values you need to access along with the key used to access them. Or another alternative is to just out the array you create to make the links into the session so that the controller can just access it by index. that way it doesn't matter what the contents of the link are just so long as you have a way of matching that value to the data you need to get at.
What is actually returned from the code you posted?
objectArray[i].getDBfield1Data().toUpperCase()
Can you post the entire code that generates the links, along with an example of what the contents of a ResultSet row would look like?
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1083
HI Dave,
Thanks for your reply. Full source code below for JSP page.
The text of the hyperlink from something like
objectArray[i].getDBfield1Data().toUpperCase()
resolveds to a string from one field in one record of a table in a database.
For example, lets say you have a table called Process. In the table process, you have a "PK" field, which is a unique row number (I know you understand all these concepts, but I'm just trying to go step by step <s> .
A bean that does the database lookup builds a list of array objects. Each object in the array is just a "class" with three fields (first field is for the table to search, and the other two fields are fields in that table to get field information from).
So, in every element of the array, each of these three fields holds one record with the string for the two fields in this record (plus, the table name in the first field).
The loop above creates a hyperlink based on the contents of the field's data from the array filled in by the bean.
So when the user clicks a link (Master info), I want to display the detail information for the actual link clicked in either another JSP page, a servlet, or what/where ever.
So, it seems like I have to somehow send the link's text to the next JSP page, servlet or what/where ever.
Here's the entire JSP code. I appreciate your patience and help!!! <s>
--------------------------------------------------
JSP CODE:
<%@ page import="dbaccess.DBFields" %>
<jsp:useBean id="SQLData" scope="application" class="dbaccess.DBAccess1" />
<FORM ACTION="http://localhost:8081/DBAccess_3.jsp" METHOD="POST">
</body>
<h1>
Display Messij Database Table Info.
</h1>
<p>
<jsp:setProperty name="SQLData"
property="tableName"
value= '<%= request.getParameter("tableName") %>'/>
<jsp:setProperty name="SQLData"
property="pkFieldName"
value='<%= request.getParameter("pkNameField")%>'/>
<jsp:setProperty name="SQLData"
property="nameField"
value='<%= request.getParameter("nameField") %>'/>

<% DBFields[] objectArray = SQLData.getDBFields();%>
<% out.println("<B> Table Name=</B> " + SQLData.getTableName()); %>
<p>
<% String tableName = request.getParameter("tableName"); %>
<%session.setAttribute( "theTableName", tableName );%>
<% String pkNameField = request.getParameter("pkNameField"); %>
<%session.setAttribute( "thepkNameField", pkNameField );%>
<% String nameField = request.getParameter("nameField"); %>
<%session.setAttribute( "theNameField", nameField );%>

<p>Name field entered: <jsp:getProperty name="SQLData" property="nameField" />
<p>Primary key entered, <jsp:getProperty name="SQLData" property="pkFieldName" /><br>
<p>
<hr><br>
<UL COMPACT TYPE=SQUARE>
<%
if ( objectArray != null)
{
for (int i = 0; i < objectArray.length; i++)
{
out.println("<p><L1>Table Name: <A HREF='http://localhost:8081/DBAccess_3.jsp?id=1'/>" + objectArray[i].getDBfield1Data().toUpperCase()+ "</A>") ;
out.println("<p><L1>Name Field: <A HREF='http://localhost:8081/DBAccess_3.jsp?id=2'/>" + objectArray[i].getDBfield2Data().toUpperCase()+ "</A>") ;
out.println("<p><L1>Primary Key: <A HREF='http://localhost:8081/DBAccess_3.jsp?id=3'/>"+ objectArray[i].getDBfield3Data().toUpperCase()+ "</A>");
out.println("<p> ---------------------------------------------------------- <p>");
}

out.println("<p>"+ objectArray.length + " Records Retrieved.");
}
else
out.println("<p><H2> Sorry, no records found for your entered query. </H2> ");
%>
</UL>
</BODY>
------------------------------------------------
>>>> End of JSP code <<<<
------------------------------------------------
If I click one of the records (hyperlinks) created above, I would get the following response form the next JSP page (assuming I click the "PK" link, which is the id=1 hyperlink above:
Greetings from DBAccess_3.JSP
You want to get all records by Primary Key Field.
-------------------------------------------------
Table Name Passed -- Process
Name Field Passed -- Pro_Name
PK Field Passed -- PRO_PK
-----------------------------------------------
END of JSP output
------------------------------------------------
Here, based on "id=" field, I have identified which of the three hyperlink "Types" they clicked. I have identified the three fields returned from the bean from the previous JSP page, but I still don't know which link (record number, etc.) from the array they clicked on....so I can't do a database lookup for the actual item they clicked on to give them detail.
Does this extra detail help?
I really appreciate your support.
-- Mike
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Mike
It seems to me, if I understand correctly, that you need to find out which record the user wants to look up based on which link they clicked. For each record they can look it up in one of three ways/types (identified with the id parameter in the query) - am I right so far?
The actual record ID is gotten with the code:
objectArray[i].getDBfield1Data().toUpperCase()
that returns a string. You need to find a way to pass this, along with the id, to the next page in the app, right?
I an think of two ways to do it:
The first would be to just add another name/value pair to the query string in the href, like this:
out.println("<p><L1>Table Name: <A HREF='http://localhost:8081/DBAccess_3.jsp?id=1&record=" + objectArray[i].getDBfield1Data().toUpperCase()+ "'/>" + objectArray[i].getDBfield1Data().toUpperCase()+ "</A>");
Now in the next JSP/servlet you can access that info by calling getParameter("record").
The second way would be to store objectArray in the session and then just add the value of the current index to the href, like this:
out.println("<p><L1>Table Name: <A HREF='http://localhost:8081/DBAccess_3.jsp?id=1&record=" + i + "'/>" + objectArray[i].getDBfield1Data().toUpperCase()+ "</A>");
then in the next page just find out what i is then access that element of the array and you've got the entire object.
I hope that is what you're looking for
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1083
SUCCESS!!!1
I was surprised since the new hyperlink (that is, the new part: &FTable=" + objectArray[i].getDBfield1Data().toUpperCase()+ "'/>" + objectArray[i].getDBfield1Data().toUpperCase()),
actually sent the *value of i* and not a *reference to i* when the list was done.
I had been afraid that 'i' would not be usable once the list was complete since it had been incremented along the way and was now at "EOF" so to speak.
Fortunately, this wasn't the case.
Therefore, I can now click on any link and transmit the text string of the link
THANK YOU!!!
-- Mike
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
I'm happy to have helped, glad it worked for you!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Knowning which link was clicked (take 2)