Im building a Java web project and i need to redirect a page, only thing is i cant redirect the page from a jsp file. I tried using the following basic methods SendRedirect | getRequestDispatcher.foward | setStatus & setHeader but none of them work?
IDE is Netbeans 8.2(i heard that java web projects dont run well on version 8.2?)
Database is Mysql
code for .jsp
<body>
<h1>Hello World!</h1>
<input type="button" value="Login" onclick="foo()" />
<script>
function foo(){
var req = new XMLHttpRequest();
req.open("POST","svLogin",true);
req.send();
alert('gg');
}
</script>
</body>
code for svLogin.java (servlet)
protected void doPost(HttpServletRequest request, HttpServletResponse response) {
try {
System.out.println("xx");
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location", "/project_2/chat.jsp");
/* THIS DOESNT WORK
String nextJSP = "/project_2/chat.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
dispatcher.forward(req, resp);
*/
/* this doesnt work either
response.sendRedirect("/project_2/chat.jsp");
*/
} catch (Exception e) {
e.printStackTrace();
}
}
Problem: when i use sendRedirect, getrequestdispatcher Or setStatus & setHeader
*if i give the correct url for the redirect, absolutely nothing happens.
*if i give the wrong url for the redirect then it says 404 error on the console and thats it.
*adding a return statement does nothing too btw
honestly at this point im willing to skype just to understand whats wrong and why this is happening