Я создаю прототип веб-приложения для менеджера баз данных.После успешного входа в систему приложение должно перейти к представлению менеджера баз данных (dbmanager.html).Однако запрашиваемая страница не отображается в браузере.
Среда - Windows 10, Apache-Tomcat-7.0.94, Glassfish 4, MySql 5.7.24.
login.html
<form id="login_form" name="login_form" action="LogIn"
method="post" onsubmit="formPost(this);return false;">
<fieldset>
<!-- more fields here -->
<input id="input_login" value="Login" type="submit" >
</fieldset>
<p id="mesg" style="color: red;"></p>
</form>
LogIn.java
@WebServlet(name = "LogIn", urlPatterns = { "/LogIn" })
@MultipartConfig
public class LogIn extends HttpServlet {
protected void doPost(
HttpServletRequest request,
HttpServletResponse response
) throws ServletException, IOException {
//get the inputs, customized method getParameter
String user = getParameter("user");
String password = getParameter("password");
//validate inputs
String errorMsg = null;
if(user == null || user.equals("")){
errorMsg ="User can't be null or empty";
}
if(password == null || password.equals("")){
errorMsg = "Password can't be null or empty";
}
//show error, if so
if(errorMsg != null){
response.setContentLength("#mesg=".length() + errorMsg.length());
PrintWriter out= response.getWriter();
out.println("#mesg=" + errorMsg);
return;
}
//no errors, forward / redirect to next page
request.getRequestDispatcher("/contents/dbmanager.html")
.forward(request, response);
}
protected void doGet(
HttpServletRequest request,
HttpServletResponse response
) throws ServletException, IOException {
request.getRequestDispatcher("/contents/dbmanager.html")
.forward(request, response);
}
}
JavaScript (без jQuery)
function formPost(_form){
var xhr = new XMLHttpRequest();
xhr.open(_form.method, _form.action);
xhr.onload = function(event){
event.preventDefault(); // Important! Prevents submitting the form.
var response = event.target.response;
//process response...
console.log(response);
//response is a array of string of structure {id=value}
var idMark = response.indexOf("#");
if( isNaN(idMark) ){
return;
}
if(idMark !== 0){
return;
}
//other validations here...
};
// or onerror, onabort
var formData = new FormData(_form);
xhr.send(formData);
}