You can send requests to the servlet and then redirect requests to another servlet, if necessary.
In your case, after checking, you can save the result in an attribute, and then transfer control to another servlet. (if that's what you want to do)
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/urlToServlet"); dispatcher.forward(request, response);
And here is how to handle cookies.
create and send cookies
Cookie userCookie = new Cookie("name", "value"); userCookie.setMaxAge(60*60*24*365); //Store cookie for 1 year response.addCookie(userCookie);
read cookie from client
String cookieName = "somecookie"; Cookie[] cookies = request.getCookies(); if (cookies != null) { for(int i=0; i<cookies.length; i++) { Cookie cookie = cookies[i]; if (cookieName.equals(cookie.getName())) { doSomethingWith(cookie.getValue()); } } } else { //do something else for firsttime visitors }
Do you use session tracking cookies? If yes, use HttpSession . Using HttpSession, there is no need to directly enable cookies for session tracking.
For example, on a simple login page, this is what you do
HttpSession session = request.getSession(); session.setAttribute("username",username); In other pages, if(session.getAttribute("username")==null) {
source share