title: not able to use jsp variable in java
body: <p>We want to access a variable from jsp inside a java class in same file. Can someone pls suggest how to use it ?</p>
  
  <p>Below is the code. In this we are using <code>data</code> variable in jsp tags &amp; we want to access the value of <code>data</code> inside <code>DummyDB</code> class of java.</p>
  
  <p>JSP File:</p>
  
  <pre><code>&lt;%@page import="java.util.ArrayList"%&gt;
  &lt;%@page import="java.util.List"%&gt;
  &lt;%@page import="java.util.StringTokenizer"%&gt;
  &lt;%@ page import="java.sql.*" %&gt;
      &lt;%@ page import="java.io.*"%&gt;
      &lt;%@ page import="java.util.*"%&gt;
      &lt;%@ page import="java.net.*" %&gt;
  
  &lt;%
  
  HttpSession Session = request.getSession(true);
  
  String data = Session.getAttribute("data").toString();
  
  %&gt;
  
  &lt;%!
  
  public class DummyDB {
      private int totalCountries;
  
     RequestDispatcher rd = request.getRequestDispatcher("include.jsp");
  rd.include(request, response); 
  
      private List&lt;String&gt; countries;
      public DummyDB() {
          countries = new ArrayList&lt;String&gt;();
          StringTokenizer st = new StringTokenizer(data, ",");
  
          while(st.hasMoreTokens()) {
              countries.add(st.nextToken().trim());
          }
          totalCountries = countries.size();
      }
  
      public List&lt;String&gt; getData(String query) {
          String country = null;
          query = query.toLowerCase();
          List&lt;String&gt; matched = new ArrayList&lt;String&gt;();
          for(int i=0; i&lt;totalCountries; i++) {
  
              country = countries.get(i).toLowerCase();
              if(country.startsWith(query)) {
                  matched.add(countries.get(i));
              }
          }
          return matched;
      }
          }
  %&gt;
  </code></pre>