首页 > 代码库 > servlet获取参数时,request.getParameter("id")参数获取失败
servlet获取参数时,request.getParameter("id")参数获取失败
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="http://www.mamicode.com/查询"/>
<a href="http://www.mamicode.com/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="http://www.mamicode.com/edit.jsp?id=">修改</a></td>
<td><a href="http://www.mamicode.com/delete.jsp?id=">删除</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="http://www.mamicode.com/<%=request.getParameter("id")%>"/>
<input type="submit" value="http://www.mamicode.com/修改"/>
</form>
<%=request.getParameter("id") %>
</body>
</html>
servlet获取参数时,request.getParameter("id")参数获取失败