标签:
servlet获取参数时,request.getParameter("id")参数获取失败,这里的参数是“index”里面href中的参数
要注意,取不到值,是不是要取的参数有没有提交
servlet:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
String idStr = request.getParameter("id");
Integer id = Integer.valueOf(idStr);
ContactsDao contactsDao = new ContactsDao();
Contacts con = contactsDao.get(id);
request.setAttribute("contacts", con);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
jsp页面:
index:
<form action="QueryServlet" method="GET">
姓名:<input type="text" name="name"/>
地址:<input type="text" name="address"/>
电话:<input type="text" name="phone"/>
<input type="submit" value="查询"/>
<a href="add.jsp">增加</a>
</form>
<br><br><br><br>
<%
List<Contacts> allContactsList = (List<Contacts>) request.getAttribute("allContactsList");
if(allContactsList != null && allContactsList.size()>0){
%>
<table border=1px>
<tr>
<th>id</th>
<th>name</th>
<th>address</th>
<th>phone</th>
<th>修改</th>
<th>删除</th>
</tr>
<%
for(Contacts c : allContactsList){
%>
<tr>
<td><%=c.getId() %></td>
<td><%=c.getName()%></td>
<td><%=c.getAddress()%></td>
<td><%=c.getPhone() %></td>
<td><a href="edit.jsp?id=<%=c.getId()%>">修改</a></td>
<td><a href="delete.jsp?id=<%=c.getId()%>">删除</a></td>
</tr>
<%
}
%>
</table>
<%
}
edit界面
<form action="EditServlet" method="get">
姓名:<input type="text" name="name"/>
地址:<input type="text" name="address"/>
电话:<input type="text" name="phone"/>
<input type="hidden" name="id" value="<%=request.getParameter("id")%>"/>
<input type="submit" value="修改"/>
</form>
<%=request.getParameter("id") %>
</body>
</html>
servlet获取参数时,request.getParameter("id")参数获取失败
标签:
原文地址:http://www.cnblogs.com/lixiuming521125/p/5870998.html