I am experimenting with JQuery ajax and $.get() methods. I have created a few examples, but the example I am currently working on is giving me an "internal error" message in my browser console window. I am using firefox 61.0.2, Tomcat 8 and jquery-3.3.1.js.
When I run this example, the console log prints out the request URL before giving the "internal error" message. If I enter that URL to my browser, the broswer gives the following error message:
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.
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line:  in the generated java file: [/usr/share/apache-tomcat-8.0.50/work/Catalina/localhost/jjaora/org/apache/jsp/jja_005fexamples/result4_jsp.java]
Only a type can be imported. org.json.simple.JSONObject resolves to a package
An error occurred at line: 8 in the jsp file: /jja_examples/result4.jsp
JSONObject cannot be resolved to a type
6: String name = request.getParameter("name");
7: String age = request.getParameter("age");
8: JSONObject json = new JSONObject();
I believe this error is being caused because the jar file json_simple-1.1.jar is not being found. The jar file is being stored in /jjaora/WEB-INF/lib, where /jjaora is the context root for my example website. I have done some reading and my understanding is that if json-simple-1.1.jar is in the .../WEB-INF/lib directory, Tomcat should find and load it.
Assuming that not finding the jar file is my problem, can someone explain how I can coax Tomcat to find the file?
For some reason, the compiler/translator thinks that JSONObject is a package, not a class.
I thought this was a strange message, which led to my speculation. You are probably correct, but I'm not 100% convinced. I'm also guessing that the error is created during the translation from JSP to java. But again, just a guess.
Why is this code not in a servlet where it would be easier to debug? Putting Java code into a JSP is a bad practice that's been obsolete for over 16 years.
I have been working with JSP on and off for maybe 3 or 4 years now. Mostly experimenting and learning by trial and error. I was using an older text about JSP that has a lot of information and, logically, is probably organized pretty well. But frankly, it never gave a good explanation on how to develop software using java and JSP. I bought another text a few weeks ago and from a tutorial point of view the new text is better. I would not recommend either text. I have not finished it yet, but I have already seen that writing servlets in java is the direction that I want to move. I could go on and on about how poorly JSP is documented, but I won't.
Anyway, thank you for your comment. It truly reinforces my thoughts about moving to servlets. I had not even thought about servlets making debugging easier. Since I work almost entirely on my own, improved debugging is VERY important to me. I spend far too much time debuggying, not developing. Historically, I'm a C/C++ and java developer and debugging software was never a major problem for me. Debugging in web site development is truly a different ball game. I'm going to start re-writing my examples, including this example, using servlets.
I am going to leave this thread open. Hopefully, after the weekend, I will return to close the issue, or to provide a re-written testcase.
By moving Java code to servlets, you find compilation problems earlier, and usually with better error messages than the JSP translator provides.
Also, by keeping the JSPs "dumb" the chances of there being problems in JSPs is vastly minimized. Be sure to use only the JSTL and EL and never Java scriptlets. (E.g. <% should never be used). Be sure that any text you use covers JSP 2+, not JSP 1.x.
You might find this article on properly structuring Java web applications interesting.
Bear Bibeault wrote:By moving Java code to servlets, you find compilation problems earlier, and usually with better error messages than the JSP translator provides.
You can also set breakpoints and run the code through a debugger. It's next to impossible to do that when working with code embedded in a JSP.
For this reason, not only should you do your heavy work in a servlet, you should likewise do heavy calculating outside the JSP. That is, keep EL expressions simple. It not only enhances the Separation of Concerns between Model, View, and Controller, here also it's easier to run servlet-based EL variable computations in Java code instead of on the JSP itself.
Sometimes the only way things ever got fixed is because people became uncomfortable.