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

时间:2017-04-20 17:02:55

标签: java jsp servlets java-ee arraylist

这里我将从此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>

3 个答案:

答案 0 :(得分:0)

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

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

基于emp id从db

检索数据

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

对于表格边框,使用表格开头标记中的border属性

<table border="1">

答案 1 :(得分:0)

一旦你点击&#34;提交&#34;按钮,您正在向服务器发送请求。

您可以使用请求对象访问请求参数,如下所示

request.getParameter("parameter name");

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

答案 2 :(得分:0)

谢谢,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>