title: jsp login authentication with database user
body: <p>i would like to authenticate user and password which the data store in database. I'm using Netbeans IDE in my decelopment. The problem I faced is it keep show an error page with content internal server error. Can someone one help me to check my code. I have some code like this:</p>
  
  <p>LoginForm.jsp:</p>
  
  <pre><code>    &lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt; 
  &lt;html&gt; 
      &lt;head&gt; 
          &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt; 
          &lt;title&gt;Login Form&lt;/title&gt; 
      &lt;/head&gt; 
      &lt;body bgcolor="#D8CEF6"&gt; 
          &lt;h1&gt;Login Page&lt;/h1&gt; 
          &lt;fieldset&gt;
             &lt;center&gt;
        &lt;legend&gt;&lt;h2&gt;Sign in Details&lt;/h2&gt; &lt;/legend&gt;
          &lt;form action="LoginCheck.jsp" method="post"&gt; 
  
              &lt;br/&gt;Username:&lt;input type="text" name="username"/&gt; 
              &lt;br/&gt;Password:&lt;input type="password" name="password"/&gt; 
              &lt;br/&gt;
              &lt;tr&gt;
              &lt;td&gt;usertype&lt;/td&gt;
              &lt;td&gt;
                  &lt;select name="usertype"&gt;
                      &lt;option value="student"&gt;student&lt;/option&gt;
                      &lt;option value="faculty"&gt;faculty&lt;/option&gt;
                  &lt;/select&gt;
              &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;
              &lt;br/&gt;&lt;input type="submit" value="Submit" name="bt"/&gt; 
                  &lt;/td&gt;
              &lt;/tr&gt;
          &lt;/form&gt; 
      &lt;/center&gt; 
                  &lt;/fieldset&gt;
          &lt;/body&gt; 
          &lt;/html&gt;
  </code></pre>
  
  <p>LoginCheck.jsp:</p>
  
  <pre><code>&lt;%@page import ="java.sql.*" %&gt;
  &lt;%@page import ="java.io.IOException" %&gt;
  &lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt; 
  &lt;%@page import="java.io.*"%&gt;
      &lt;html&gt; 
          &lt;head&gt;
              &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
              &lt;title&gt;Login Check&lt;/title&gt; 
          &lt;/head&gt; 
          &lt;body&gt; 
              &lt;%
              String username = request.getParameter("username");
              String password = request.getParameter("password");
              String userType = request.getParameter("usertype");
              String dbURL = "jdbc:derby://localhost:1527/learningApp";
              String dbuser = "learningApp";
              String dbpassword = "p@ssw0rd";
              Connection theConnection = null;
              PreparedStatement theStatement = theConnection.prepareStatement("select * from USERNAME where username=? and password=?");
              try{
              Class.forName("org.apache.derby.jdbc.ClientDriver");
              }catch(Exception ex){
              System.out.println("Class Not Found");
              }
              try{
              theConnection = DriverManager.getConnection(dbURL, dbuser, dbpassword);
              }catch(Exception ex){
              System.out.println("Connection Error");
              }
              try{
                  theStatement.setString(1,request.getParameter("username"));
                  theStatement.setString(2,request.getParameter("password"));
                  ResultSet theResult = theStatement.executeQuery();
                  if(theResult.next()){
              response.sendRedirect("Home.jsp");
          }
  
              }catch(Exception ex){
              System.out.println("Statement Error");
              }
  
  
              %&gt;
      &lt;/body&gt; 
      &lt;/html&gt;
  </code></pre>
  
  <p>My database name is learningApp with password p@ssw0rd
  and have a table named USERNAME with a user(username = alice, password = alice)</p>