title: make a simple login authentication using java class,servlet,jsp and session and database
body: <p>I want to make a login process using mvc structure where i send userid and password from  <code>index.jsp</code> page.If they match with the database value then it will go to the <code>stater001.jsp</code>.In <code>index.jsp</code> i've 2 fields userid and password from.I've post userid and pwd into <code>LoginServlet</code> servlet. </p>
  
  <p><code>LoginServlet</code> process <code>userid , pwd</code> making the instance of <code>loginAuthentication</code> class.<code>loginAuthentication</code> class checks whether <code>userid</code> and <code>password</code> exists in database or not with <code>select query</code>.I have also used setter getter class<code>user</code> to set <code>userid</code> and <code>pwd</code> into session.I tried to do the whole login process in an organised way.But i'm stucked with error i couldn't under stand what is the problem please help me to solve this.First i tried it without using mvc pattern then it worked fine.But now it is not working.
  Below is my previous code all process in one servlet</p>
  
  <pre><code>package DataAccess;
  import DataAccess.dbconnector;
  import java.io.IOException;
  import java.io.PrintWriter;
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
    import java.io.*;
   import javax.servlet.*;
  import javax.servlet.http.*;
   import java.sql.*;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  
  @WebServlet(urlPatterns = {"/LoginServlet"})
  public class LoginServlet extends HttpServlet {
  
  String userid, pwd;
    Connection connection = null;
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
      response.setContentType("text/html;charset=UTF-8");
      PrintWriter out = response.getWriter();
  
      Statement statement = null;
     userid = request.getParameter("uname");
      pwd = request.getParameter("pass");
      try {
          dbconnector dbc = new dbconnector();
          connection = dbc.Open();
          PreparedStatement ps = connection.prepareStatement("select * from member where uname='" + userid + "' and pass='" + pwd + "'");
  
          ResultSet rs = ps.executeQuery();
          if (rs.next()) {
              HttpSession session = request.getSession();
              session.setAttribute("userid", userid);
              response.sendRedirect("view/starter001.jsp");
          } else {
  
              request.setAttribute("errorMessage", "Invalid user or password");
              RequestDispatcher rd = getServletContext().getRequestDispatcher("/index.jsp");
              rd.include(request, response);
  
          }
  
      } catch (Exception e) {
          e.printStackTrace();
      }
   finally {
          try {
              connection.close();
          } catch (SQLException ex) {
              Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
  
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And in my new code here i've stucked with error in execution
  <a href="https://i.stack.imgur.com/lqJzu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lqJzu.png" alt="enter image description here"></a>
  And here i've attached my new edited codes where i tried to do this in an organised way .My errors have been gone but not showing the userid after login    <a href="http://pastebin.com/RKyieMqn" rel="nofollow noreferrer">http://pastebin.com/RKyieMqn</a>
  Please consider my question</p>
  
  <p>at first when all codes were in loginservlet then it was like<a href="https://i.stack.imgur.com/6uwFw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6uwFw.png" alt="enter image description here"></a>  but when i start using mvc structure it is not showing the username</p>