My Project Structure is this:
The body of my LoginPage is the following
<body>
<P id="errors"></P>
<p>Login Details</p>
<div id = "page"></div>
<table>
<tr>
<td> Login ID :</td>
<td><input type="number" id="loginid" min="1" max="200" style="width:169px;"/></td>
</tr>
<tr>
<td> Passowrd :</td>
<td><input type="password" id="password"/></td>
<tr>
</table>
<button id="loginB" onclick="login()">submit</button>
</body>
After Successful Login I would like to move from loginPage.jsp to home.jsp using JavaScript or Ajax. My code is
function login()
{
var name = document.getElementById("loginid").value;
var password = document.getElementById("password").value;
var url="/loginPage?name="+name+"&password="+password;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var responsetext=xhttp.responseText;
var parsedresult=JSON.parse(responsetext);
if(parsedresult.success==true)
{
window.location="home.jsp";
}
else
{
document.getElementById("errors").innerHTML=parsedresult.message;
}
}
};
xhttp.open("POST",url,true);
xhttp.send();
}
It wouldn't go to home.jsp and give a 404 Error. How to configure it to move to home.jsp ?
My Login API is the following. The LoginModel has hardcoded username and password that is used for validation. The loginmodel.validate() would return a string "false" if the credentials are incorrect.
@Controller
public class LoginController {
LoginModel loginmodel = new LoginModel();
@RequestMapping(value="/")
public ModelAndView redirect(HttpServletRequest request, HttpServletResponse response) throws IOException
{
return new ModelAndView("loginPage");
}
@RequestMapping(value="/loginPage",method=RequestMethod.POST)
public void login(HttpServletRequest request, HttpServletResponse response)
throws IOException {
String sessionName = loginmodel.validate(request.getParameter("name"), request.getParameter("password"));
if(!sessionName.equals("false"))
{
HttpSession session = request.getSession();
session.setAttribute("name", sessionName);
JSONObject result= new JSONObject();
result.put("success", true);
response.setContentType("application/json");
response.getWriter().print(result);
}
else
{ JSONObject result= new JSONObject();
result.put("success", false);
result.put("message", "invalid credentials");
response.setContentType("application/json");
response.getWriter().print(result);
}
}
@RequestMapping(value="/logout")
public String logout(HttpServletRequest request, HttpServletResponse response) throws IOException
{
request.getSession().invalidate();
return "loginPage";
}
}
Please write one method in controller which returns home.jsp page like below :
@RequestMapping(value="/home")
public String logout(HttpServletRequest request, HttpServletResponse response) throws IOException
{
return "home";
}
AND then from your jsp page :
window.location=" contextPath /home";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.