title: How can I set a session variable in a servlet and get it in a JSP?
body: <p>I'm learning java and try to pass some variable from servlet to jsp page. Here is code from servlet page</p>
  
  <pre><code>@WebServlet("/Welcome")
  public class WelcomeServlet extends HttpServlet
  {
      private static final long serialVersionUID = 1L;
  
      protected void doGet(HttpServletRequest request, HttpServletResponse response)      throws ServletException, IOException
      {
          HttpSession session = request.getSession();
          session.setAttribute("MyAttribute", "test value");
  
          // response.sendRedirect("index.jsp");
          RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp");
          dispatcher.forward(request, response);
      }
  
  }
  </code></pre>
  
  <p>And simple jsp</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;My Index page&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  Index page
  &lt;br /&gt;
   &lt;% 
  Object sss = request.getAttribute("MyAttribute"); 
  String a = "22";
  
  %&gt;
  
     &lt;%= request.getAttribute("MyAttribute"); %&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Whatever I do attribete at jsp is null. </p>
  
  <p>What is wrong at this simple code?</p>