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.

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

then how do you suggest me to handle the onclick part.?
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: stackoverflow.com/questions/25881204/…
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" >

6 Comments

It's not giving any error now, but redirecting me to the same login page and not even going to the break-point when running in debug mode.
try writing return "/list-books"
In my sevlet.xml I already have <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/" /> <property name="suffix" value=".jsp" /> </bean> So, "/" will automatically be prefixed
what url is updated while clicking on submit button?
url is not changing, it seems it just is refreshing the page And when I'm simply clicking on "Login" button(without entering any data) then same Error-405 is coming
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.