Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting a parameter from a JSP page dynamically?  RSS feed

 
Jurgen Malinao
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm displaying a 3-column table with the column names ID, name, and salary. I made the ID into a link to go to the EditServlet class. What I'm trying to do is figure out which ID# was clicked to get onto the servlet page, any idea how to implement that dynamically? I know that if you put something like ?x=1 and then getParameter("x") on the servlet page would work, but then all the IDs would have the same parameters since I'm using a for loop to print out my ArrayList of objects. Any help would be appreciated, my code for the jsp part is below.

for (int count=0; count<list.size(); count++)
{
%>

<tr>
<td> <a href="EditServlet"><%= list.get(count).id %></a>
<td> <%= list.get(count).name %>
<td> <%= "$" +list.get(count).salary %>
</tr>

<br>
<%
}
%>
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jurgen Malinao wrote:I know that if you put something like ?x=1 and then getParameter("x") on the servlet page would work, but then all the IDs would have the same parameters since I'm using a for loop to print out my ArrayList of objects.


Hi Jurgen, welcome to the Ranch!

Yes, that's right. You want to pass a parameter back from the HTML to identify which thing in the list you want to work with. And do try to think of it as HTML, because that's what you are working with. The JSP generates HTML which appears in the browser, so there's no "dynamically" available anywhere. So figure out exactly what HTML you want to generate, then write a JSP which generates that.

As for why the parameter would always be the same because you're using a for-loop, I don't understand that at all. The value of the parameter should be something which identifies the thing in the list, not a constant. (Sorry I keep having to say "thing" but your variable name "list" is not very helpful for understanding the application.)

And as for the for-loop, why is it there at all? Scriptlets in JSP have been obsolete for a decade now. You should be using JSTL (which has looping constructs) and EL in any modern JSP.

 
Jurgen Malinao
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm doing on-the-job training and our instructor just taught us scriplets, that's why I'm using them. He might think a good background of scriplets might be useful in the future or something.

But my list is an ArrayList of the object Employee where e.id = "101", e.name = "Jimmy" and e.salary = "5000" (examples). I don't really know how to describe what I'm looking for so maybe a demonstration will be better.


ID Name Salary
101 Jimmy 5000
102 Jenna 6000


That's a table on my jsp page, with the ID numbers hyperlinked to the EditServlet class. All the values in the table are coming from a database (Oracle). What I want to know is, when a user clicks an ID number like 101, is there a way for my servlet to know which ID# was clicked so that I can use that information to figure out which employee I have to edit in the database. The EditServlet is supposed to be for editing employee details, if that helps any.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Like you already said. Pass the ID as a parameter of the URL.
 
Jurgen Malinao
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I already tried that. I used this but it always returns as null.

String value = request.getParameter("id");
System.out.println(value);
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. Then the thing to do is to look at the HTML from which that request was submitted. See if the form or URL correctly specifies the "id" parameter. In case you didn't already know, you view the HTML in your browser by right-clicking and selecting "View Source".
 
Jurgen Malinao
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my entire code for the jsp sending the request to the EditServlet


<%@page import="java.util.ArrayList"%>
<%@page import="com.Employee"%>

<form action="EditServlet">
<table width="25%" border="1">

<tr>
<td> ID
<td> Name
<td> Salary
</tr>

<%
ArrayList<Employee> list = (ArrayList<Employee>) request.getAttribute("key");

for (int count=0; count<list.size(); count++)
{
%>

<tr>
<td> <a href="EditServlet" id="id"><%= list.get(count).id %></a>
<td> <%= list.get(count).name %>
<td> <%= "$" +list.get(count).salary %>
</tr>

<br>
<%
}
%>

</table>
</form>



I try to run

String value = request.getParameter("id");
System.out.println(value);

in the doGet() method of the servlet and it always gives me a null.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And did you do what I suggested?
 
Jurgen Malinao
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep. This was the page source:



<form action="EditServlet">
<table width="25%" border="1">

<tr>
<td> ID
<td> Name
<td> Salary
</tr>



<tr>
<td> <a href="EditServlet" id="id">102</a>
<td> Jimmy
<td> $1440
</tr>

<br>


<tr>
<td> <a href="EditServlet" id="id">103</a>
<td> Bob
<td> $2880
</tr>

<br>


<tr>
<td> <a href="EditServlet" id="id">105</a>
<td> Katherine
<td> $1200
</tr>

<br>


<tr>
<td> <a href="EditServlet" id="id">107</a>
<td> Jesslyn
<td> $960
</tr>

<br>


<tr>
<td> <a href="EditServlet" id="id">109</a>
<td> Rebecca
<td> $1080
</tr>

<br>


</table>
</form>
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good. You can see the URL which is requesting your servlet:

href="EditServlet"


And you can see that it doesn't have an "id" parameter, right? In your original post you said

I know that if you put something like ?x=1 and then getParameter("x") on the servlet page would work...


Which is correct, as I said back then. But your HTML doesn't have that. So your first step should be to work out what you need your HTML to look like instead of what you have now.
 
Jurgen Malinao
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I took the "?x=1" part from the hyperlink because I want the id parameter to change dynamically, if that is possible. Like if I used <a href="EditServlet?x=1"> in my for loop below, all the entries in the table would have the same parameters and I wouldn't know which one was clicked.

Since I'm using a for loop to display the contents of my table, is it possible to use the int count part of the for loop in the hyperlink in any way? That way it can give the url a different id for each time the loop was ran.

I know this one doesn't work, but it's basically what I'm trying to do:


for (int count=0; count<list.size(); count++)
{
<a href="EditServlet?x=count?"><%= list.get(count).id %></a>
<%= list.get(count).name %>
<%= "$" +list.get(count).salary %>
}


I took away the formatting so it'll be easier to read.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're still going about it backwards. Instead of asking whether you can write code to produce the HTML you want, you should start by deciding what HTML you want. Then you write code to produce that. So... what would you like to see in your HTML.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!