我从NetBeans开始,并实施一个表单,当您按下"提交"按钮,执行验证并告诉您输入的数据是否正确。我还没有进入验证部分,目前我所要做的就是,当提交"单击按钮,弹出一条消息。我在这里遇到麻烦,我觉得这是一个简单的快速修复,但我没有在留言板或文档上找到任何东西。
编辑 - 谢谢你们!错过了#34;形式"标签。我认为这很简单,再次感谢大家的帮助!这是我的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>
}
答案 0 :(得分:2)
用于创建控件的元素通常出现在FORM中 元素,但也可能出现在FORM元素声明之外 当它们用于构建用户界面时。这将在讨论中讨论 关于内在事件的部分。 请注意,
form
外的控件不能 成功控制。
A <input>
element for submit
is a control
。因此,如果它出现在<form>
之外,就像您目前所拥有的那样,点击它就不会做任何事情。
将<input>
元素(以及其他任何显示元素)嵌套在<form>
中,指定用于提交数据的action
和method
。
答案 1 :(得分:1)
如果您想提交,那么您的html中应该有<form>
例如
<form name="input" action="/clientinformationservlet" method="POST">
// your inputs
</form>
答案 2 :(得分:1)
你应该添加
<body><form action="/clientinformationservlet" method="POST">
...
</form></body>
在体内。