// index.jsp
<form method="post" action="backend.jsp"> <input type="text" name="one" /> <input type="submit value="Submit" /> </form>
In backend.jsp what does request.getParameter("one"); return?
request.getParameter("one").getClass().getName();
returns java.lang.String, so it must be a String right?
However I cannot do
String one = request.getParameter("one"); if (!"".equals(one)) {}
or
if (one != null) {}
This is obvious, because variable one does not return null. Is
if (one.length() > 0) {}
only way to go, or are there better solutions or a better approach? I am considering both solutions to be on jsp. Using a servlet (although jsp is a servlet) is a different use case in this scenario.
getParameter is a function name in JSP which is used to retrieve data from an HTML/JSP page and passed into the JSP page. The function is designated as getParameter() function. This is a client-side data retrieval process. The full function can be written as request. getParameter().
getParameter() is the method in request object, which returns String value always. So convert that string output to Integer [ line number 21] Integer.
getParameter() returns http request parameters. Those passed from the client to the server. getAttribute() is for server-side usage only - you fill the request with attributes that you can use within the same request.
getParameter() method to get the value of a form parameter. getParameterValues() − Call this method if the parameter appears more than once and returns multiple values, for example checkbox. getParameterNames() − Call this method if you want a complete list of all parameters in the current request.
Per the Javadoc:
Returns the value of a request parameter as a String, or null if the parameter does not exist.
Do note that it is possible to submit an empty parameter - such that the parameter exists, but has no value. For example, I could include &log=&somethingElse
into the URL to enable logging, without needing to specify &log=true
. In this case, the value will be an empty String (""
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With