gpt4 book ai didi

java - 表单不会向 servlet 发布任何值

转载 作者:行者123 更新时间:2023-12-01 10:49:26 25 4
gpt4 key购买 nike

嗯,我的表单没有向我的 servlet 发布任何值。我已经尝试过其他 id 值。也不起作用。

它不断从表单中获取空值并出现 HTTP 500 错误

java.lang.NumberFormatException: null
java.lang.Integer.parseInt(Unknown Source)
java.lang.Integer.parseInt(Unknown Source)
login.StoreVM.doPost(StoreVM.java:46)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

这是 HTML 代码:

                  <li>
<ul>
<li class="title"><h3>Lite</h3><h4>€100<small>/month</small></h4></li>
<li class="divider"></li>
<li>2 Core CPU</li>
<li>4GB RAM</li>
<li>50GB Disk Space</li>
<li>No Backup</li>
<li>8 AM to 5 PM Live Support</li>
<li><form method="post" action="storeVM">
<input type="text" id="vmstore" value="hello"/>
<button type="submit" class="grey">Sign up now</button>
</form>
</ul>
</li>

这是 java servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
HttpSession session = request.getSession(false);

//Make some variables and shorter things.
int button = Integer.parseInt(request.getParameter("vmstore"));
PrintWriter out = response.getWriter();;

//Check if session is set.
if(session == null || session.getAttribute("username") == null) {
out.println("Please login first before ordering");
RequestDispatcher rs = request.getRequestDispatcher("index.html");
rs.include(request, response);
} else {
//Parse string to int.
out.println(button);
if(button == 1) {
out.println("yeah");
} else if (button == 2) {
out.println("yeah2");
} else if (button == 3) {
out.println("yeah3");
} else {
out.println("Something went wrong. Please contact us");
RequestDispatcher rs = request.getRequestDispatcher("index.html");
rs.include(request, response);
}

我不知道为什么它不起作用,因为所有其他形式都有效。

最佳答案

答案是更改 <input>命名为“vmstore”。

request.getParameter 不接受 id,只接受 name。

关于java - 表单不会向 servlet 发布任何值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34002959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com