【问题标题】:Getting null value while passing value from jsp to jsp将值从jsp传递给jsp时获取空值
【发布时间】:2016-06-16 12:11:38
【问题描述】:

嗨朋友们,我正在尝试通过链接将值从一个 jsp 传递到另一个,但该值没有传递。我在论坛中提到了很多答案。没有任何帮助。

显示.jsp

<%@ page import="java.sql.*" %>
<%ResultSet resultset =null;%>

<HTML>
<HEAD>
    <TITLE>Select element drop down box</TITLE>
</HEAD>

<BODY>



<h2>EMPLOYEE DETAILS</h2>
<br>
<form  action = "Form.jsp">
<button type = "submit">Add New Employee</button>
</form>
<%
    try{
           Class.forName("com.mysql.jdbc.Driver").newInstance();

           Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/hirarchy_check","root","root@123");

           Statement statement = connection.createStatement() ; 

           resultset =statement.executeQuery(" SELECT e.Emp_Id,e.Emp_Name ,e.Emp_Designation, m.Emp_Designation"+
                  " FROM employee_details e " +
                   " INNER JOIN employee_details m on e.Manager_id = m.Emp_Id ") ;

%>
  <table border = 1>
              <tr><th>Employee Id</th><th>Employee Name</th><th>Employee Designation</th><th>Reporting Manager</th><th>View</th></tr>
              <tr><td>1</td><td>Steve</td><td>ceo</td><td>0</td><td><a href = 'FormUpdate.jsp'>View</a></td></tr>

  <%    
              while(resultset.next())
              { %>
                  <tr><td><%=resultset.getInt(1)%></td><td><%=resultset.getString(2)%></td>
                  <td><%=resultset.getString(3)%></td><td><%=resultset.getString(4)%></td>
                  <td><a href="EmpDetails.jsp?empId=${resultset.getInt(1)}&empName=${resultset.getString(2)}" >update</a></td></tr>
            <%  } %>

    </table>

<%         }
        catch(Exception e)
        {
             out.println(e);
        }
%>

</BODY>
</HTML>

我必须传递值的页面是

EmpDetails.jsp

  <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Employee Details</title>
</head>
<body>
out.println<%=request.getParameter("empId") %>
out.println<%=request.getParameter("empName") %>
</body>
</html>

【问题讨论】:

    标签: java jsp servlets jakarta-ee


    【解决方案1】:

    注意

    要从以下答案运行代码,您必须包含 JSTL 标记库。可以找到here。

    或者要包含,在您的 JSP 顶部写下以下行。

    <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
    

    回答

    你能试试 forEach 吗?

    <c:forEach var="row" items="${resultset.rows}">
    <tr>
    <td><a href="EmpDetails.jsp?empId=${row.Emp_Id}&empName=${row.Emp_Name}">update</a></td>
    </tr>    
    </c:forEach>
    

    【讨论】:

    • 我认为 resultset.getInt(1) 无法按您的预期工作,恕我直言,这种方式更具可读性。
    • 提问者可能没有使用JSTL
    • 是的,我也是 Java 技术和编程的新手@rupinderjeet47
    【解决方案2】:

    如果您使用 jsp 到 jsp 重定向,只需使用 EL(Expression Language) 并放置

    ${param.empId} insted of &lt;%=request.getParameter("empId") %&gt;

    ${param.empName} insted of &lt;%=request.getParameter("empName") %&gt;

    当您从 JSP 重定向到 JSP 时,您需要这样做,否则不需要...

    【讨论】:

      猜你喜欢
      • 2014-01-17
      • 2014-09-09
      • 2013-02-13
      • 1970-01-01
      • 1970-01-01
      • 2014-07-19
      • 2012-06-04
      • 2013-11-25
      • 2012-05-03
      相关资源
      最近更新 更多