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.
html post servlets junit
add a comment |
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.
html post servlets junit
add a comment |
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.
html post servlets junit
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
html post servlets junit
asked Nov 9 at 23:55
Andrew S.
124
124
add a comment |
add a comment |
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??
add a comment |
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??
add a comment |
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??
add a comment |
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??
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??
answered Nov 10 at 2:48
Andrew S.
124
124
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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