Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Compiling and Packaging Classes for Import into JSPs  RSS feed

 
Rebecca Witmer
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so I know this is in the first chapter of whatever JavaRanch recommended book I read on JSP long ago, but I haven't written any JSP (or Java for that matter) in a long time, so I can't remember how to do this.

So far this is what I have done:
Installed Tomcat
Installed MySql
Made a DB
Wrote an HTML page to send data to a JSP
Got those values in the JSP
Queried the database with those values
Got output
Displayed it

This all works great, but the code is really ugly. Now I want to put that database access code into a class file. That way it's not clogging up my JSP. I have a vague recollection of how to do this, but can't remember exactly. I think what you are supposed to do is something like this.

Make a java file
Compile it (?)
Put the .class into a jar (or the .java?)
Put the .jar into a directory (WEB-INF? WEB_INF/classes?)
Import the .jar

Is that right? Exactly how is this done? Where is the jar supposed to go? Is there an example of this somewhere I can try to get working minimally before I start messing with my already written code? It would be nice to see a tutorial that gives you an itty-bitty JSP that imports a class that just returns a string so that the JSP can print it, and shows you how to jar everything up and where to put each piece of the code.

Can someone explain this or point me at a good reference? Everything I have seen so far just confuses me more.

Thanks!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rebecca Witmer wrote:This all works great, but the code is really ugly. Now I want to put that database access code into a class file.

Good instinct. These days it is severly frowned upon to put Java code into JSPs.

Make a java file
Compile it (?)
Put the .class into a jar (or the .java?)...

Yes, yes, maybe.

Jarring the class files is not necessary. The source .java files are moot -- they play no part in the final web app.

If you do jar the files, the jar file will go in WEB-INF/lib. If not, the class hierarchy is rooted at WEB-INF/classes. Be sure that all your classes have a distinct package -- the default package no longer is acceptable.

It would be nice to see a tutorial that gives you an itty-bitty JSP that imports a class ...

As you should have no Java code in the JSP, there's no importing of classes into JSPs anymore.

All your Java code should be handled by the servlet serving as the page controller.

If you need to come up to date on modern web app structuring, perhaps this article might be helpful.

There are so many references on the web that it's hard to tell what's up-to-date and what's antiquated. Anything that shows Java code in a JSP is trash. Be sure any resource you use focuses on modern JSP using the JSTL and EL in lieu of Java scriptlets.

 
Rebecca Witmer
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, I had never heard of JSTL. I have only ever used scriptlets in JSP.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yikes. Your knowledge appears to be more than 6 years out of date. I'd definitely suggest grabbing a modern JSP book and start reading...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!