简体   繁体   English

HTTP状态500-无法为JSP编译类:用于使用jsp mongodb

[英]HTTP Status 500 - Unable to compile class for JSP: for using jsp mongodb

i am new to jsp and Mongodb I have to search data from mongodb based on my input in search field using jsp 我是jsp和Mongodb的新手,我必须根据我在jsp中对搜索字段的输入,从mongodb中搜索数据

here is my code 这是我的代码

NewFIle.jsp NewFIle.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body bgcolor="#d0d0d0">
<br></br>
<br></br>
<br></br>
<h2><center>enter Hospital details</center></h2>
<form name="searchhospital"action="searchhospital.jsp"method="post">
<table border="2"align=center>
<tr><td>
<b>enter the id u want tho search:</b>
<input type="text"name="id"/>
<br/>
<br/>
</td></tr>
</table>
<center>
<input type="submit"value="submit"/>
<input type="reset"value="reset"/>
</form>



</body>
</html>

and search file as searchhospital.jsp 并搜索文件为searchhospital.jsp

<%@page import="java.util.*"%>


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%@page import="com.mongodb.MongoClient"%>
<%@page import="com.mongodb.MongoException"%>
<%@page import="com.mongodb.WriteConcern"%>
<%@page import="com.mongodb.DB"%>
<%@page import="com.mongodb.DBCollection"%>
<%@page import="com.mongodb.BasicDBObject"%>
<%@page import="com.mongodb.DBObject"%>
<%@page import="com.mongodb.DBCursor"%>
<table>
<tr>
<th>name</th>
<th>id</th>
<th>age</th>
<th>branch</th>
</tr>

<%
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()); 
    %>


    <tr>
<td><%=query.getString("DOcFirstName")%></td>
<td><%=query.getString("DOcFirstName")%></td>
<td><%=query.getString("DocMiddleName")%></td>
<td><%=query.getString("DocQualification")%></td>
</tr>
       <%

       i++;
    }

}catch(Exception e){
    System.err.println( e.getClass().getName() + ": " + e.getMessage() );
 }
       %>




</body>
</html>

Iam getting error as follow 我得到错误如下

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

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) 请帮助我解决这个问题。如果我的代码不正确,请提供良好的代码以使用jsp(或servlet)从Mongodb中搜索数据

Well, as your error message says, it can't import the mongodb classes. 好吧,正如您的错误消息所说,它无法导入mongodb类。

Only a type can be imported. com.mongodb.MongoClient resolves to a package Only a type can be imported. com.mongodb.MongoClient resolves to a package Now, this doesn't look like a package name to me, but rather, a specific class in a package, so i doubt that error is quite right. Only a type can be imported. com.mongodb.MongoClient resolves to a package现在,这看起来并不像一个包名给我,而是一类特殊的包装,所以我怀疑这个错误是完全正确的。

Is it possible your mongodb library isn't in the servlet container's libs folder or your application's libs folder? 您的mongodb库是否可能不在servlet容器的libs文件夹或应用程序的libs文件夹中?

MongoClient contains class within itself. MongoClient本身包含类。 Hence, importing MongoClient on jsp itself is not allowed. 因此,不允许在jsp本身上导入MongoClient。

What you need to do here is to submit the data to a servlet handling your request and take the code to the servlet. 您需要在此处将数据提交给处理您的请求的servlet,并将代码带到servlet。 It is never a good practice to keep complete database connection code in the jsp. 在jsp中保留完整的数据库连接代码永远不是一个好习惯。

I hope this helps. 我希望这有帮助。 Let me know if further help is required 让我知道是否需要进一步的帮助

I just solved this problem. 我刚刚解决了这个问题。 You need put "mongo-java-driver-3.5.0.jar" and "mongo-java-driver-3.5.0-javadoc.jar" into tomcat's lib. 您需要将“ mongo-java-driver-3.5.0.jar”和“ mongo-java-driver-3.5.0-javadoc.jar”放入tomcat的lib中。 Also in you need put these two jar into lib which is under WEB-INF. 同样,您需要将这两个jar放入WEB-INF下的lib中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM