如何从jsp页面中的表单发送数据到servlet?

时间:2016-06-09 12:53:05

标签: javascript java html jsp servlets

我有.jsp页面,其中我显示来自DB的一些数据(表seria [id,name,description,photo])。当用户点击带有照片和文字的区域时,如何将id发送到servlet?现在我点击的任何地方,servlet都会获得seria_id = 1。网站http://joxi.ru/krDLaDYS07pwNr我的表单在这里的屏幕截图 - >

<form id="myform" action="/seria" method="post">
    <%Iterator itr;%>
    <% LinkedList<Seria> data = (LinkedList<Seria>) request.getAttribute("data");
    Seria seria;
    for (itr=data.iterator(); itr.hasNext(); ) {
        seria = (Seria) itr.next();
    %>
    <input type="hidden" name="seria_id" value=<%=seria.getId()%>>
    <table onclick="document.getElementById('myform').submit();">
        <tr>
            <td colspan="2">
                <h3 align="center">Серия &laquo;<%=seria.getName()%>&raquo;</h3>
            </td>
        </tr>
        <tr>
            <td width="50%">
                    <img src="/img/<%=seria.getPhoto()%>" width="60%" alt="cars" hspace="20%">
            </td>
            <td>
                <%=seria.getDescription()%>
            </td>
        </tr>
    </table>
    <hr>
    <%}%>
</form>

的web.xml

<servlet>
    <servlet-name>SeriaServlet</servlet-name>
    <servlet-class>mypackage.SeriaServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>SeriaServlet</servlet-name>
    <url-pattern>/seria</url-pattern>
</servlet-mapping>

的servlet

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Enumeration en=request.getParameterNames();
    while(en.hasMoreElements())
    {
        Object objOri=en.nextElement();
        String param=(String)objOri;
        String value=request.getParameter(param);
        System.out.println("Parameter Name is '"+param+"' and Parameter Value is '"+value+"'");
    }
}

始终打印“参数名称为'seria_id'且参数值为'1'” 关于github项目的问题。 https://github.com/sasha361322/jsp-servlet/commit/aaf869ccde54d53e16f964a6db9786c27def8eab

2 个答案:

答案 0 :(得分:0)

如果你想通过

获得价值
    getParameter(name);

然后你应该发送值URL 即在您的情况下,操作URL是/ seria

所以你将值发送为

/seria?parmName1=ParmaValue1&parmName2=ParmaValue2

答案 1 :(得分:0)

我得到了解决方案。而不是一种形式,现在我为每一种形式制作。

<%Iterator itr;%>
                            <% LinkedList<Seria> data = (LinkedList<Seria>) request.getAttribute("data");
                            Seria seria;
                            for (itr=data.iterator(); itr.hasNext(); ) {
                                seria = (Seria) itr.next();
                            %>
                            <form id="<%=seria.getId()%>" action="/seria" method="post">
                                <input type="hidden" name="seria_id" value=<%=seria.getId()%>>
                                <table onclick="document.getElementById('<%=seria.getId()%>').submit();">
                                    <tr>
                                        <td colspan="2">
                                            <h3 align="center">Серия &laquo;<%=seria.getName()%>&raquo;</h3>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td width="50%">
                                                <img src="/img/<%=seria.getPhoto()%>" width="60%" alt="cars" hspace="20%">
                                        </td>
                                        <td>
                                            <%=seria.getDescription()%>
                                        </td>
                                    </tr>
                                </table>
                            </form>
                            <hr>
                            <%}%>