gpt4 book ai didi

java - 简单的 NetBeans servlet,按钮不起作用

转载 作者:行者123 更新时间:2023-12-01 18:26:57 24 4
gpt4 key购买 nike

我从 NetBeans 开始,实现一个表单,当您按下“提交”按钮时,该表单将执行验证并告诉您输入的数据是否正确。我还没有到达验证部分,目前我想做的就是,当单击“提交”按钮时,会弹出一条消息。我在这里遇到了麻烦,我感觉这是一个快速简单的修复,但我在留言板或文档上没有找到任何内容。

编辑 - 谢谢大家!缺少“form”标签。我想这很简单,再次感谢大家的帮助!

这是我的index.html 文件:

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>Client Information</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div><h1>Client Information</h1><table border="1">
<tbody>
<tr>
<td>First Name</td>
<td><input type="text" name="FirstName" value="" size="50" /></td>
<td>Surname</td>
<td><input type="text" name="Surname" value="" size="50"/></td>
</tr>
<tr>
<td>Age</td>
<td><input type="number" name="Age" value="" min="0" max="120"/></td>
<td>Gender</td>
<td><input type="text" name="Gender" value="" size="1" maxlength="1"/></td>
</tr>
</tbody>
</table>
<input type="submit" value="Submit" name="validation" />
</div>
</body>
</html>

这里是 ClientInformationServlet.java 文件,最重要的是 processRequest 方法,而 if (request.getParameter("validation")!= null) 行是我试图让行动发生。

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package clientInformation;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
*
* @author nicolasdubus
*/
@WebServlet(name = "ClientInformationServlet", urlPatterns = {"/clientinformationservlet"})
public class ClientInformationServlet extends HttpServlet {

/*
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Client Information</title>");
out.println("</head>");
out.println("<body>");

String sfirst = request.getParameter("FirstName");
String ssecond = request.getParameter("SurName");
String sAge = request.getParameter("Age");
String sGender = request.getParameter("Gender");
try {
Integer age = Integer.parseInt(sAge);
if (request.getParameter("validation") != null) {
System.out.println("<h1>Client information is valid</h1>");
out.println("<h1>Client</h1>");
System.exit(0);
}
} catch (IllegalArgumentException e) {
out.println("<h1>Client information is invalid, please verify entries</h1>");
}
out.println("<body>");
out.println("</html");
/* */
out.close();
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}

最佳答案

HTML specification

The elements used to create controls generally appear inside a FORM element, but may also appear outside of a FORM element declaration when they are used to build user interfaces. This is discussed in the section on intrinsic events. Note that controls outside a form cannot be successful controls.

A <input> element for submit is a control 。因此,如果它出现在 <form> 之外,正如您目前拥有的那样,单击它不会执行任何操作。

嵌套您的 <input> <form> 中的元素(以及任何其他显示元素)其中指定 actionmethod用于提交您的数据。

关于java - 简单的 NetBeans servlet,按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25792225/

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