0

The following code tries to determine from which input field the user has filled via the getParameter() method:

protected void doPost(HttpServletRequest request, HttpServletResponse 
response) throws ServletException, IOException {

        System.out.println(request.getParameter("userinput"));

        if(request.getParameter("userinput") != "userinput")
            accessExampleView(request, response);
        else
            accessServletSubsidiary(request, response);

    }

Visually, I am trying to find out whether the user entered a value in the first field, or the second:

enter image description here

However, each time this runs, it points to accessExampleView. I tried setting the if statement logic to:

if(request.getParameter("userinput") != null)

to no avail - the first line continues to be true and routes to the accessExampleView method.

Essentially, is there a better way to check what kind of value exists in request object?

Here is the JSP file that this utilizes:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>JSP HTML page</title>
</head>
<body>

<form action="ServletHome" method="post">
<fieldset name="f1">
This page communicates with the servlet page via input types.
<br>
<br>
<label>Enter some text: </label>
<input type="text" name="userinput">
<input type="submit" value="send">
<br>
Enter your age:
<input type="number" name="userAge">
<input type="submit" value="send">
<br>
</fieldset>
</form>

</body>
</html>
Matthew
  • 817
  • 1
  • 13
  • 39
  • Incorrect marking @BalusC. This question is not about how one compares strings. This is about how a POST request is analyzed in terms of what values are passed. While the answer provided fixed my problem, there is certainly a better way to check values of POST requests than the way I've shown above (checking for empty strings), which is the root of my problem. – Matthew Jul 16 '19 at 19:11

1 Answers1

1

The problem is that you have written JavaScript code, but compiled it as though it was Java code.

In Java, a String is an object. When you perform a test if (objectReference1 != objectReference2) you are testing to see if the location of objectReference1 is the same as the location of objectReference2. In your case, this is written as if(request.getParameter("userinput") != "userinput")

That test will almost always be true, irrespective of the value stored in the parameter named "userinput". It is true because the address of the value of the parameter named "userinput" is almost never going to be the address of the string "userinput".

It seems likely that you actually want to compare the value of the parameter named "userinput" with the String value "userinput". If that is the case, you must use the equals method. One way to do this is
if ("userinput".equals(request.getParameter("userinput")))

This assumes that the default value of the "userinput" filed in the HTML (perhaps JSP) page is the value "userinput".

DwB
  • 37,124
  • 11
  • 56
  • 82
  • Yeah, I should've noticed that sooner. Pretty hardcore noob mistake on my part. Thanks for the help. – Matthew Jul 16 '19 at 18:10