【问题标题】:Send particular object of Arraylist from JSP to Servlet on click of button单击按钮将 Arraylist 的特定对象从 JSP 发送到 Servlet
【发布时间】:2017-09-17 10:49:51
【问题描述】:

在这里,我从这个 JSP 向 Servlet 发送联系号码。单击按钮时,我想发送与该特定按钮关联的联系人编号。不知道怎么弄,求推荐方法。。。 另外,表格边框没有显示我尝试增加宽度和厚度仍然没有显示。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1">
<title>View Employee</title>
<style>
td
{
 padding: 12px 20px;
    margin: 8px 0;
}
th
{
 padding: 12px 20px;
    margin: 8px 0;
}
</style>
</head>




<body style="background-color:powderblue;">


<%@ include file="MenuBar.jsp" %>



<%@ page import="in.idk.service.ViewEmployee" %> 
<%@ page import="java.util.List" %>
<%@ page import= "in.idk.model.Employee" %>
<table>
<tr>
<th width="119"><label>Employee_Name</label></th>
<th width="168"><label>Employee_Contact_No.</label></th>
<th><label></label></th>
</tr>

<%
                     ViewEmployee viewEmployee = new ViewEmployee();
                     List<Employee> list = viewEmployee.getListOfEmployees();

                             for (Employee e : list) {
                 %>
                 <tr>
                     <td width="119"><%=e.getEmployeeName()%></td>
                     <td width="168"><%=e.getEmployeeContactNo()%></td>

                     <td><form action="GetOneEmployee" method="post">

                    <input type="submit" value="Submit" ></form></td>

                 </tr>
                 <%}%>

</table>

</body>
</html>

【问题讨论】:

    标签: java jsp servlets jakarta-ee arraylist


    【解决方案1】:

    在表单内部将员工引用分配给隐藏类型的输入并为该属性命名

    <input name="emp" value="<%=e%>" type="hidden">
    

    根据emp id从db中检索数据

    在 servlet 中从请求对象引用中读取该参数

    对于表格边框,在表格开始标签中使用边框属性

    <table border="1">
    

    【讨论】:

      【解决方案2】:

      点击“提交”按钮后,您将向服务器发送请求。

      您可以通过如下请求对象访问请求参数

      request.getParameter("parameter name");
      

      在您的情况下,要访问客户联系电话,首先为其分配一个名称,然后如上所述在 servlet 中访问它。

      【讨论】:

        【解决方案3】:

        谢谢,Migrated Pigeon 和 jangachary sriramadasu 都回答了我的问题。这里是我的问题的答案。

         <%@ page language="java" contentType="text/html; charset=UTF-8"
            pageEncoding="UTF-8"%>
        <!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=UTF-8">
        
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>View Employee</title>
        <style>
        td
        {
         padding: 12px 20px;
            margin: 8px 0;
        }
        th
        {
         padding: 12px 20px;
            margin: 8px 0;
        }
        </style>
        </head>
        
        <body style="background-color:powderblue;">
        <%@ include file="MenuBar.jsp" %>
        <%@ page import="in.idk.service.ViewEmployee" %> 
        <%@ page import="java.util.List" %>
        <%@ page import= "in.idk.model.Employee" %>
        <table border="1">
        <tr>
        <th width="119"><label>Employee_Name</label></th>
        <th width="168"><label>Employee_Contact_No.</label></th>
        <th><label></label></th>
        </tr>
        
        <%
                             ViewEmployee viewEmployee = new ViewEmployee();
                             List<Employee> list = viewEmployee.getListOfEmployees();
        
                                     for (Employee e : list) {
                         %>
                         <tr>
                             <td ><%=e.getEmployeeName()%></td>
                             <td ><%=e.getEmployeeContactNo()%></td>
        
                             <td><a name="view" href="GetOneEmployee?id=<%=e.getId() %>">View</a></td>
        
                         </tr>
                         <%}%>
        
        
        
        
        </table>
        </body>
        </html>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-08-03
          • 2014-03-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-07-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多