简体   繁体   English

单击按钮,将Arraylist的特定对象从JSP发送到Servlet

[英]Send particular object of Arraylist from JSP to Servlet on click of button

Here I am sending contact no from this JSP to Servlet. 在这里,我将从此JSP向Servlet发送联系号码。 On click of a button, I want to send Contact No associated with that particular button. 单击按钮后,我要发送与该特定按钮关联的“联系电话”。 I don't know how to do it please suggest a way... Also, table borders are not displaying I tried increasing width and thickness still it not displaying. 我不知道该怎么做,请提出一种方法...此外,表格边框未显示,我尝试增加宽度和厚度仍未显示。

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

Inside form assign employee reference to a input of type hidden and give a name to that attribute 在内部表单中,将员工引用分配给隐藏类型的输入,并为该属性命名

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

Based on emp id retrieve data from db 基于emp id从数据库检索数据

In servlet read that parameter from request object reference 在servlet中,从请求对象引用中读取该参数

And for table border use border attribute in table opening tag 对于表格边框,请在表格开头标签中使用border属性

<table border="1">

Once you hit the "submit" button you are sending a request to the server. 点击“提交”按钮后,您将向服务器发送请求。

You can access the request parameters with request object as below 您可以使用以下请求对象访问请求参数

request.getParameter("parameter name");

in your case to access the Customer contact number first assign a name to it,then access it in servlet as mentioned above. 如果您要访问客户联系电话,请先为其分配一个名称,然后如上所述在servlet中对其进行访问。

Thanks, both Migrated Pigeon and jangachary sriramadasu responding to my question. 谢谢,迁移鸽和jangachary sriramadasu都回答了我的问题。 Here the answer of my question. 这是我的问题的答案。

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

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

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