Я работаю над веб-приложением, используя JAVA Сервлет. Я пытаюсь создать форму для входа. Я просто хочу, чтобы пользователь установил имя пользователя и пароль, и, если они действительны, откройте главную страницу. html Код запускается без ошибки или исключения, но когда эта функция запускает requestDispatcher.forward()
перезагрузка страницы без пересылки на главную страницу. html
это функция проверки:
public static boolean validate(String name,String pass){
boolean status=false;
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=(Connection) DriverManager.getConnection(
"jdbc:mysql://localhost:3309/project","root","123456");
java.sql.PreparedStatement ps=con.prepareStatement(
"select * from user where username=? and password=?");
ps.setString(1,name);
ps.setString(2,pass);
ResultSet rs=ps.executeQuery();
status=rs.next();
}catch(Exception e){System.out.println(e);}
return status;
}
это функция входа в систему :
public static void Login(HttpServletRequest request, HttpServletResponse response) {
String userName= request.getParameter("username");
String password =request.getParameter("password");
if(UserDao.validate(userName, password)){
RequestDispatcher requestDispatcher=request.getRequestDispatcher("mainPage.html");
try {
requestDispatcher.forward(request,response);
/*I already debug the code , it run requestDispatcher.forward(request,response); without any exceptions but nothing happens the html page only refreshing ,not opening mainPage.html*/
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{
System.out.print("Sorry username or password error");
RequestDispatcher rd=request.getRequestDispatcher("http://localhost:8080/project");
try {
rd.include(request,response);
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
получение запроса от ajax в контроллере (сервлете):
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
if (action.contentEquals("Login"))
UserBusiness.Login(request, response);
}
ajax вызов:
function Login() {
var username = $('#username').val();
var password = $('#password').val();
$.ajax({
url: 'Controller',
data : {
action: 'Login',
username: username,
password: password,
},
type: 'post',
success: function(response) {
},
error: function(e) {
alert('error');
}
});
}
логин html страница:
<script>
$(document).ready(function () {
// loadJsFile("scripts/user-page.js");
$('#login_btn').on('click', function(){
Login();
});
});
</script>
<form class="sign-in-htm" method="GET" >
<div class="group">
<label for="user" class="label">Username</label>
<input id="username" name="username" type="text" class="input">
</div>
<div class="group">
<label for="pass" class="label">Password</label>
<input id="password" name="password" type="password" class="input" data-type="password">
</div>
<div class="group">
<input id="check" type="checkbox" class="check" checked>
<label for="check"><span class="icon"></span> Keep me Signed in</label>
</div>
<div class="group">
<input type="submit" id="login_btn" class="button" value="Sign In">
</div>
<div class="hr"></div>
<div class="foot-lnk">
<a href="#forgot">Forgot Password?</a>
</div>
</form>