Results 1 to 2 of 2
  1. #1
    infamousKTF is offline Member
    Join Date
    Mar 2014
    Posts
    1
    Rep Power
    0

    Exclamation Putting a dynamic parameter on a hyperlink?

    Hey guys,

    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.


    XML Code:
    <%@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?x=   1   "><%= list.get(count).id %></a>
    	<td> <%= list.get(count).name %>
    	<td> <%= "$" +list.get(count).salary %>
    </tr>
    
    <br>
    <%
    }
    %>
    
    </table>
    </form>

    And I'm using this to get the parameter:

    XML Code:
    String value = request.getParameter("x");
    System.out.println(value);

    So instead of ?x=1, can I put a dynamic variable (like int count in the for loop) inside the URL?

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    354
    Rep Power
    5

    Default Re: Putting a dynamic parameter on a hyperlink?

    Your best bet is to use the MVC(Model View Controller) approach. Using scriptlets in a JSP 'breaks' the MVC architecture most of the time, when you are taking the approach that you have.

    Use a Servlet as your controller layer, and add an employees attribute to the request.
    Then, in your JSP, all you have to do is use Expression Langauge to retrieve the attributes from that object in a cleaner fashion.
    The scriptlet that you have that creates the Array List of employees, for example, should really be done in a Servlet, and just let the JSP be responsible for the 'view' portion.

    If you want to do it the way you have it, you can:

    Java Code:
    <td><a href="EditServlet?x=<%= list.get(count).id %>"><%= list.get(count).id %></td>
    It is messy, which is why you probably want to use a servlet to do your java work, and then use the JSP for display purposes.

    An example of how you could use MVC would be to, in your Servlet

    Java Code:
    public class MyServlet extends HttpServlet {
    
    //inside doPost or doGet
    //get the list of employees (from your database, or wherever they come from
    ArrayList<Employee> employeeList = ........
    //Set the employees list in the request as an attribute, so you can retrieve it in the jsp
    request.setAttribute("employeesList",employeeList);
    //forward to your jsp
    request.getRequestDispatcher("path/to/your/jsp").forward(request,response);
    
    }
    Then, in your jsp, you can use expression language to easily display your list. The key though is that your Employee objects have to follow the bean spec, basically meaning that for the attributes that you want to access, you have to provide getters that follow the casing conventions. So if, for example your Employee class has a salary attribute, you would need to provide a getter:

    Java Code:
    public double getSalary(){
      return salary;
    }
    This allows you to have your jsp code be more dynamic, in that you can test for an empty list of employees, and maybe display some sort of message if there are no employees:
    Java Code:
    <c:choose>
       <c:when test="${empty employeesList}">
          <h1>No Employees Founds</h1>
       </c:when>
       <c:otherwise>
         <c:forEach var="currentEmployee" items="${employeesList}" >
            <h3>${currentEmployee.salary></h3>
         <c:/forEach>
       </c:otherwise>
    </c:choose>
    You can put the employees in your table structure. The key is that you've separated the controller from the view, and made the JSP code easier to read/maintain.

    Does that help?
    Last edited by sehudson; 05-19-2014 at 09:44 PM.

Similar Threads

  1. Replies: 3
    Last Post: 02-11-2013, 07:57 PM
  2. Reflection - Dynamic parameter types
    By viper2843 in forum New To Java
    Replies: 3
    Last Post: 01-29-2013, 06:48 PM
  3. Getting the hyperlink URL
    By leifster in forum New To Java
    Replies: 1
    Last Post: 11-27-2010, 05:01 PM
  4. get data from hyperlink
    By shyameni in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 02-19-2010, 10:32 AM
  5. [SOLVED] Hyperlink
    By Mir in forum New To Java
    Replies: 91
    Last Post: 07-09-2008, 11:37 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •