java 将数据从 JSP 传递到 Servlet

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17261104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:28:39  来源:igfitidea点击:

Passing Data from JSP to Servlet

javajspservlets

提问by Mulgard

As described in the title I need to pass data from my JSP page to my servlet. I load data out of a database into a form of my JSP page. Now the user should be able to change that data. So I have to send the changed data back to my servlet to update my database. Therefore I want to use the doPost()method in my servlet

如标题中所述,我需要将数据从 JSP 页面传递到我的 servlet。我将数据从数据库加载到我的 JSP 页面的形式中。现在用户应该能够更改该数据。因此,我必须将更改后的数据发送回我的 servlet 以更新我的数据库。因此我想doPost()在我的 servlet 中使用该方法

This is my JSP:

这是我的 JSP:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv="content-script-type" content="text/javascript" />
    <meta http-equiv="content-style-type" content="text/css" />
    <meta http-equiv="content-language" content="de" />

    <link href="../resources/css/basic.css" type="text/css" rel="stylesheet" />

    <title>Edit Movie</title>
</head>

<body>
    <div id="wrapper">
        <h2 id="title">Edit Person</h2>
        <br></br>
        <br></br>
        <form id="1" class="appnitro"  method="post" action="">                     
            <ul>
                <li id="li_1" >
                    <label class="description" for="element_1">Name</label>
                    <div>
                        <input id="element_1" name="element_1" class="element text large" type="text" maxlength="255" value="${requestScope.person.name}"/> 
                    </div> 
                </li>       
                <li id="li_2" >
                    <label class="description" for="element_2">Deparment</label>
                    <div>
                        <input id="element_2" name="element_2" class="element text large" type="text" maxlength="255" value="${requestScope.person.department}"/> 
                    </div> 
                </li>   
                <li id="li_3" >
                    <label class="description" for="element_3">Job</label>
                    <div>
                        <input id="element_3" name="element_3" class="element text large" type="text" maxlength="255" value="${requestScope.person.job}"/> 
                    </div> 
                </li>       
                <li id="li_4" >
                    <label class="description" for="element_4">Biographie</label>
                    <div>
                        <textarea id="element_4" name="element_4" class="element textarea medium">${requestScope.person.biography}</textarea> 
                    </div> 
                </li>
                <li class="buttons">
                    <input type="hidden" name="form_id" value="652973" />
                    <input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
                </li>
            </ul>
        </form>                 
    </div>
</body>
</html>

And this is my Servlet without the doPost()method:

这是我的 Servlet 没有该doPost()方法:

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import de.hof.university.spj.model.People;
import de.hof.university.spj.model.PeopleDAO;

public class SinglePersonEditServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private PeopleDAO peopleDao = new PeopleDAO();

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String name = "id";
        String value = request.getParameter(name);

        int id = Integer.parseInt(value);

        People people = peopleDao.getPerson(id);

        request.setAttribute("person", people);

        RequestDispatcher reqDispatcher = request.getRequestDispatcher("../jsp/singlePersonEdit.jsp");
        reqDispatcher.forward(request, response);
    }
}

After the submit button was pressed I want to send the changed data to my servlet so I can store it in my database.

按下提交按钮后,我想将更改的数据发送到我的 servlet,以便我可以将其存储在我的数据库中。

回答by Akheloes

Why String name = "id"; String value = request.getParameter(name);? I can't seem to find any input in your JSP that's name= "id" ...

为什么String name = "id"; String value = request.getParameter(name);?我似乎无法在您的 JSP 中找到任何输入name= "id" ...

In the servlet, you should have this (for example), this :

在 servlet 中,你应该有这个(例如),这个:

String element_1_value = request.getParameter("element_1") ;

Either you forgot the input with id nameor I am missing something. In any case, this is what you need to fix within your code.

要么您忘记了带 id 的输入,name要么我遗漏了一些东西。无论如何,这是您需要在代码中修复的内容。

Not to mention that you forgot inserting the name of the servlet in the action attribute of the form tag, so you had this :

更不用说您忘记在表单标签的 action 属性中插入 servlet 的名称了,所以您有这个:

<form id="1" class="appnitro"  method="post" action="">

Which should become this :

应该变成这样:

<form id="1" class="appnitro"  method="post" action="SinglePersonEditServlet">

Finally, your action method is "post"(as shown in the above two code lines), in the piece of servlet of your question you work with doGet, you ought to put your code in doPostunless that's done, otherwise it's sufficient to call doGetinside doPost.

最后,您的操作方法是"post"(如上面两行代码所示),在您使用的问题的 servlet 中doGetdoPost除非完成,否则您应该将代码放入,否则调用doGetinside就足够了doPost

I am a beginner myself, so I recognize one when I see it, we all started somewhere and I would recommand you this totuor any good search about "handling form data with servlet".

我自己是一个初学者,所以当我看到它时我认出了一个,我们都从某个地方开始,我会向你推荐这个totu或任何关于“使用 servlet 处理表单数据”的好搜索。

Note: duplicate of this, check it out for further learning :).

注意副本,请查看以进一步学习:)。

Regards.

问候。