title: use one session attribute for different servlets
body: <p>I've made a login environment for a user, i've retrieved username and password from database for login.And set user name as a string in a session.now i want to use same user name for another servlet.But i can't use the same string in two servlet.How can i use the same session attribute for another servlet.Below is my code.
  <code>loginservlet</code></p>
  
  <pre><code>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.*;
  @WebServlet(urlPatterns = {"/LoginServlet"})
  public class LoginServlet extends HttpServlet {
  
  String userid, pwd;
  
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
      response.setContentType("text/html;charset=UTF-8");
      PrintWriter out = response.getWriter();
      Connection connection = null;
      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);
              //out.println("welcome " + userid);
              //out.println("&lt;a href='logout.jsp'&gt;Log out&lt;/a&gt;");
              response.sendRedirect("success.jsp");
          } else {
  
              request.setAttribute("errorMessage", "Invalid user or password");
              RequestDispatcher rd = getServletContext().getRequestDispatcher("/index.jsp");
              rd.include(request, response);
  
          }
  
      } catch (Exception e) {
          e.printStackTrace();
      }
  
  }
  }
  </code></pre>
  
  <p><code>profile servlet</code> here i want to use the session value userid</p>
  
  <pre><code>import java.io.IOException;
  import java.io.PrintWriter;
  import java.sql.Connection;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.sql.Statement;
  import javax.servlet.RequestDispatcher;
  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 javax.servlet.http.HttpSession;
  
  
  @WebServlet(urlPatterns = {"/usesesvaluegetdbdata"})
  public class usesesvaluegetdbdata extends HttpServlet {
  
  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
      response.setContentType("text/html;charset=UTF-8");
      PrintWriter out = response.getWriter();
        Connection connection = null;
      Statement statement = null;
      String userid;
  
      try {
          dbconnector dbc = new dbconnector();
          connection = dbc.Open();
          PreparedStatement ps = connection.prepareStatement("select eid,ename from member where uname='" + userid + "' and pass='" + pwd + "'");
  
  
  
      } catch (Exception e) {
          e.printStackTrace();
      }
  
  }
  </code></pre>