I am running a .jsp file on a server and trying to send user input form data to the "doPost" method in the HttpServlet.
When I try to print the vals of user input in doPost, they are null.
I am trying to get the vals by their html ID, but that's not working for some reason. There could be a simple issue in the HTML.
The submit button seems to be working as it is routing properly back to the .java file I am trying to parse the user input data with. It is only the vals that are null.
Here is my code.
Thank you! :)
<%@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);
}
The issue is with these tags, eg,: <input type="number" id="toencode">
The tag needs a name
attribute, like this: name="mynumber"
The servlet receives the name-value pairs of the request parameters from the JSP. In your JSP the name
is missing. The correct way to code your JSP is: <input type="number" id="toencode" name="mynumber">
In the servlet program access the posted parameter and its value as follows in the doPost
method:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String myNumber = request.getParameter("mynumber");
getServletContext().log("# My Number: " + myNumber); // this prints in the log file
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("My Number: " + myNumber); // this prints on the browser page
}
This should show the number you had entered in the JSP in the browser page, like: My Number: 999
. You can also refer the server logs.
Add name attribute to your input like below:
<input type="number" name="toencode" id="toencode">
<input type="number" name="mask1" id="mask1">
<input type="number" name="mask2" id="mask2">
request.getParameter
does not recognize id attributes.
The type of three input should be text, not number . try it.
In the three form input fields, use the name
attribute instead of or in addition to id
. Only the values of these input fields are included in the request as parameters.
Your form seems to be missing the name
attribute for all the fields.
Try changing this:
<input type="number" id="toencode">
to this (adding the name
attribute to the toencode
field):
<input type="number" id="toencode" name="toencode">
Obviously, for the other fields (mask1, mask2) the name
s value would be matching their id
s
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.