Can't get a servlet to behave differently depending on presence of a POST request parameter









up vote
0
down vote

favorite












I have this in my servlet's doPost():



if(request.getParameter("mode").equals("html"))
request.getRequestDispatcher("WEB-INF/jsp/Menu.jsp").forward(request, response);
else
request.getRequestDispatcher("WEB-INF/jsp/success.jsp").forward(request, response);



I test for the execution of the first statement by having an HTML page with a form as follows:



<form action="" method="post">
Username:<br>
<input type="text" name="user"><br>
Password:<br>
<input type="text" name="pass"><br><br>
<input type="hidden" name="mode" value="html" />
<input type="submit" value="Submit">
</form>


When I submit the form, the first statement (forwarding to Menu.jsp) executes correctly.
I test for the execution of the second statement by having a unit test (which I didn't write myself) that makes the post request directly, not through a form, as follows:



@Test
public void testLoginSuccess()
try
WebClient webClient = new WebClient();
String jsonText = login(webClient, "bob3", "fred3");
DocumentContext dc = JsonPath.parse(jsonText);
Assert.assertEquals("success", dc.read("$['status']"));


catch (IOException e)
e.printStackTrace();
Assert.assertTrue(false);




The login method:



private String login(WebClient webClient, String username, String password)
throws MalformedURLException, IOException
WebRequest requestSettings = new WebRequest(new URL(URL_BASE+"Login"), HttpMethod.POST);
requestSettings.setRequestParameters(new ArrayList());
requestSettings.getRequestParameters().add(new NameValuePair("user", username));
requestSettings.getRequestParameters().add(new NameValuePair("pass", password));

Page page = webClient.getPage(requestSettings);
String jsonText = page.getWebResponse().getContentAsString();
testLogger.log(Level.INFO, jsonText);
return jsonText;



The test fails and gives me a FailingHttpStatusCodeException at this line inside the login method:



Page page = webClient.getPage(requestSettings);


Since I didn't write the test myself and don't fully understand its code, I can't tell why it's failing, but it's passing when I don't have the if-else in my servlet and, instead, always execute the else statement.



Thank you for any help.










share|improve this question

























    up vote
    0
    down vote

    favorite












    I have this in my servlet's doPost():



    if(request.getParameter("mode").equals("html"))
    request.getRequestDispatcher("WEB-INF/jsp/Menu.jsp").forward(request, response);
    else
    request.getRequestDispatcher("WEB-INF/jsp/success.jsp").forward(request, response);



    I test for the execution of the first statement by having an HTML page with a form as follows:



    <form action="" method="post">
    Username:<br>
    <input type="text" name="user"><br>
    Password:<br>
    <input type="text" name="pass"><br><br>
    <input type="hidden" name="mode" value="html" />
    <input type="submit" value="Submit">
    </form>


    When I submit the form, the first statement (forwarding to Menu.jsp) executes correctly.
    I test for the execution of the second statement by having a unit test (which I didn't write myself) that makes the post request directly, not through a form, as follows:



    @Test
    public void testLoginSuccess()
    try
    WebClient webClient = new WebClient();
    String jsonText = login(webClient, "bob3", "fred3");
    DocumentContext dc = JsonPath.parse(jsonText);
    Assert.assertEquals("success", dc.read("$['status']"));


    catch (IOException e)
    e.printStackTrace();
    Assert.assertTrue(false);




    The login method:



    private String login(WebClient webClient, String username, String password)
    throws MalformedURLException, IOException
    WebRequest requestSettings = new WebRequest(new URL(URL_BASE+"Login"), HttpMethod.POST);
    requestSettings.setRequestParameters(new ArrayList());
    requestSettings.getRequestParameters().add(new NameValuePair("user", username));
    requestSettings.getRequestParameters().add(new NameValuePair("pass", password));

    Page page = webClient.getPage(requestSettings);
    String jsonText = page.getWebResponse().getContentAsString();
    testLogger.log(Level.INFO, jsonText);
    return jsonText;



    The test fails and gives me a FailingHttpStatusCodeException at this line inside the login method:



    Page page = webClient.getPage(requestSettings);


    Since I didn't write the test myself and don't fully understand its code, I can't tell why it's failing, but it's passing when I don't have the if-else in my servlet and, instead, always execute the else statement.



    Thank you for any help.










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have this in my servlet's doPost():



      if(request.getParameter("mode").equals("html"))
      request.getRequestDispatcher("WEB-INF/jsp/Menu.jsp").forward(request, response);
      else
      request.getRequestDispatcher("WEB-INF/jsp/success.jsp").forward(request, response);



      I test for the execution of the first statement by having an HTML page with a form as follows:



      <form action="" method="post">
      Username:<br>
      <input type="text" name="user"><br>
      Password:<br>
      <input type="text" name="pass"><br><br>
      <input type="hidden" name="mode" value="html" />
      <input type="submit" value="Submit">
      </form>


      When I submit the form, the first statement (forwarding to Menu.jsp) executes correctly.
      I test for the execution of the second statement by having a unit test (which I didn't write myself) that makes the post request directly, not through a form, as follows:



      @Test
      public void testLoginSuccess()
      try
      WebClient webClient = new WebClient();
      String jsonText = login(webClient, "bob3", "fred3");
      DocumentContext dc = JsonPath.parse(jsonText);
      Assert.assertEquals("success", dc.read("$['status']"));


      catch (IOException e)
      e.printStackTrace();
      Assert.assertTrue(false);




      The login method:



      private String login(WebClient webClient, String username, String password)
      throws MalformedURLException, IOException
      WebRequest requestSettings = new WebRequest(new URL(URL_BASE+"Login"), HttpMethod.POST);
      requestSettings.setRequestParameters(new ArrayList());
      requestSettings.getRequestParameters().add(new NameValuePair("user", username));
      requestSettings.getRequestParameters().add(new NameValuePair("pass", password));

      Page page = webClient.getPage(requestSettings);
      String jsonText = page.getWebResponse().getContentAsString();
      testLogger.log(Level.INFO, jsonText);
      return jsonText;



      The test fails and gives me a FailingHttpStatusCodeException at this line inside the login method:



      Page page = webClient.getPage(requestSettings);


      Since I didn't write the test myself and don't fully understand its code, I can't tell why it's failing, but it's passing when I don't have the if-else in my servlet and, instead, always execute the else statement.



      Thank you for any help.










      share|improve this question













      I have this in my servlet's doPost():



      if(request.getParameter("mode").equals("html"))
      request.getRequestDispatcher("WEB-INF/jsp/Menu.jsp").forward(request, response);
      else
      request.getRequestDispatcher("WEB-INF/jsp/success.jsp").forward(request, response);



      I test for the execution of the first statement by having an HTML page with a form as follows:



      <form action="" method="post">
      Username:<br>
      <input type="text" name="user"><br>
      Password:<br>
      <input type="text" name="pass"><br><br>
      <input type="hidden" name="mode" value="html" />
      <input type="submit" value="Submit">
      </form>


      When I submit the form, the first statement (forwarding to Menu.jsp) executes correctly.
      I test for the execution of the second statement by having a unit test (which I didn't write myself) that makes the post request directly, not through a form, as follows:



      @Test
      public void testLoginSuccess()
      try
      WebClient webClient = new WebClient();
      String jsonText = login(webClient, "bob3", "fred3");
      DocumentContext dc = JsonPath.parse(jsonText);
      Assert.assertEquals("success", dc.read("$['status']"));


      catch (IOException e)
      e.printStackTrace();
      Assert.assertTrue(false);




      The login method:



      private String login(WebClient webClient, String username, String password)
      throws MalformedURLException, IOException
      WebRequest requestSettings = new WebRequest(new URL(URL_BASE+"Login"), HttpMethod.POST);
      requestSettings.setRequestParameters(new ArrayList());
      requestSettings.getRequestParameters().add(new NameValuePair("user", username));
      requestSettings.getRequestParameters().add(new NameValuePair("pass", password));

      Page page = webClient.getPage(requestSettings);
      String jsonText = page.getWebResponse().getContentAsString();
      testLogger.log(Level.INFO, jsonText);
      return jsonText;



      The test fails and gives me a FailingHttpStatusCodeException at this line inside the login method:



      Page page = webClient.getPage(requestSettings);


      Since I didn't write the test myself and don't fully understand its code, I can't tell why it's failing, but it's passing when I don't have the if-else in my servlet and, instead, always execute the else statement.



      Thank you for any help.







      html post servlets junit






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 23:55









      Andrew S.

      124




      124






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          My bad, I was doing



          request.getParameter("mode").equals("html")


          where "request.getParameter("mode")" might return null if the parameter is not set then calling .equals on the null result.



          Why didn't it give me a NullPointerException in the stack trace though??






          share|improve this answer




















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234756%2fcant-get-a-servlet-to-behave-differently-depending-on-presence-of-a-post-reques%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            My bad, I was doing



            request.getParameter("mode").equals("html")


            where "request.getParameter("mode")" might return null if the parameter is not set then calling .equals on the null result.



            Why didn't it give me a NullPointerException in the stack trace though??






            share|improve this answer
























              up vote
              0
              down vote













              My bad, I was doing



              request.getParameter("mode").equals("html")


              where "request.getParameter("mode")" might return null if the parameter is not set then calling .equals on the null result.



              Why didn't it give me a NullPointerException in the stack trace though??






              share|improve this answer






















                up vote
                0
                down vote










                up vote
                0
                down vote









                My bad, I was doing



                request.getParameter("mode").equals("html")


                where "request.getParameter("mode")" might return null if the parameter is not set then calling .equals on the null result.



                Why didn't it give me a NullPointerException in the stack trace though??






                share|improve this answer












                My bad, I was doing



                request.getParameter("mode").equals("html")


                where "request.getParameter("mode")" might return null if the parameter is not set then calling .equals on the null result.



                Why didn't it give me a NullPointerException in the stack trace though??







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 10 at 2:48









                Andrew S.

                124




                124



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234756%2fcant-get-a-servlet-to-behave-differently-depending-on-presence-of-a-post-reques%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Use pre created SQLite database for Android project in kotlin

                    Darth Vader #20

                    Ondo