繁体   English   中英

在单击按钮时使用会话将动态值从一个jsp页面传递到另一个jsp页面

[英]passing dynamic value from one jsp page to another using session on button click

我正在使用Java创建求职Web应用程序。 我为每个作业分配了一个作业ID,并在数据库的引导表中显示作业列表。 在每个按钮上单击,我需要将此工作ID转发到jsp页面,在这里我将申请人姓名添加到与这些工作相对应的数据库中。我的代码是:

<%@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>

我在获取其他jsp页面中的会话值ID时遇到了困难,因为无论单击哪个按钮,我都将获取表中最后一行的ID。请帮助我运行代码。谢谢!

<% hs.setAttribute("ID", rs.getString("id")); %>

在上面的摘录中,您正在设置会话属性“ ID”。 执行while循环后,它将存储最后的 “ id”值。 无论单击哪个链接,都会在目标页面中恢复相同的值。

一种可能的解决方案是将所调用URL中的id传递到下一页。

<a href="jobDetails.jsp?idJob=<%=rs.getString("id")%>">
     <button type="button" class="btn btn-primary">View Details</button>

您可以使用以下方法恢复jobDetails.jsp中的参数

request.getParameter("idJob")

注意:我对JSP有点生锈。 也许我忘记了报价或其他内容,但这就是想法。

您可以像请求的参数一样传递作业ID。

例如,您可以这样调用jsp页面:

<a href="jobDetails.jsp?job_id=3" />

其中job_id是参数名称,3是其值。

之后,您可以通过以下方式在jobDetails.jsp中读取参数的值:

<%=request.getParameter("job_id")%>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM