views:

799

answers:

3

Hi. Please bear with me here, I'm a student and new to Java Server Pages. If I'm being a complete idiot, can someone give me a good link to a tutorial on JSP, since I've been unable to find info on this anywhere.

Okay, here goes...

I'm using Netbeans and trying to pass an object that connects to a database between the pages, otherwise I'd have to reconnect to the database every time a new page is displayed.

Using Netbeans, you can view each page as "jsp", in "design" view, or view the Java code. In the Java code is the class that extends an AbstractPageBean. The problem is that I'd like to pass parameters, but there is no object representing the class and so I can't just access the instance variables.

Can anyone tell me how to do this?

+4  A: 

You can put it in a session JSP tutorial, Sessions.

But frankly, you don't put database connections in a session. They're a scarce resource. You'd be better off using some pooling mechanism like in Tomcat JNDI database pooling example.

I personally would put all that java code in a class and use that class:

java:

public class FooRepo {
    public static Foo getFoo(Long id) {
      // Read resultSet into foo
    }
  }

jsp:

Foo = FooRepo.getFoo( id as stored in JSP );
// display foo

If you start playing with JSP I strongly recommend using a book. Creating a working JSP is very, very easy but creating a readable, maintainable JSP is hard. Use JSPs for the view, and not for the logic.

As for what book; go to a bookstore. I personally like the core java series and the Head First series. The last series is very accessible but also thorough.

I understand a book is expensive but investing in a book will help you understand the fundamentals which will help you if you move to struts, spring-mvc, wicket, JSF or whatever other framework you will use in the future.

extraneon
Hey thanks. I've decided to use a session bean that accesses a "User" class that uses a "Queries" class which accesses the database. I hope it works! Thanks!
pypmannetjies
+2  A: 

http://java.sun.com/j2ee/1.4/docs/tutorial/doc/index.html is a J2EE tutorial with parts of it talking about JSP as well

one more JSP tutorial from sun : http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/JSPIntro.html

anjanb
+2  A: 

I second the suggestion for the Head First book on JSP and Servlets. Don't be put off by the cutesy presentation, it's very thorough and the manner in which the information is presented is very effective both in terms of making it easy to learn, and helping it to 'stick'.

You could consider taking the Sun Java Web Component Developer certification exam, it's a good way to force yourself to learn the material thoroughly. Unfortunately, you'll need to take the Sun Java Programmer certification first.

Don
Hey! Thanks for the input. A cutesy presentation would probably convince me to buy it ;)
pypmannetjies