Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does request.getParameter return?

Tags:

// 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.

like image 518
Pramod Avatar asked Dec 08 '11 02:12

Pramod


People also ask

What is request getParameter in JSP?

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().

What is getParameter () method?

getParameter() is the method in request object, which returns String value always. So convert that string output to Integer [ line number 21] Integer.

What is the difference between request getParameter () and request getAttribute ()?

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.

What is the use of getParameter () getParameterNames () getParameterValues ()?

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.


1 Answers

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 ("").

like image 135
ziesemer Avatar answered Dec 25 '22 21:12

ziesemer