when i logged in using the post method, the form data will be sent to owner-controller servlet and finally redirect to viewStaff.jsp page.
the problem is, if i'm using request dispatcher, whenever i reload the viewStaff.jsp page, the browser will ask me to confirm form resubmission. where am I doing wrong? however if i use response.sendRedirect method, i can't pass some necessary data to viewStaff.jsp page, unless if i'm using session.
therefore, how do i prevent confirm form resubmission if i'm using request dispatcher?
//this is the method that will handle the request and response object, and forward it to viewStaff.jsp
public void viewStaff(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher rd = request.getRequestDispatcher("/owners/viewStaff.jsp");
rd.forward(request, response);
}
//this is the method that will handle login error. if there are no error, then it will call the viewStaff method
public void login(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
String username = "";
String password = "";
String username_err = "";
String password_err = "";
username = request.getParameter("username");
password = request.getParameter("password");
if(username.isEmpty()) {
username_err = "please enter username";
}
if(password.isEmpty()) {
password_err = "please enter password";
}
if(!username.isEmpty()&&!password.isEmpty()) {
viewStaff(request, response);
} else {
request.setAttribute("username_err", username_err);
request.setAttribute("password_err", password_err);
RequestDispatcher rd = request.getRequestDispatcher("/owners/login.jsp");
rd.forward(request, response);
}
}
//this is the actual post method
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
String action = "";
action = request.getParameter("action");
if(action.equals("login")) {
login(request,response);
}
}
//this is my login.jsp
<c:set var="username_err" value="${requestScope.username_err}" />
<c:set var="password_err" value="${requestScope.password_err}" />
<c:set var="username" value="${param.username}" />
<c:set var="password" value="${param.password}" />
<main class="form-signin">
<form action="owner-controller?action=login" method="POST">
<h1 class="h3 mb-3 fw-normal">
Please sign in |
<p class="fw-lighter">owner</p>
</h1>
<div class="form-floating">
<input type="text" name="username"
class="form-control <c:if test="${not empty username_err}">is-invalid</c:if> "
value="${param.username}" id="floatingInput"
placeholder="Username"> <label for="floatingInput">Username</label>
</div>
<span style="color: red;">
<!-- username error here -->
</span>
<div class="form-floating">
<input type="password" name="password" class="form-control <c:if test="${not empty password_err}">is-invalid</c:if>"
id="floatingPassword" placeholder="Password"> <label
for="floatingPassword">Password</label>
</div>
<span style="color: red;">
<!-- password error here -->
</span>
<div class="checkbox mb-3">
<label> <input type="checkbox" value="remember-me">
Remember me
</label>
</div>
<button class="w-100 btn btn-lg btn-primary" type="submit">Sign
in</button>
</form>
<a href="owner-controller?action=login">click here to invoke GET method</a>
</main>
i did try using session attribute and it works, i don't have to confirm form resubmission. but somehow i want to know why request dispatcher does not work?