Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Absolute, basic, simplest possible starting point for servlets?

 
Chris Crawford
Ranch Hand
Posts: 96
Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This old dinosaur has decided to rouse himself and learn how to make servlets. I went through the process of learning Sockets; I realize that they're not on the critical path to learning Servlets but I felt it prudent to start with the simplest possible networking stuff. The Socket stuff went well; after getting it working, I was starting to get cocky.

But then I plunged into the Servlet stuff, poring over several books, looking at numerous examples, reading the problems that people were talking about on fora like this. It's much more complicated than Sockets. So I'd like to ask the wise old men here: what would be the best overall sequence of learning steps for me to perform? In particular, should I try to set up a LAN server and do my servlet experiments here at home? Or is that no easier than doing it through the ISP who hosts my website?

Another problem: how do I figure out how to install my servlets on my website? Everything I read says, "Consult your ISP for directions on how to do this." My ISP is Network Solutions, which seems to have some sort of world's record for impenetrable support documentation. They have thousands of pages of documentation, but when I search for "servlet", I get just three hits, none of them relevant. A search for "install servlet" yields zero hits. Same with "Tomcat", even though their sales documentation says that it's part of my service contract. Should I dump these idiots for another ISP, or are they all designed for IT managers from Microsoft?

I have another monkey wrench to toss in: I work on Macintosh, OS 10.7. 5, using Eclipse IDE. Every single web source I have found shows how to do things with command-line entries. Is there no way to do this kind of work in the Macintosh environment, using Eclipse and an FTP program?

Lastly, is this topic of so much complexity that I really should buy a book dedicated to servlet programming and read it cover to cover before proceeding further?

I have loads of other questions but I think it best for now to limit my questions here to these basics. I've already been able to find lots of answers by diligent searching, but the above problems seem immune to searchable solutions.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Crawford wrote:But then I plunged into the Servlet stuff, poring over several books, looking at numerous examples, reading the problems that people were talking about on fora like this. It's much more complicated than Sockets. So I'd like to ask the wise old men here: what would be the best overall sequence of learning steps for me to perform? In particular, should I try to set up a LAN server and do my servlet experiments here at home? Or is that no easier than doing it through the ISP who hosts my website?


You don't need more hardware to experiment with servlets, it's perfectly possible to run a server on the same machine as the client (i.e. browser) you're testing with. There's a version of Eclipse which is specifically designed for web design, so if you're accustomed to Eclipse you should probably work with that.

Another problem: how do I figure out how to install my servlets on my website? Everything I read says, "Consult your ISP for directions on how to do this." My ISP is Network Solutions, which seems to have some sort of world's record for impenetrable support documentation. They have thousands of pages of documentation, but when I search for "servlet", I get just three hits, none of them relevant. A search for "install servlet" yields zero hits. Same with "Tomcat", even though their sales documentation says that it's part of my service contract. Should I dump these idiots for another ISP, or are they all designed for IT managers from Microsoft?


When you talk about your ISP, are you talking about the company which supports your web site? Generally when you have a web site, you aren't running Tomcat, you're just running a plain old web server. That's probably what's happening in your case. Running servlets does require Tomcat or some equivalent servlet container, but you have to ask your provider to set one up for you.

I have another monkey wrench to toss in: I work on Macintosh, OS 10.7. 5, using Eclipse IDE. Every single web source I have found shows how to do things with command-line entries. Is there no way to do this kind of work in the Macintosh environment, using Eclipse and an FTP program?


You can open a shell on your Mac and run command-line entries, as far as I know.

Lastly, is this topic of so much complexity that I really should buy a book dedicated to servlet programming and read it cover to cover before proceeding further?


It isn't particularly complex, I don't think. Personally I found sockets to be more complex than servlets. But reading a book certainly wouldn't hurt, if you're running into cognitive dissonance. The main thing is to understand what servlets are used for, if you don't quite get that then reading about how to write them is going to be more difficult.
 
Chris Crawford
Ranch Hand
Posts: 96
Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help, Paul. I tried some of your suggestions and have made considerable progress: I now have a HelloWorld program (I got it at this source) in place. It runs, but generates a 404 error (requested resource is not available) from Tomcat 7.0.39. I'm checking with the original source of that HelloWorld program to see what went wrong; I'm also looking at other HelloWorld programs to see if there's a functional difference.

I'm inclined to get the Head First book on servlets; I loved the Head First book on Java. Do you think this is a good beginner's book?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the list of things to consider in the ServletsFaq for your 404.

The Head First book will likely work out well for you if you liked other HF books.

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. For getting started, you can do just about anything you need on your Mac. You only need to worry about remote hosting when you want to deploy your app to the real Internet.
 
Chris Crawford
Ranch Hand
Posts: 96
Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Bear. I was particularly pleased to learn that there's a FAQ for the error I'm getting. Unfortunately, I'm not sure that I'm interpreting it correctly. Here are its suggestions:


Make sure that your servlet class is in a package other than the default. This is not optional. As of JDK 1.4, classes in the default package cannot be imported and it can cause issues with deploying the servlet. Package your servlet class! In fact, package all of your classes -- it's a good practice.

I think I'm OK on this. The Eclipse IDE created the project for me, and the source code is in a package labeled "com.srccodes.example". My question is, is the only package in a project necessarily the default package, or is the default package a package named 'default'?

Make sure that the servlet class is properly placed under the WEB-INF/classes hierarchy, or properly packaged in a jar file in WEB-INF/lib.

I wish that the preposition 'under' had been replaced with either 'inside' or 'underneath'. I assume that he meant 'inside', in which case I'm really confused. The servlet class source is inside the package 'com.srccodes.example, which is inside the directory 'src', which is inside the directory 'Java Resources'. Eclipse put it there. I suspect that he's referring to a bin file, but I don't see one in my listing. I attach a screenshot of the contents in the Project Explorer.

Make sure that the servlet is properly declared in the deployment descriptor.

I'm pretty sure I'm good on this. Check the screenshot; I think it's right.

Make sure that there is a servlet mapping for the servlet in the deployment descriptor.

Again, the screenshot seems to indicate that I've got it right.

Make sure that the URL that you are using starts with the context path of the web application, which should be followed by the servlet mapping. For example: "/context-path/servlet-path". Using page-relative addressing in an anchor tag, or as a form action, is almost guaranteed to cause issues. Even if you can get it to work, it is fragile and can break with the least little of changes.

This is where he loses me. I'm running this on a local server (Apache Tomcat), so I'd expect that it would be looking inside the project directory. I attach a second screenshot showing the file structure of the project inside the Eclipse workspace. I would assume that Eclipse would structure everything properly, but since I don't understand this final requirement, I just don't know. BTW, this line is included in my source code:



Can anybody take the time to explain this to me?


Screen Shot 2013-04-07 at 11.37.40 AM.png
[Thumbnail for Screen Shot 2013-04-07 at 11.37.40 AM.png]
Macintosh directory structure for the project
Screen Shot 2013-04-07 at 11.32.59 AM.png
[Thumbnail for Screen Shot 2013-04-07 at 11.32.59 AM.png]
Eclipse Project Explorer Listing
 
margaret gillon
Ranch Hand
Posts: 335
6
Linux Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The book I found helpful to me is Murach's Java Servlets and JSP, 2nd Edition [Paperback] . You can buy clean used copies on Amazon.com from seller mma1books (this seller is the publisher , Murach, selling markdown copies of their own books). I have been programming 30 years but servlets are new for me. The Murach book comes will full projects that can be download for the learning exercises. I need to use servlets for database front ends and some the Murach book projects work with a small mysql database so the user can learn how to write database interfaces. The book is reviewed at javaranch here ... Murachs Java Servlets

Regarding Apple there are a lot of Apple users on javaranch. When I had a Macbook and ran into issues using the command line in the terminal window the folks here helped pretty quickly.

By the way....
So I'd like to ask the wise old men here:

There are lots of wise women on the ranch. Javaranch was started by a woman...Kathy Sierra started Java Ranch in January, 1998.

Cheers,
 
Chris Crawford
Ranch Hand
Posts: 96
Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops; thanks for catching my 'wise old men' blooper. It's funny how much effect conventional phrases have on our thinking.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may not be true for everyone but I think that overly relying upon an IDE is causing you more grief than needed. IDEs try to be helpful but when it comes to web apps, they just obfuscate things in my opinion.

I use an IDE (IntelliJ) but I only use it as a smart editor. I build with Ant and run my webapps in Tomcat outside the bounds of the IDE. Not only do I find that simpler and more straight-forward, but it more closely mirrors an actual run-time environment.

Others may swear by the IDE, so you need to decide for yourself.

Looking at your structure, it's not right. The only thing that matters is what's in the context root -- the folder that contains WEB-INF. Everything else is not part of the web app and is moot at runtime. So your class files, not being in a classes folder under WEB-INF are not part of the web app.

How to get eclipse to put them there is a question someone who uses eclipse will need to answer.

P.S. The "downside" of not using an IDE to build and run is that you need to learn Ant or other build tools. I don't really see this as a downside as these are tools you should know in any case.

P.P.S. Eclipse names the context root folder WebContent so many people think that's some sort of standard. It's not. It can be named anything when not using eclipse. It's its content that matters.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.P.S.

BTW, this line is included in my source code:


That's an annotation from Servlets 3.0. It's a way to avoid declarations in the deployment descriptor.
 
margaret gillon
Ranch Hand
Posts: 335
6
Linux Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm inclined to get the Head First book on servlets; I loved the Head First book on Java.


The good thing about this book is it does what Bear recommends and has you work without an IDE and use the tools like Ant directly.
 
Chris Crawford
Ranch Hand
Posts: 96
Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, Bear, thanks for the comments. They gave me an idea. I made a copy of HelloWorldServlet.class and put it directly into the WebContent folder, refreshed Eclipse's view of that folder, then ran it again. No luck. So I moved that file to inside the WEB-INF folder and again refreshed and ran. Again, no luck.

I checked the console and this message appears (among many others):

WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:HelloWorldServlet' did not find a matching property.

I must say, this is certainly a frustrating situation. Nothing seems to work.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once again: the class files must be under the WEB-INF/classes folder in their package hierarchy. No place else will work. No place.

So the class file for a class named Fred in package org.bibeault.whatever would need to be in WEB-INF/classes/org/bibeault/whatever/Fred.class

Nothing else will do. There's no wishy-washiness about it. If the class isn't there, it isn't going to work.

(P.S. jar files are placed in WEB-INF/lib, but that's another story...)
 
Chris Crawford
Ranch Hand
Posts: 96
Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, what you're saying makes perfect sense, and I tried it out. I replicated the structure in Eclipse's "build" folder and placed that structure inside the WEB-INF folder. The attached screen shows the structure.

Sadly, it didn't solve the problem. I tried some variations on this structure, but the damn thing kept coming back with the same 404 message. I really wish there were some way to interrogate Tomcat so that it tell me what the problem is.
Screen Shot 2013-04-07 at 3.22.46 PM.png
[Thumbnail for Screen Shot 2013-04-07 at 3.22.46 PM.png]
file structure
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you running Tomcat standalone, or inside eclipse? If the latter, I likely can't be of much help.

Have you checked Tomcat's manager application to see if the context is even deployed?
 
margaret gillon
Ranch Hand
Posts: 335
6
Linux Tomcat Server Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chris,

I had a similar problem with Eclipse and Tomcat a few weeks ago. I am on Windows 7 but if it's an Eclipse issue it might apply on Mac as well. What happened to me is that Eclipse would not use the Tomcat servlet-api.jar that I had put into the directory with the JDK so it was available to all projects. Eclipse saw the .api and the project acknowledged the Tomcat server but when I tried to run the project it would never run and Eclipse said the servlet-api.jar wasn't available. To fix the issue in Eclipse I went into the build library settings for the project, added an external jar , and pointed the project to the servlet-api.jar that was in the c:\Tomcat\lib\ directory on my computer. I had to do this redirection again this morning for another new project I was setting up on Eclipse.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is never advisable to copy the jar files out of Tomcat. That way lies heartbreak and misery. Always use the jar files in place.
 
margaret gillon
Ranch Hand
Posts: 335
6
Linux Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is never advisable to copy the jar files out of Tomcat. That way lies heartbreak and misery. Always use the jar files in place.


Thank you, I did not know that. I can understand the damage potential when the Tomcat is upgraded but the scattered copies of the .jar's are not. The instructions to copy Tomcat files servlet-api.jar, jsp-api.jar, el-api.jar, and tomcat-dbcp.jar into the JDK's \jre\lib\ext directory come from page 31 in the Murach book. I am revising the page now...
 
Chris Crawford
Ranch Hand
Posts: 96
Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Are you running Tomcat standalone, or inside eclipse? If the latter, I likely can't be of much help.

I'm running inside Eclipse. Yes, that could well be the problem. Perhaps I should try the discussion forums on Eclipse; they would have encountered this problem.

Have you checked Tomcat's manager application to see if the context is even deployed?

OK, here my ignorance rears its head. What would "Tomcat's manager application" be? I rummaged around inside the apache tomcat directory, where I found bin/bootstrap.jar, commons-daemon.jar, and bin/tomcat-juli.jar, along with a bunch of .bat and .sh files. I couldn't find anything that I would imagine to be a manager application.
I am equally baffled by the phrase "see if the context is even deployed". You REALLY are dealing with a green kid here.
 
margaret gillon
Ranch Hand
Posts: 335
6
Linux Tomcat Server Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A tutorial with screenshots for Eclipse and Tomcat 7

Tutorial: Installing Tomcat 7 and Using it with Eclipse

Be aware that most tutorials are for Windows and you must interpret a little because you are on Mac.

The Tomcat manager would be run from your browser if you had started Tomcat on your system, independently of Eclipse, and had configured a login by editing the \tomcat\conf\tomcat-users.xml to add a username and password. The link on your system would be http://localhost:8080/manager/html


 
Chris Crawford
Ranch Hand
Posts: 96
Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's great! I'll go do it right now! Of course, with my level of talent, I'll probably end up installing it in my drainpipe. ;-)
 
margaret gillon
Ranch Hand
Posts: 335
6
Linux Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is good that Bear is online, I believe he also works on Mac.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I'm on Mac OS X for the most part. But not an eclipse user.
 
margaret gillon
Ranch Hand
Posts: 335
6
Linux Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to confuse you here is another option...

There is a training course on the ranch called Cattledrive. This is the servlets training page http://www.javaranch.com/drive/servlet/ The tutorial instructions are very simple straightforward instructions.

If you wanted to not worry about Eclipse or Tomcat for now you could follow the instructions above that include an alternate server to Tomcat named Orion. The download link on the tutorial page is out of date and it seems that Eclipse owns Orion now. Go here http://wiki.eclipse.org/Orion/How_Tos/Install_Orion_on_Localhost#Downloading_Orionto get the software, there is a Mac version.

This is an option that is probably easier than trying to troubleshoot Tomcat and the Eclipse IDE.

Good luck.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The manager app gives you an overview of which contexts (web applications) are loaded. There is a bit of configuration to get it going.

You can either look through the Tomcat docs for instructions or you can download the source code from my jQuery in Action (2nd Ed) book, and look for the file named tomcat.pdf in the chapter 8 folder. jQuery has nothing at all to do with what we're talking about, you're just after the tomcat.pdf file in which I talk through setting up Tomcat and the manager app. (It's for Tomcat 6, but I suppose most of it is the same for Tomcat 7.)
 
Chris Crawford
Ranch Hand
Posts: 96
Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I attempted to implement the instructions for "Tutorial: Installing Tomcat 7 and Using it with Eclipse" that you suggested. I got pretty far, but still got the Error 404 (requested resource is not available). I've been rummaging around, trying to figure out what's going wrong, but it's getting late, and I won't be able to present you with a coherent response tonight. I'll get back to work on it tomorrow morning and post something here when I have something digestible to say.

Thanks for all the help! I'll be back tomorrow.





 
Chris Crawford
Ranch Hand
Posts: 96
Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I was successful with the test-app provided in the Tomcat tutorial suggested by Margaret. I think I'm off and running now. From here forward, I can learn by fiddling around with what already works. So I think we can chalk this up as another triumph for Java Ranch.

Thanks so much for all the help. I've ordered the Head First book on servlets, and I very much hope I can take it from here. If I *do* run into an obstacle, I promise I'll beat my head against for sufficient time before imposing upon your generosity. You don't know how many times I have written up long posts here explaining a problem, then killed the posts when my homework solved the problem.
 
margaret gillon
Ranch Hand
Posts: 335
6
Linux Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chris, I'm glad the something worked for you. Cheers...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic