title: HTTP Status 500 - Unable to compile class for JSP: for using jsp mongodb
body: <p>i am new to jsp and Mongodb I have to search data from mongodb based on my input in search field using jsp</p>
  
  <p>here is my code </p>
  
  <p>NewFIle.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;Insert title here&lt;/title&gt;
  &lt;/head&gt;
  &lt;body bgcolor="#d0d0d0"&gt;
  &lt;br&gt;&lt;/br&gt;
  &lt;br&gt;&lt;/br&gt;
  &lt;br&gt;&lt;/br&gt;
  &lt;h2&gt;&lt;center&gt;enter Hospital details&lt;/center&gt;&lt;/h2&gt;
  &lt;form name="searchhospital"action="searchhospital.jsp"method="post"&gt;
  &lt;table border="2"align=center&gt;
  &lt;tr&gt;&lt;td&gt;
  &lt;b&gt;enter the id u want tho search:&lt;/b&gt;
  &lt;input type="text"name="id"/&gt;
  &lt;br/&gt;
  &lt;br/&gt;
  &lt;/td&gt;&lt;/tr&gt;
  &lt;/table&gt;
  &lt;center&gt;
  &lt;input type="submit"value="submit"/&gt;
  &lt;input type="reset"value="reset"/&gt;
  &lt;/form&gt;
  
  
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>and search file as 
  searchhospital.jsp</p>
  
  <pre><code>&lt;%@page import="java.util.*"%&gt;
  
  
  &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;Insert title here&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;%@page import="com.mongodb.MongoClient"%&gt;
  &lt;%@page import="com.mongodb.MongoException"%&gt;
  &lt;%@page import="com.mongodb.WriteConcern"%&gt;
  &lt;%@page import="com.mongodb.DB"%&gt;
  &lt;%@page import="com.mongodb.DBCollection"%&gt;
  &lt;%@page import="com.mongodb.BasicDBObject"%&gt;
  &lt;%@page import="com.mongodb.DBObject"%&gt;
  &lt;%@page import="com.mongodb.DBCursor"%&gt;
  &lt;table&gt;
  &lt;tr&gt;
  &lt;th&gt;name&lt;/th&gt;
  &lt;th&gt;id&lt;/th&gt;
  &lt;th&gt;age&lt;/th&gt;
  &lt;th&gt;branch&lt;/th&gt;
  &lt;/tr&gt;
  
  &lt;%
  try{    
  
      // To connect to mongodb server
      MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
  
      // Now connect to your databases
      DB db = mongoClient.getDB( "HealthCare" );
     System.out.println("Connect to database successfully");
  //    boolean auth = db.authenticate("","");
  //    System.out.println("Authentication: "+auth);
  
     DBCollection coll = db.getCollection("DocInfoTable");
      System.out.println("Collection DocInfoTable selected successfully");
  
     BasicDBObject query = new BasicDBObject();
      BasicDBObject fields = new BasicDBObject();
      String s2=request.getParameter("id");
  
      query.put("DocLastname",s2 );
  
      // Assign 1 or 0 for projecting a specific field.
      fields.put("_id", 0);
      fields.put("DOcFirstName", 1);
      fields.put("DOcFirstName", 1);
      fields.put("DocMiddleName", 1);
      fields.put("DocQualification", 1);
  
      DBCursor cursor = coll.find(query,fields);
      int i = 1;
  
      while (cursor.hasNext()) { 
           System.out.println(cursor.next()); 
      %&gt;
  
  
      &lt;tr&gt;
  &lt;td&gt;&lt;%=query.getString("DOcFirstName")%&gt;&lt;/td&gt;
  &lt;td&gt;&lt;%=query.getString("DOcFirstName")%&gt;&lt;/td&gt;
  &lt;td&gt;&lt;%=query.getString("DocMiddleName")%&gt;&lt;/td&gt;
  &lt;td&gt;&lt;%=query.getString("DocQualification")%&gt;&lt;/td&gt;
  &lt;/tr&gt;
         &lt;%
  
         i++;
      }
  
  }catch(Exception e){
      System.err.println( e.getClass().getName() + ": " + e.getMessage() );
   }
         %&gt;
  
  
  
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Iam getting error as follow</p>
  
  <pre><code>HTTP Status 500 - Unable to compile class for JSP: 
  
  type Exception report
  
  message Unable to compile class for JSP: 
  
  description The server encountered an internal error that prevented it from fulfilling this request.
  
  exception
  org.apache.jasper.JasperException: Unable to compile class for JSP: 
  
  An error occurred at line: [15] in the generated java file: [E:\all softwares\apache-tomcat-9.0.0.M9-windows-x64\apache-tomcat-9.0.0.M9\work\Catalina\localhost\happy\org\apache\jsp\searchhospital_jsp.java]
  Only a type can be imported. com.mongodb.MongoClient resolves to a package
  
  An error occurred at line: [16] in the generated java file: [E:\all softwares\apache-tomcat-9.0.0.M9-windows-x64\apache-tomcat-9.0.0.M9\work\Catalina\localhost\happy\org\apache\jsp\searchhospital_jsp.java]
  Only a type can be imported. com.mongodb.MongoException resolves to a package
  
  An error occurred at line: [17] in the generated java file: [E:\all softwares\apache-tomcat-9.0.0.M9-windows-x64\apache-tomcat-9.0.0.M9\work\Catalina\localhost\happy\org\apache\jsp\searchhospital_jsp.java]
  Only a type can be imported. com.mongodb.WriteConcern resolves to a package
  
  An error occurred at line: [18] in the generated java file: [E:\all softwares\apache-tomcat-9.0.0.M9-windows-x64\apache-tomcat-9.0.0.M9\work\Catalina\localhost\happy\org\apache\jsp\searchhospital_jsp.java]
  Only a type can be imported. com.mongodb.DB resolves to a package
  
  An error occurred at line: [19] in the generated java file: [E:\all softwares\apache-tomcat-9.0.0.M9-windows-x64\apache-tomcat-9.0.0.M9\work\Catalina\localhost\happy\org\apache\jsp\searchhospital_jsp.java]
  Only a type can be imported. com.mongodb.DBCollection resolves to a package
  
  An error occurred at line: [20] in the generated java file: [E:\all softwares\apache-tomcat-9.0.0.M9-windows-x64\apache-tomcat-9.0.0.M9\work\Catalina\localhost\happy\org\apache\jsp\searchhospital_jsp.java]
  Only a type can be imported. com.mongodb.BasicDBObject resolves to a package
  
  An error occurred at line: [21] in the generated java file: [E:\all softwares\apache-tomcat-9.0.0.M9-windows-x64\apache-tomcat-9.0.0.M9\work\Catalina\localhost\happy\org\apache\jsp\searchhospital_jsp.java]
  Only a type can be imported. com.mongodb.DBObject resolves to a package
  
  An error occurred at line: [22] in the generated java file: [E:\all softwares\apache-tomcat-9.0.0.M9-windows-x64\apache-tomcat-9.0.0.M9\work\Catalina\localhost\happy\org\apache\jsp\searchhospital_jsp.java]
  Only a type can be imported. com.mongodb.DBCursor resolves to a package
  
  An error occurred at line: 33 in the jsp file: /searchhospital.jsp
  MongoClient cannot be resolved to a type
  30: try{    
  31:     
  32:     // To connect to mongodb server
  33:     MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
  34:         
  35:     // Now connect to your databases
  36:     DB db = mongoClient.getDB( "HealthCare" );
  
  
  An error occurred at line: 33 in the jsp file: /searchhospital.jsp
  MongoClient cannot be resolved to a type
  30: try{    
  31:     
  32:     // To connect to mongodb server
  33:     MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
  34:         
  35:     // Now connect to your databases
  36:     DB db = mongoClient.getDB( "HealthCare" );
  
  
  An error occurred at line: 36 in the jsp file: /searchhospital.jsp
  DB cannot be resolved to a type
  33:     MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
  34:         
  35:     // Now connect to your databases
  36:     DB db = mongoClient.getDB( "HealthCare" );
  37:    System.out.println("Connect to database successfully");
  38: //    boolean auth = db.authenticate("","");
  39: //    System.out.println("Authentication: "+auth);
  
  
  An error occurred at line: 41 in the jsp file: /searchhospital.jsp
  DBCollection cannot be resolved to a type
  38: //    boolean auth = db.authenticate("","");
  39: //    System.out.println("Authentication: "+auth);
  40:    
  41:    DBCollection coll = db.getCollection("DocInfoTable");
  42:     System.out.println("Collection DocInfoTable selected successfully");
  43:         
  44:    BasicDBObject query = new BasicDBObject();
  
  
  An error occurred at line: 44 in the jsp file: /searchhospital.jsp
  BasicDBObject cannot be resolved to a type
  41:    DBCollection coll = db.getCollection("DocInfoTable");
  42:     System.out.println("Collection DocInfoTable selected successfully");
  43:         
  44:    BasicDBObject query = new BasicDBObject();
  45:     BasicDBObject fields = new BasicDBObject();
  46:     String s2=request.getParameter("id");
  47: 
  
  
  An error occurred at line: 44 in the jsp file: /searchhospital.jsp
  BasicDBObject cannot be resolved to a type
  41:    DBCollection coll = db.getCollection("DocInfoTable");
  42:     System.out.println("Collection DocInfoTable selected successfully");
  43:         
  44:    BasicDBObject query = new BasicDBObject();
  45:     BasicDBObject fields = new BasicDBObject();
  46:     String s2=request.getParameter("id");
  47: 
  
  
  An error occurred at line: 45 in the jsp file: /searchhospital.jsp
  BasicDBObject cannot be resolved to a type
  42:     System.out.println("Collection DocInfoTable selected successfully");
  43:         
  44:    BasicDBObject query = new BasicDBObject();
  45:     BasicDBObject fields = new BasicDBObject();
  46:     String s2=request.getParameter("id");
  47: 
  48:     query.put("DocLastname",s2 );
  
  
  An error occurred at line: 45 in the jsp file: /searchhospital.jsp
  BasicDBObject cannot be resolved to a type
  42:     System.out.println("Collection DocInfoTable selected successfully");
  43:         
  44:    BasicDBObject query = new BasicDBObject();
  45:     BasicDBObject fields = new BasicDBObject();
  46:     String s2=request.getParameter("id");
  47: 
  48:     query.put("DocLastname",s2 );
  
  
  An error occurred at line: 57 in the jsp file: /searchhospital.jsp
  DBCursor cannot be resolved to a type
  54:     fields.put("DocMiddleName", 1);
  55:     fields.put("DocQualification", 1);
  56:    
  57:     DBCursor cursor = coll.find(query,fields);
  58:     int i = 1;
  59:         
  60:     while (cursor.hasNext()) { 
  
  
  Stacktrace:
      org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
      org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:212)
      org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:457)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:377)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:349)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:333)
      org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:600)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:368)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
      org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
  
  
  note The full stack trace of the root cause is available in the Apache Tomcat/9.0.0.M9 logs.
  
  
  Apache Tomcat/9.0.0.M9
  </code></pre>
  
  <p>Please help me ragarding this issue.If my code is wrong than please provide good code to search data from Mongodb using jsp(or servlets)  </p>