无法获取隐藏的表单字段数据

时间:2015-12-02 05:58:37

标签: java html servlets

我试图获取我保存在我称为noteid的jsp表中的隐藏值。我试图在我的控制器中使用noteid但我无法访问它,当我使用int id = Integer.parseInt(request.getParameter(" noteid"));我得到一个零错误500.如何从jsp隐藏字段获取数据以在我的控制器中使用?

这是我的jsp,它有标题链接表,除非我误用了隐藏的表单字段来保存注释ID。应该是对的

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>MyNotes</title>
</head>
<body>

<p align="right">Hello, ${sessionScope.CurrentUser}!&nbsp;&nbsp;&nbsp;<a href="Logout">Logout</a></p>
<span>JOT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="MyNotes">My Notes</a>&nbsp;|&nbsp;<a href="NewNote.jsp">New</a></span>
<br>
<br>
<br>


<table border=1>

  <tr><th>Titles</th></tr>
<c:forEach items="${entries}" var="entry">
  <tr>
    <td><a href="NoteDetails?id="${entry.getId()}>${entry.title}</a></td>
     <td><input type="hidden"  value="${entry.getId()}" id="noteid" name="noteid"/></td>
  </tr>
</c:forEach>

</table>

<p><a href="NewNote.jsp">New Note</a></p>



</body>
</html>




here the controller for my notedetails.im trying to get the noteid i saved from the hidden form field in my jsp





@WebServlet("/Homework2/NoteDetails")
public class NoteDetails extends HttpServlet {
    private static final long serialVersionUID = 1L;



    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        getServletContext().setAttribute("cs320.hw2.Note", new ArrayList<Notes>());

        Notes note = null;
        try{
            int id = Integer.parseInt(request.getParameter("noteid"));
            ArrayList<Notes> entries = (ArrayList<Notes>) getServletContext().getAttribute("cs320.hw2.Note");
            for (Notes e : entries){
                if (e.getId() == id){
                    note = e;
                    System.out.println(e+"+"+id);
                    break;
                }
            }
        }catch(Exception e){}
        finally{
            if (note == null){
                System.out.println("WRONG");
                return;
            }
        }






}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

}

2 个答案:

答案 0 :(得分:1)

您没有通过表单提交隐藏字段

以下只是一个例子

<form action="hello" >
<input type="hidden" value="100" name="id"/>
input type="submit"/>

的servlet

public void doGet(HttpServletRequest request, HttpServletResponse response)
{
int i=Integer.parseInt(request.getParameter("id"));
}

答案 1 :(得分:0)

我使用隐藏在html中的输入类型

<form>
<input type="hidden" name="test" value="test1"/>
</form>