title: Redirecting to the root URL in servlet
body: <p>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, </p>
  
  <p>whenever the user types the URL <code>http://localhost:8888/</code>, he must take to the <code>dashboard</code> servlet (as mention in the <code>web.xml</code>)</p>
  
  <p><strong>In the <code>dashboard servlet</code>,</strong>  where session is checked </p>
  
  <p>if the session is available, then I will serve <code>welcome.html</code> page,
  else I redirect to <code>login servlet</code></p>
  
  <p><strong>In the <code>login servlet</code>,</strong></p>
  
  <p><code>GET</code> method,
  if the user have the session I redirect to the <code>dashboard servlet</code> 
  else I will the forward the <code>welcome.html</code> page</p>
  
  <p>after user entry the information, it will post the information to the <code>login servlet</code> (i.e., POST)</p>
  
  <p><code>POST</code> method,
  I will get the username and password from the request, I will check value is <code>admin</code> and <code>password</code></p>
  
  <p>if it is true create a session and redirect the user to the <code>dashboard servlet</code>
  else I will redirect the user to the <code>login servlet</code>(GET Method)</p>
  
  <p>web application should need to have only these 2 URL,</p>
  
  <p>(1) <code>http://localhost:8888/</code>
  (2) <code>http://localhost:8888/login</code></p>
  
  <p>user should not aware of the type of page I am serving to them(html or jsp) but I need to serve HTML page only(this my requirement)</p>
  
  <p>when I start the app engine, At first the request send to dashboard then it is redirected to the <code>login servlet</code> but then its making loop for the dashboard once again,</p>
  
  <p>I used <code>System.out.println()</code> to track the control my output is </p>
  
  <pre><code>The admin console is running at http://localhost:8888/_ah/admin
  Jul 25, 2017 11:12:55 AM com.google.appengine.tools.development.DevAppServerImpl doStart
  INFO: Dev App Server is now running
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  login servlet get method
  request no session
  --&gt;dashboard servlet
  get method
  no session,
  </code></pre>
  
  <blockquote>
    <p>QUESTION:</p>
  </blockquote>
  
  <p>How can solve this problem? </p>
  
  <p>If I change url pattern in <code>web.xml</code> as <code>&lt;url-pattern&gt;/index&lt;/url-pattern&gt;</code> and in the servlet whereever redirection to the <code>/</code> I changed to <code>/index</code> for the <code>dashboard servlet</code> it is working fine, but how can I make use of the "/" for servlet</p>
  
  <p>I have attached the source code below, </p>
  
  <p><strong>dashboard servlet</strong></p>
  
  <p>package servlet;</p>
  
  <pre><code>import java.io.IOException;
  
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpSession;
  
  @SuppressWarnings("serial")
  public class DashboardServlet extends HttpServlet {
      public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
          System.out.println("--&gt;dashboard servlet");
          System.out.println("get method");
          HttpSession session = req.getSession(false);
          if (session != null) {          
              System.out.println("session is available");
              req.getRequestDispatcher("/WEB-INF/welcome.html").forward(req, resp);
          } else {
              System.out.println("no session,");
              resp.sendRedirect("/login");
          }
      }
  }
  </code></pre>
  
  <p><strong>Login Servlet</strong></p>
  
  <pre><code>package servlet;
  
  import java.io.IOException;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpSession;
  
  @SuppressWarnings("serial")
  public class LoginServlet extends HttpServlet {
      public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
          // TODO Auto-generated method stub
          System.out.println("--&gt;&gt;&gt;login servlet post method");
          String name = req.getParameter("username");
          String password = req.getParameter("password");
          if (name.equals("admin") &amp;&amp; password.equals("password")) {
              HttpSession session = req.getSession();
              session.setAttribute("sessionUserName", name);
              resp.sendRedirect("/");
  
          } else {
              resp.sendRedirect("/login");
          }
      }
  
      public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  
          System.out.println("login servlet get method");
          HttpSession session = req.getSession(false);
          if (session != null) {
              System.out.println("request with session");
              resp.sendRedirect("/");
          } else {
              System.out.println("request no session");
              req.getRequestDispatcher("/WEB-INF/login.html").forward(req, resp);
          }
  
      }
  }
  </code></pre>
  
  <p><strong>web.xml</strong></p>
  
  <pre><code>&lt;web-app&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;Login&lt;/servlet-name&gt;
          &lt;servlet-class&gt;servlet.LoginServlet&lt;/servlet-class&gt;
      &lt;/servlet&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;Login&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/login&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;Dashboard&lt;/servlet-name&gt;
          &lt;servlet-class&gt;servlet.DashboardServlet&lt;/servlet-class&gt;
      &lt;/servlet&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;Dashboard&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p><strong>appengine-web.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;appengine-web-app xmlns="http://appengine.google.com/ns/1.0"&gt;
      &lt;application&gt;&lt;/application&gt;
      &lt;version&gt;1&lt;/version&gt;
      &lt;threadsafe&gt;true&lt;/threadsafe&gt;
      &lt;system-properties&gt;
          &lt;property name="java.util.logging.config.file" value="WEB-INF/logging.properties" /&gt;
      &lt;/system-properties&gt;
      &lt;sessions-enabled&gt;true&lt;/sessions-enabled&gt;
  &lt;/appengine-web-app&gt;
  </code></pre>
  
  <p><strong>login.html</strong></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Insert title here&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form action="/login" method="post"&gt;
          User name :&lt;input type='text' name='username'&gt;Password :&lt;input
              type='password' name='password'&gt;&lt;input type='submit'
              value='Submit'&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>welcome.html</strong></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta charset="ISO-8859-1"&gt;
  &lt;title&gt;Insert title here&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;welcome here
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Thank you,</p>