Reputation: 107
I'm new to using jsp and servlets so bear with me. The website I'm building has a login form to access the rest of the pages. When you login it sets a cookie with your username and the idea is for the other page to check for this cookie when you try to access them.
The login form runs this Login Servlet in the action:
public class LoginServlet extends HttpServlet implements Constants {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String Username = request.getParameter("username");
String password = request.getParameter("password");
if (StringUtils.isStringEmpty(Username) || StringUtils.isStringEmpty(password)) {
RequestDispatcher rd = request.getRequestDispatcher("/index.html");
rd.forward(request, response);
} else {
System.out.println(Username + password);
UserManager uMgr = new UserManager();
User user = uMgr.loginUser(Username, password);
if (user == null) {
RequestDispatcher rd = request.getRequestDispatcher("/login.jsp");
rd.forward(request, response);
} else {
if (user.isIsAdmin() == TRUE) {
Cookie loginCookie = new Cookie("ADMIN", Username);
//setting cookie to expiry in 30 mins
loginCookie.setMaxAge(30*60);
response.addCookie(loginCookie);
request.getSession(true).setAttribute(SESSION_ADMIN, user);
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
rd.forward(request, response);
} else {
RequestDispatcher rd = request.getRequestDispatcher("/login.jsp");
rd.forward(request, response);
}
}
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
On the index.jsp page I have this bit of code to run just after the opening body tag:
<jsp:forward page="CookieServlet"/>
This is the CookieServlet:
public class CookieServlet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String userName = null;
Cookie[] cookies = request.getCookies();
if(cookies !=null){
for(Cookie cookie : cookies){
if(cookie.getName().equals("ADMIN")) userName = cookie.getValue();
}
}
if(userName == null) response.sendRedirect("login.jsp");
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
The login code works and it creates the cookie for me and the cookieservlet that checks for the cookie also does the check for the cookie but none of the HTML displays on any of the pages with the <jsp:forward page="CookieServlet"/>
in it. Any ideas with what is wrong?
Upvotes: 1
Views: 108
Reputation: 3433
When you forward, you go from one page to another. Naturally the information on the new page is different from the information on the old page. It would be a bug if you navigate to a page and still have the old page displayed.
Upvotes: 1
Reputation: 691973
Nothing is wrong. That's what a forward is for. Forwarding to another resource is like saying: "I've done my part of the job for this request, now transfer the control to this other resource".
You should not check the login in the view. You shouldn't even check it in the controller. You should check it in a central place, before any controller is invoked: a servlet filter.
Upvotes: 0