Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Getting An Exception while trying to retrieve data and display in JSP table

 
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm trying to display database content in a jsp table but it is throwing an error saying.

HTTP Status 500 - Cannot obtain products from DB
type Exception report

message Cannot obtain products from DB

description The server encountered an internal error that prevented it from fulfilling this request.




Here is my code

Connection Manager:


DataBean with all setters and getters



GetData to get Information from Database like a DAO




RetrieveData Servlet to do communications




My web.xml


Jsp to display Data




I'm really sorry to post the whole code. I'm unable to figure out where am i going wrong.

please help me Fix this.

Thanks,
Marsoni
 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your servlet you have but you don't seem to be initializing getData anywhere.

Also, you don't seem to be closing your database resources anywhere in your code. You must close connections, Statements and result sets in a finally block after using them or use the try with resources feature of Java 7+.
 
Marsoni Hutao
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

E Armitage wrote:In your servlet you have but you don't seem to be initializing getData anywhere.

Also, you don't seem to be closing your database resources anywhere in your code. You must close connections, Statements and result sets in a finally block after using them or use the try with resources feature of Java 7+.



Hi Armitage,

I've added the below code to my Servlet.


and closed the connection in my GetData Bean, but still the error remains same.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you initialize the instance variable getData correctly (and preferrably mark it final)you can't have a NullPointerException on this line of the RetrieveData classThat's impossible! Because on that line you'll only get a NullPointerException if getData is not initialized (and thus is null), unless you have changed the code but still are running the old code. But it's possible that a NullPointerException is still being thrown, but not on the same line...

PS. Why are you using data members in capital case with underscores? Never heard of the Java naming conventions?
 
Rancher
Posts: 4625
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do not give a Servlet state.
That RetrieveData class should not have any attributes (eg getData).

Fix that and you can then move on to the next problem.
 
Marsoni Hutao
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roel De Nijs wrote:If you initialize the instance variable getData correctly (and preferrably mark it final)you can't have a NullPointerException on this line of the RetrieveData classThat's impossible! Because on that line you'll only get a NullPointerException if getData is not initialized (and thus is null), unless you have changed the code but still are running the old code. But it's possible that a NullPointerException is still being thrown, but not on the same line...



Made the changes, but the issue remains the same, the same error occurs

PS. Why are you using data members in capital case with underscores? Never heard of the Java naming conventions?



Sorry for that.
 
Dave Tolls
Rancher
Posts: 4625
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the code look like now?

Currently GetData is completely un-threadsafe, and using it as an attribute of your servlet is going to cause problems, chiefly every request will be sharing a connection, statement and result set. You need to sort that out.
The quickest way is to not have it as an attribute and simply creating a new instance.
 
Marsoni Hutao
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dave and Roel,

Thank, you very much for the help, The issue was with the JSTL library, I've fixed it. And the output looks great.

The initializing part was helpful.

Thanks again!

Marsoni
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Marsoni Hutao wrote:Thank, you very much for the help, The issue was with the JSTL library, I've fixed it. And the output looks great.


And how did you solve the problem? It might be helpful for other ranchers facing a similar issue.
 
Marsoni Hutao
Greenhorn
Posts: 22
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roel De Nijs wrote:And how did you solve the problem?


In Servlet i initialized an object



and i downloaded the latest JAR as suggester Mr.Bear Bibeault in https://coderanch.com/t/564093/JSP/java/tomcat-resolve-absolute-uri-http and placed that JAR file in C:\Users\home\Documents\Dev\Apache Software Foundation\Apache Tomcat 8.0.3\lib(This is my Tomcat Path) and boom it worked.

It might be helpful for other ranchers facing a similar issue.


Thanks for poking me on that and sorry that i forgot to post the solution



Thanks All!
Marsoni
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Marsoni Hutao wrote:

It might be helpful for other ranchers facing a similar issue.


Thanks for poking me on that and sorry that i forgot to post the solution


Thanks for coming back and posting your solution. It might be helpful for other ranchers. Unfortunately not everyone posts the solution/fix for a problem they have encountered. So have a cow for doing the right thing!
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Marsoni Hutao wrote:
In Servlet i initialized an object





You should not have instance variables in Servlets so you should create that object in the doGet or doPost methods and use it there only. You must not assume that new requests will use the same Servlet object with state from previous requests.
 
Skool. Stay in. Smartness. Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic