0

I am using JSONObject but when I import import org.json.simple.JSONObject; then it shows compilation error as follows:

i.e The type java.util.Map$Entry cannot be resolved. It is indirectly referenced from required .class files.

Please help me to resolve this.

package Servlet;
import java.io.FileReader; 
import java.io.IOException; 
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.simple.JSONObject; 
import org.json.simple.parser.JSONParser;
/**
  * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public LoginServlet() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
}

/**
 *    @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
     response.setContentType("text/html");  
        PrintWriter out=response.getWriter();  

        request.getRequestDispatcher("link.html").include(request, response);  

        String name=request.getParameter("name");  
        String password=request.getParameter("password");  

        JSONParser parser=new JSONParser();
        Object obj=parser.parse(new FileReader("/home/mukesh/Downloads/Downloads/Json Files/mukesh.json"));
        JSONObject ob=(JSONObject)obj;
        if(ob.get(name)==password){
            out.print("You are successfully logged in!");  
            out.print("<br>Welcome, "+name);  

            Cookie ck=new Cookie("name",name);  
            response.addCookie(ck);  
        }else{  
            out.print("sorry, username or password error!");  
            request.getRequestDispatcher("login.html").include(request, response);  
        }  

        out.close(); 
    }

}
Kruti Patel
  • 1,422
  • 2
  • 23
  • 36
Mukesh Gupta
  • 1,373
  • 3
  • 17
  • 42
  • I'm guessing you are using JDK 1.8? Are you using Eclipse also, if so, what version? – Damien O'Reilly Oct 20 '15 at 09:42
  • 1
    I am using eclipse indigo. – Mukesh Gupta Oct 20 '15 at 10:03
  • 1
    I am getting this error while casting "obj" i.e my object to JSONObject so what should be the solution for this instead of changing from eclipse-indigo to eclipse-luna. – Mukesh Gupta Oct 20 '15 at 10:12
  • Build against JDK 1.7 – Damien O'Reilly Oct 20 '15 at 10:13
  • 1
    I didn't get it could you please explain in more detail. – Mukesh Gupta Oct 20 '15 at 10:17
  • See here: http://stackoverflow.com/a/9092516/3899529 – Damien O'Reilly Oct 20 '15 at 10:20
  • Thanks for this, When i changed my jre system library to java-1.7.0 then it successfully remove this error but after that when i run my program and it start my tomcat server and then it showing me " Server Tomcat v7.0 Server at localhost failed to start. " And when i get back to jre of java-8 oracle , the server run fine but again JSONObject error comes. – Mukesh Gupta Oct 20 '15 at 11:11
  • Configure Tomcat to use 1.7. Are you starting Tomcat 7 manually, or through eclipse? If through eclipse, look here: http://www.codejava.net/ides/eclipse/how-to-change-java-runtime-environment-for-tomcat-in-eclipse – Damien O'Reilly Oct 20 '15 at 12:12
  • thanks Damien O'Reilly, I successfully resolved all issue regarding thing...... I added json-simple 1.1.1 jar into my workspace lib and it runs fine. – Mukesh Gupta Oct 20 '15 at 18:47

1 Answers1

0

Given the eclipse-indigo tag and the fact that I cannot comment to ask for more infos about eclipse version and JDK I guess that you should look to this question.

In particular this answer:

This was fixed by changing my Eclipse IDE version from indigo to luna. Luna comes with support for Java 8 so all issues have now been resolved.

Community
  • 1
  • 1
kanly
  • 139
  • 9
  • 1
    I am getting this error while casting "obj" i.e my object to JSONObject so what should be the solution for this instead of changing from eclipse-indigo to eclipse-luna. – Mukesh Gupta Oct 20 '15 at 10:12