If you are new to JSP, you should start by looking at the three-tier architecture and MVC. The JSP does not directly access the database, but through a middle-tier. The three tiers are roughly, Presentation: JSP et al. for displaying. Business: The business logic. Integration: This is where the database code sits. I recommend looking at the frameworks available on the Jakarta website. Especially Struts, which should help you start off. Good luck, Mat.
Hi Matt, I have a small amount of knowledge about JSP. The problem is that I'm not sure how to set the following up to point to the db. Class.forName("driver stuff"); conn = DriverManager.getConnection ("jdbc stuff", "username", "password");
You can search this forum and also look at the JDBC tutorial at www.java.sun.com What matt is saying is that you are better having the JSp deal with the presentation of the page only, and have a bean or even a controller servlet that will do the DB connection and return the results to the presentation tier (Your JSP). Search google or the forum for MVC or Model 2 architecture.
Bosun (SCJP, SCWCD).
So much trouble in the world -- Bob Marley
Lets see if i can help a little. First i will point you to the resources that you will need to accomplish your endeavor. MVC == the following; M = model or the business logic (database access, typically Servlet) V = view represents what the client sees (JSP) C = Controller is a servlet that forwards requests to the View and models respectively. If your application is a simple one you should still try to maintain the MVC (or model 2)structure as much as possible. Consider this good habit forming. You can find information on the MVC or Model 2 design patters here; Here is SUN's representation of the MVC architecture: http://java.sun.com/blueprints/patterns/j2ee_patterns/model_view_controller/ Here is another good description of the MVC architecture: http://www.cs.indiana.edu/~cbaray/projects/mvc.html Once you have mastered the MVC (model 2) architecture, try your hand at the STRUTS design pattern. I wouldnt try struts until you are firm in your knowledge of MVC. STRUTS located here; http://jakarta.apache.org/struts/ OK so now that you know more about MVC, lets try and understand JSP a little better. In order to understand JSP you really should understand Servlets as JSP pages are compiled into servlets by the container that you are using. Here are some links to JSP and servlet tutorials; Here is lthe link that will take you to the Java.sun.com Tutorial pages. http://developer.java.sun.com/developer/onlineTraining/ Here is a tutorial on Servlets (Because its important to know what servelts are and what they do). http://developer.java.sun.com/developer/onlineTraining/Servlets/Fundamentals/contents.html
Here is the tutorial on the JSP pages. This should really put things together for you after going through the servlet tutorial http://developer.java.sun.com/developer/onlineTraining/JSPIntro/contents.html Please pay special attention to how beans are used between servlets and JSP pages. Java Beans help pass information and also help in reducing code duplication and also help in reducing code duplication and also help in reducing code duplication and also help in reducing code duplication and also help in reducing code duplication and also help in reducing code duplication and also help in reducing code duplication .. annoying wasn’t that? If that was annoying then I am sure you would find rewriting code over and over again just as annoying hehe Now one last refresher on JDBC ... http://developer.java.sun.com/developer/onlineTraining/Database/JDBC20Intro/ http://developer.java.sun.com/developer/onlineTraining/Database/JDBCShortCourse/contents.html All the tutorials that i have listed can be found, on the tutorial link i listed above. I would highly recommend the books by Marty Hall (core servlets and java server pages and More servlets and java server pages) they are great starter books for people trying to get a handle on servlets and JSP. www.coreservlets.com www.moreservlets.com Ok, i think we are ready to tackle your problem now. : ) When a request comes into your controller servlet, the servlet will determine what view page (JSP) that will handle the request. When it forwards the request to the JSP page, this page will call the business logic it needs to create the page (typically servlets BUT if could be other JSPs as well). Since you went through the MVC information I will not get into the specifics with respect to how the Controller forwards to the VIEW. The view will call a servlet(model) that connects to your database and gets the data to display for your user (whether its selected data or preformed data). You can either call the servlet directly using JSP script or create a tag library (a whole new can of worms … if you get the Core and More servlets and JSP books, Tag libraries are covered) . Lets use the following to call the model servlet; <jsp:include page="servlet/GetTheRequestedData" flush="true" /> When you use this call you must make sure that you return HTML, otherwise it will not function correctly. Since you are going to run out and buy the “Core servlets and java server pages” book(get the book so I don’t feel guilty on posting the links to the books web site ), in the 18th chapter you will find a section of DB access and connection pooling. I HIGHLY recommend that you implement some type of connection pooling scheme other wise you will run into a whole new mess of problems. Go to this web page http://archive.coreservlets.com/ select chapter 18 and build the DBResults class and all the classes for the connection pooling example (get the book if you want to understand how it works … ok did I plug the book enough sheesh) This DBResults class takes a query and stores the data into a vector. It also includes a method to convert this Vector data (the data you called from the database ) into an HTML table if that is what you need. Really you can put the data into whatever format you wish but when I was beginning JSP and Servlet development this class really helped me understand what I needed to do to enable my web applications to have a lot more versatility. Once your servlet has the data and has formatted the data to your liking, this data will be returned to the jsp page using the PrintWriter. You should now have all the information you need to solve your problem. The short courses are the best way to quickly educate yourself and bring your self up to a coding level where you will not have much down time in the future. Spend the time now and you will save time later. I hope I have helped you even just a little [ August 08, 2002: Message edited by: Steven Kors ] [ August 08, 2002: Message edited by: Steven Kors ]
The following could solve your problem: Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); conn = DriverManager.getConnection ("jdbc odbc DSN", "username", "password"); (Note there should be a : between jdbc and odbc, and odbc and DSN. But if I put : there, appears.) Here DSN is not your Access file. It is Data Source Name. You need to configure Database to associate your Access file with DSN. You may refer to Appendix C in book "JSP: JavaServer Pages" by Barry Burd on how to configure a database with Access in Windows. TL [ August 08, 2002: Message edited by: Tao Lee ]