Reputation: 1
I am making a job search web application using java. I have alotted a job id to each job and i am displaying the list of jobs in a bootstrap table from database. on each button click i need to forward this job id to a jsp page where i am adding the applicant name to the database corresponding to the jobs.My code is:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="com.dbutil.CrudOperation"%>
<%@ page import="java.util.*,java.sql.*" %>
<%@ page import="javax.servlet.http.HttpSession" %>
<html>
<head>
<title>Job List</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/jobs.css">
<script src="jquery.js"></script>
<script src="bootstrap.min.js"></script>
</head>
<body data-spy="scroll" data-target=".navbar" data-offset="50">
<% Connection con = null;
ResultSet rs = null,rs1=null;
PreparedStatement ps = null;
HttpSession hs = null;
hs=request.getSession();
con = CrudOperation.createConnection();
%>
<div class="container">
<%String strsql = "select * from postjob1 where sr>0";
try {
ps = con.prepareStatement(strsql);
rs = ps.executeQuery(); %>
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th>Job Id</th><th>Job Category</th><th>Available Posts</th><th>Required Qualifications</th><th>Salary Details</th><th>Location</th><th>Last date of application</th><th></th>
</tr>
</thead>
<% while (rs.next()) {
%>
<tbody>
<tr><td><%=rs.getString("id")%></td><td><%=rs.getString("jobcategory")%></td><td><%=rs.getString("available")%></td><td><%=rs.getString("requiredqualification")%></td><td><%=rs.getString("salarydetails")%></td><td><%=rs.getString("location")%></td><td><%=rs.getString("lastdate")%></td><td><a href="jobDetails.jsp"><button type="button" class="btn btn-primary" onclick="<% hs.setAttribute("ID",rs.getString("id")); %>" >View Details </button></a></td></tr>
</tbody>
<%}
} catch (SQLException se) {
System.out.println(se);
}%>
</table>
</div
</div>
</body>
</html>
I am facing difficulty when I am retreiving the session value ID in other jsp page as i am getting id of the last row in the table irrespective of whichever button i click.Kindly help me run my code.Thanks!
Upvotes: 0
Views: 2053
Reputation: 2468
<% hs.setAttribute("ID", rs.getString("id")); %>
In the excerpt above, you are setting the session attribute "ID". After executing the while
loop, it stores the last "id" value. The same value is recovered in the destination page, whichever the clicked link.
A possible solution is passing the id in the invoked URL to the following page.
<a href="jobDetails.jsp?idJob=<%=rs.getString("id")%>">
<button type="button" class="btn btn-primary">View Details</button>
You can recover the parameter in jobDetails.jsp using
request.getParameter("idJob")
Note: I'm a bit rusty in JSP. Maybe I've forgotten a quote or something, but that's the idea.
Upvotes: 1
Reputation: 354
You can pass job id like request's parameter.
For example you can call your jsp-page like that:
<a href="jobDetails.jsp?job_id=3" />
Where job_id is a parameter name and 3 is its value.
After that you can read parameter's value in your jobDetails.jsp in next way:
<%=request.getParameter("job_id")%>
Upvotes: 0