title: Servlet JSP getparameter getattribute returning null
body: <p>I'm having trouble with passing servlet variables to jsp.</p>
  
  <p>Of course, I also have the web.xml set for the servlet already</p>
  
  <pre><code>&lt;servlet&gt;
      &lt;servlet-name&gt;databaseServlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;Servlet.databaseServlet&lt;/servlet-class&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;databaseServlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/dbServlet&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p>The result is all of the name, owner, species and sex values are null. Can someone help me with this? Thanks</p>
  
  <p>P.S: I also tried to use request.getSession().setAttribute in the servlet, didn't work either</p>
  
  <p>P.P.S: So if I make the following changes:</p>
  
  <p>databaseServlet.java</p>
  
  <pre><code>package Servlet;
  
  import javax.servlet.*;
  import javax.servlet.http.*;
  import java.io.*;
  import java.sql.*;
  import java.util.ArrayList;
  
  @SuppressWarnings("serial")
  
  public class databaseServlet extends HttpServlet {
  private Connection conn;
  private Statement statement;
  
  String name;
  String owner;
  String species;
  String sex;
  String birth;
  String death;
  
  public void init(ServletConfig config) throws ServletException {
      try {
          Class.forName("com.mysql.jdbc.Driver");
          conn = DriverManager.getConnection(
                  "jdbc:mysql://localhost/STUDENTS",
                  "root",
                  "");
          statement = conn.createStatement();
  
          String sql = "SELECT name, owner, species, sex, birth, death FROM pet";
          ResultSet rs = statement.executeQuery(sql);
  
          //STEP 5: Extract data from result set
          while(rs.next()){
              //Retrieve by column name
              name  = rs.getString("name");
              owner = rs.getString("owner");
              species = rs.getString("species");
              sex = rs.getString("sex");
              birth = rs.getString("birth");
              death = rs.getString("death");
          }
          rs.close();
      }
      catch (Exception e) {
          e.printStackTrace();
      }
  }
  
  protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      request.setAttribute("NAME", "Hello");
      System.out.println(name);
      request.setAttribute("OWNER",owner);
      request.setAttribute("SPECIES",species);
      request.setAttribute("SEX", sex);
      RequestDispatcher dispatcher=getServletConfig().getServletContext().getRequestDispatcher("/dbServlet.jsp");
      dispatcher.forward(request,  response);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>and this is my new jsp:</p>
  
  <pre><code>&lt;body&gt;
  Name="${databaseServlet.NAME}" 
  Owner="${databaseServlet.OWNER}" 
  Species="&lt;%= request.getAttribute("SPECIES") %&gt;"
  Sex="&lt;%= request.getSession().getAttribute("SEX") %&gt;"
  
  &lt;/body&gt;
  </code></pre>
  
  <p>both Name and Owner returns empty string, while Species and Sex still returns NULL</p>
  
  <p>basically what I'm trying to do is to access MySQL database to retrieve variables from a table, and display it using JSP</p>