0

I have created a login form

<form class="login100-form validate-form p-b-33 p-t-5" method="POST">
<div class="wrap-input100 validate-input" data-validate = "Enter username">
    <input class="input100" type="text" name="username" placeholder="User name">
    <span class="focus-input100" data-placeholder="&#xe82a;"></span>
</div>
<div class="wrap-input100 validate-input" data-validate="Enter password">
    <input class="input100" type="password" name="pass" placeholder="Password">
    <span class="focus-input100" data-placeholder="&#xe80f;"></span>
</div>
<div class="container-login100-form-btn m-t-32">
    <input class="login100-form-btn" type="button" onclick="location.href='eLibrary/login'" value="Login" >
</div>

And my controller function is

@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(HttpServletRequest request, HttpServletResponse response) {
    String userName = request.getParameter("username");
    String pass = request.getParameter("pass");
    return "list-books";
}

But, when I'm trying to login, it's giving error

HTTP Status 405 - Method Not Allowed

Request method 'GET' not supported

I even have tried

@PostMapping("/login")
public String login(HttpServletRequest request, HttpServletResponse response) {
    String userName = request.getParameter("username");
    String pass = request.getParameter("pass");
    return "list-books";
}

But in the above case, request.getParameter("username") is giving null.

Can anyone please help me out.

Nidhi257
  • 754
  • 1
  • 5
  • 23
amit_vickey
  • 45
  • 1
  • 9

2 Answers2

1

Update the line:

<input class="login100-form-btn" type="button" onclick="location.href='eLibrary/login'" value="Login" >

with remove the onclick:

<input class="login100-form-btn" type="submit" value="Login" >

and update the FORM line to:

<form class="login100-form validate-form p-b-33 p-t-5" method="POST" action="eLibrary/login">

Onclick will always send a GET request. If you wanna do POST with javascript you have to use AJAX action.

Adam Lesiak
  • 501
  • 4
  • 10
  • then how do you suggest me to handle the onclick part.? – amit_vickey Apr 25 '18 at 09:43
  • You have two choices: 1. Remove onclick and do standard POST action. 2: Use javascript xHttpRequest or one of the framework (eg. jQuery) to send post action. Examples: https://stackoverflow.com/questions/25881204/how-to-use-jquery-post-method-to-submit-form-values – Adam Lesiak Apr 25 '18 at 09:48
0

Update below line

<input class="login100-form-btn" type="button" onclick="location.href='eLibrary/login'" value="Login" >

with <input class="login100-form-btn" type="submit" onclick="location.href='eLibrary/login'" value="Login" >

Nidhi257
  • 754
  • 1
  • 5
  • 23