如标题中所述,我需要将数据从JSP页面传递到我的servlet.我将数据从数据库加载到我的JSP页面的一种形式.现在用户应该能够更改该数据.所以我必须将更改的数据发送回我的servlet以更新我的数据库.因此我想doPost()在我的servlet中使用该方法
这是我的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>
Run Code Online (Sandbox Code Playgroud)
这是我没有doPost()方法的Servlet :
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);
}
}
Run Code Online (Sandbox Code Playgroud)
按下提交按钮后,我想将更改的数据发送到我的servlet,以便将其存储在我的数据库中.
为什么String name = "id";
String value = request.getParameter(name);?我似乎无法在JSP中找到任何输入name="id"...
在servlet中,你应该有这个(例如),这个:
String element_1_value = request.getParameter("element_1") ;
Run Code Online (Sandbox Code Playgroud)
要么你忘了带id的输入,name要么我错过了什么.无论如何,这是您需要在代码中修复的内容.
更不用说你忘了在form标签的action属性中插入servlet的名称,所以你有这个:
<form id="1" class="appnitro" method="post" action="">
Run Code Online (Sandbox Code Playgroud)
哪个应该成为这个:
<form id="1" class="appnitro" method="post" action="SinglePersonEditServlet">
Run Code Online (Sandbox Code Playgroud)
最后,你的操作方法是"post"(如图上述两行代码),在一块你的问题和你一起工作的小服务程序的doGet,你应该把你的代码doPost,除非这样做了,否则就足以叫doGet内doPost.
我是初学者自己,所以我认识一个,当我看到它,我们所有的地方开始,我将你推荐这TOTU或关于"处理表单数据和servlet的"任何好的搜索.
注:重复的这个,看看进一步学习:).
问候.