Redirecting To The Root Url In Servlet
Hi I did simple web application with servlet, to serve login and welcome page based on the session available. I had attached the code below, whenever the user types the URL http:/
Solution 1:
You should not check only whether session is null. You should put a value like 'logged_in' into session as attribute. Then, control it for deciding where to route.
Update:
As a quick solution, route doGet inside LoginServlet to doPost and change doPost as below:
publicvoiddoPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("login servlet get method");
HttpSession session = req.getSession(false);
if (session != null && session.getAttribute("sessionUserName")!=null) {
System.out.println("request with session");
resp.sendRedirect("/");
} else {
String name = req.getParameter("username");
String password = req.getParameter("password");
if (name.equals("admin") && password.equals("password")) {
session = req.getSession();
session.setAttribute("sessionUserName", name);
resp.sendRedirect("/");
} else {
System.out.println("request no session");
req.getRequestDispatcher("/WEB-INF/login.html").forward(req, resp);
}
}
}
Post a Comment for "Redirecting To The Root Url In Servlet"