【发布时间】:2019-05-21 11:30:04
【问题描述】:
我正在服务器上运行一个 .jsp 文件并尝试将用户输入表单数据发送到 HttpServlet 中的“doPost”方法。
当我尝试在 doPost 中打印用户输入的 val 时,它们为空。
我试图通过他们的 html ID 获取 vals,但由于某种原因这不起作用。 HTML 中可能有一个简单的问题。
提交按钮似乎正在工作,因为它正在正确路由回我试图解析用户输入数据的 .java 文件。只有 val 为 null。
这是我的代码。
谢谢! :)
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=US-ASCII"
pageEncoding="US-ASCII"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Binary Encoder</title>
</head>
<body>
<h2>Binary Encoding: Encode any number from 0 to 65535</h2> <br>
<h3>Date=<%= new Date() %>
</h3>
<!-- in this form I need to figure out how to get user input into Binaryencoder.java-->
<form action="../Binaryencoder" method="post">
Input number you want to encode (0 to 65536):<br>
<input type="number" id="toencode"><br>
Input first number for encoding (0 to 255) :<br>
<input type="number" id="mask1"><br><br>
Input second number for encoding (0 to 255) :<br>
<input type="number" id="mask2"><br><br>
<input type="submit" id="submit" value="Submit">
</form>
</body>
</html>
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
//code to process the form...
String toencode = request.getParameter("toencode");
String mask1 = request.getParameter("mask1");
String mask2 = request.getParameter("mask2");
//response is the thing that goes back to HTML page
PrintWriter out = response.getWriter();
String output = String.format("Number to encode is: %s", toencode);
String op1 = String.format("Mask1 is: %s", mask1);
String op2 = String.format("Mask2 is: %s", mask2);
out.println(output);
out.println(op1);
out.println(op2);
}
【问题讨论】: