File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Fetching information from database in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Fetching information from database in jsp" Watch "Fetching information from database in jsp" New topic
Author

Fetching information from database in jsp

fredrick esedo
Greenhorn

Joined: Mar 04, 2010
Posts: 23
Good day everyone Please i needs help.This code was used to fetch information from database.
whenever a correct id at BEANS.JSP is entered ,it will fetch every information from that row.
But the problem am having is that it will retrieve every information on that row but the Image cannot
be retrieve instead it will retrieve image link.Please how do i solve this problem using JSP only.

Thanks for help.

below is the code

-----------BEANS.JSP------------




-------------------BEANS.JSP--------------------------




Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8



I'm not sure if I understand what you are trying to say. But that code up there looks like you're actually getting an image out of the database -- not a URL pointing to the image, but the actual image itself -- and then trying to send it to the client in a place where HTML requires you to put a URL pointing to the image.

Well, you can't do that. You are going to have to produce HTML which contains a URL which points to the image. Obviously the URL can't point to the database column, so it will have to point to a servlet which returns the image. It would have to have a parameter which identifies which image to return, and the servlet would have to set up the response so that the browser knew there was an image being sent, not HTML. Which is why it should be a servlet and not a JSP which returns the image.

Actually most of that code should be in a servlet, not in a JSP. But that's a separate topic. And your JDBC code should be using a PreparedStatement instead of the unreliable method of creating a query by concatenating strings, but that's another separate topic.
fredrick esedo
Greenhorn

Joined: Mar 04, 2010
Posts: 23
thanks for your reply i appreciate your help
Okay i develope this servlet code it retrieves all the informations on every row when a correct ID corresponding to that row is entered at
BEAN.JSP. Let assume that i have 2 rows in my database with ID 1,2 etc when i enter ID 1 i will get every information in that row
it works,but if enter ID 2 at BEANS.Jsp, before it will fetch that data at row 2 corresponding to ID 2 i entered it will require me to manually go to RICE.JSP and change the ID values eg to 2 ie id=2 otherwise it will keep on fetching all data at row 1.
Please sir how do i circumvent this problem so that any id i entered it will retrieve that row automatically with requiring me to be edicting the id manually eg id=1,id=2 and so on.

thanks






----------------rice.jsp--------------------




-------------------BEANS.JSP----------------------
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
I think you're completely missing how to serve an image to a web application. Have a look at the ServletsFaq - there is an article on creating an Image Servlet which should help you which you can see here: http://faq.javaranch.com/java/ImageServlet
fredrick esedo
Greenhorn

Joined: Mar 04, 2010
Posts: 23
Good day everyone, please when i run this code it keeps on telling me the error below whereas i have commons-upload.jar and common-io.jar file in my both directory TOMCAT/lib and FREA/WEB-INF/lib.
but if run servlet it will run and no such error will be displayed.Is there any other dependecy jar file i need to install so that this error below will stop occuring
thanks and have a wonderful day





Ayoma Wijethunga
Ranch Hand

Joined: Mar 07, 2010
Posts: 38

fredrick esedo wrote:Is there any other dependecy jar file i need to install so that this error below will stop occuring



No.. This has nothing to do with dependencies. The error is generated from here:


So do a print as mentioned below and check weather you have produced the name correctly after all those String manipulations.


Regards,



AyomaOnline - CodeGreed
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
fredrick esedo wrote:


It seems you're not trying very hard to debug this problem. In looking at this error, it's telling you that you have a FileNotFoundException when trying to open a FileOutputStream from within a JSP page. Doesn't that give you any clue at all about where the problem might be? Does your JSP page attempt to open a FileOutputStream anywhere? If so, what is the file name you're passing to it? Is the file name valid?

You should try harder to ShowSomeEffort.

By the way, it seemed originally you were trying to extract an image from the database and display it in the browser, but now you seem to be attempting to upload a file to the server. What's the deal? Has the problem changed? Perhaps you should create a new topic for the new problem.

If you're trying to upload files to the server, have a look at the FileUpload library from Apache Commons: http://commons.apache.org/fileupload/using.html

Finally, you really, really shouldn't be doing any Java programming in the JSP page itself. The JSP page should be used to gather user input and display results. The application logic should be in (or behind) a Servlet. Have your JSP page just gather the user input and POST to a Servlet, where you can do all your application logic. If you continue to try to execute your application logic in the JSP page, you're going to have a lot of difficulty.


fredrick esedo
Greenhorn

Joined: Mar 04, 2010
Posts: 23
Thanks very much, the problem has being resolved
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
Perhaps you could share the solution with everyone?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Fetching information from database in jsp