title: I want to create a login page using servlets
body: <p>I want to create a login page using Servlet &amp; JSP.</p>
  
  <p>I ve created a page which gets Username &amp; password.</p>
  
  <p>I made a database with a table which contains Username &amp; password.</p>
  
  <pre><code>&lt;form action="LoginPage" method="POST"&gt;
      User name: &lt;input type="text" name="userName" size="20"&gt;&lt;br&gt;
      Password: &lt;input type="password" name="password" size="20"&gt;
      &lt;br&gt;&lt;br&gt;
      &lt;input type="submit" value="Submit"&gt;
  &lt;/form&gt; 
  </code></pre>
  
  <p>I entered the below code in doPost()</p>
  
  <pre><code> response.setContentType("text/html;charset=UTF-8");
      PrintWriter out = response.getWriter();
      String userName = request.getParameter("userName").toString();
      String passWord = request.getParameter("password").toString();
      Connection con = null;
      String url = "jdbc:mysql://localhost:3306/";
      String dbName = "userdb";
      String driver = "com.mysql.jdbc.Driver";
      String user = "root"; 
      String password = "1234";
      try {
          Class.forName(driver).newInstance();
          Connection conn = DriverManager.getConnection(url+dbName, user, password);
          PreparedStatement pstmt;
          String sql = "SELECT USR_NAME FROM LOGIN WHERE USR_NAME='userName'";
          pstmt = conn.prepareStatement(sql);
          ResultSet rs=pstmt.executeQuery();
          String usr = null;
          String pass = null;
          while(rs.next())
          {
              pass = rs.getString(3);
          }
          if(pass != null &amp;&amp; pass.equals(passWord))
          {
              out.println("&lt;html&gt;");
              out.println("&lt;head&gt;");
              out.println("&lt;title&gt;Login Sucessfull&lt;/title&gt;");
              out.println("&lt;/head&gt;");
              out.println("&lt;body&gt;");
              out.println("&lt;h1&gt;Login Sucessfull " + request.getContextPath () + "&lt;/h1&gt;");
              out.println("&lt;p&gt;Welcome&lt;/p&gt; " + userName);
              out.println("&lt;/body&gt;");
              out.println("&lt;/html&gt;");
  
              out.close();
          }
  
      } catch (Exception e) {
          out.println("&lt;html&gt;");
          out.println("&lt;head&gt;");
          out.println("&lt;title&gt;Login is not Sucessfull&lt;/title&gt;");
          out.println("&lt;/head&gt;");
          out.println("&lt;body&gt;");
          out.println("&lt;h1&gt;Login is not Sucessfull " + request.getContextPath () + "&lt;/h1&gt;");
          out.println("&lt;p&gt;Wrong Username Or Password&lt;/p&gt; ");
          out.println("&lt;/body&gt;");
          out.println("&lt;/html&gt;");
  
          out.close();
  </code></pre>
  
  <p>And I dont know how to make it work.
  Any Quick Fix Available For me?</p>
  
  <p>Its not a big project, i jus want a Login page which gets username &amp; password 
  then the servlet will search the username in DB then checks for password.
  I have made the changes you guys told. <strong>But the output looks like something
  wrong in try block, Because, I get the Login not successful page. 
  I put the code "Login not successful Page" in Catch block.</strong> </p>