CounterSpell
CounterSpell

Reputation: 123

JSP: How to capture the hyperlink's text on another page

jobview.jsp(JSP that display all jobs, hyperlinks on job reference number)

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.ResultSet"%>
<jsp:useBean id="job" scope="session" class="JobVacancies.Job" />
<%
        ResultSet rs = job.getResultSet();
        out.println("<h1>Job Vacancies</h1>");
        out.println("<table border=2>");
        out.println("<tr>");
        out.println("<td>Reference Number</td>");
        out.println("<td>Job Title</td>");
        out.println("<td>Brief Description</td>");
        out.println("<td>Date Posted</td>");
        out.println("<td>Date Closing</td>");
        out.println("</tr>");

        while (rs.next())
        {
          out.println("<tr>");
          out.println("<td><a href='jobdetail.jsp'>"+rs.getInt(1)+"</a></td>");
          out.println("<td>"+rs.getString(2)+"</td>");
          out.println("<td>"+rs.getString(3)+"</td>");
          out.println("<td>"+rs.getString(4)+"</td>");
          out.println("<td>"+rs.getString(5)+"</td>");
          out.println("</tr>");
         }
         out.println("</table>");
%>

jobdetail.jsp(JSP that displaying the job's detail according to Reference Number clicked on jobview.jsp)

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Job Detail</title>
    </head>
    <body>
        <h1>Job Reference Number: </h1>

        <p><a href="index.jsp">Go back to job list</a></p>
    </body>
</html>

My question is: How to capture the hyperlink text(Reference Number clicked on jobdetail.jsp) on jobdetail.jsp.

My goal is: to use that value(i) as part of the query("SELECT * FROM CQU.JOBS WHERE REFERENCE_NUM="+i)

I'm an inexperience programming student. Please explain as detail as possible. If you don't have much time, give me some hints, please. Thanks for your time.

Upvotes: 1

Views: 1435

Answers (1)

Richard JP Le Guen
Richard JP Le Guen

Reputation: 28753

You'll want to use a query string to pass the information as a request parameter. Then you can access it using the getParameter(...) of your the request object.

In jobview.jsp you'll do something like this:

int referenceNum = rs.getInt(1);
out.println("<a href='jobdetail.jsp?referenceNum="+referenceNum +"'>"+referenceNum +"</a>");

... and in jobdetail.jsp you'd do something like this:

<h1>Job Reference Number: <% out.println(request.getParameter("referenceNum")) %></h1>

PS you should consider ditching scriplets in favor of JSTL and UEL.

Upvotes: 1

Related Questions