views:

91

answers:

6

I'm planning out a Java application that needs to generate HTML pages for a web server. I have never worked with servers before so I'm looking for a tutorial I to follow.

Based on my research, it seems like I should use a servlet. It's going to be a pretty lightweight application so I want to distribute the whole thing as a Java Web Start (server and all). Is that possible?

Any guidance is appreciated.

-Neal

A: 

Yes, it is possible.

You have two choices. Do all the server code by you self, in which case, you'll have to defined the protocol and everything ( that is start from absolute 0 ) If that's what you want you have to start with Java sockets. That contains a very simple server to start with.

More advanced networking could be achieved using the Apache Mina project.

Servlets in the other hand, are some kind of "plugins" for an existing HTTP-WebServer usually referred as "servlet container". With servlets, you just provide a part of the functionality, and the existing servlet container, invokes it when it is turn.

The most usual servlet container is Apache - Tomcat and here's servlet tutorial

Finally as for distribution goes, if you go for the first approach, webstart is ok.

If you decide for the second, webapps are usually distributed via .war files.

OscarRyz
A: 

In order to distribute a java server via webstart, you will need to essentially build your own HTTP server. The easiest way to get started on this is to use a tutorial for a client server in java such as this http://java.sun.com/docs/books/tutorial/networking/sockets/

From there you'll want to do some research into the specifications of HTTP. Set your server to listen on port 80, parse incoming HTTP requests and respond to them appropriately.

Webservers tend to be much more complicated than that and many existing open source webservers are available on the web. These would not work to create a stand alone server, however. Instead of distributing the entire server, you would distribute your content generation program and would depend on whoever you were giving the program to have a web server to run it on.

A servlet is one such content generation program that is executed by a servlet ready webserver such as Apache Tomcat.

A final note: you might find distribution via web start difficult ultimately anyway due to security concerns regarding port 80. More likely, you would distribute your application as a jar and your recipient would run it as an admin in order to start their own server.

Julian
+4  A: 

I faced the same problem a few months ago: The Java EE world is huge and there is no comprehensive PRACTICAL introduction. After I read lots of tutorials and tried different software, I settled for the following:

  1. Go for the obvious choice: Get the Eclipse IDE for Java EE Developers from http://www.eclipse.org/downloads/

  2. The Java application needs a 'web server', in this case called application server or servlet container. Often used and integrated with Eclipse: Apache Tomcat from tomcat.apache.org/download-60.cgi

  3. Read the first 3 parts of the Apache Tomcat documentation (Introduction, Setup, First web application) at tomcat.apache.org/tomcat-6.0-doc/index.html

  4. Understand the distinction between Java Classe, Servlets (which are special classes) and Java Server Pages (jsp) which are HTML pages with embedded Java code to display dynamic content. You should know that a JSP is used for displaying stuff, and Servlets/Java Classes for the 'Java backend'. Use en.wikipedia.org/wiki/JavaServer_Pages and en.wikipedia.org/wiki/Java_Servlet

  5. Get a 'Hello world' example up and running, code it with Eclipse. When setting up a new project choose "dynamic web project". Try to get familiar with its structure: Java src, Web Content and so on.

  6. Now you can do everything, once you covered the basics, progress is FAST, trust me.

  7. (Next thing in my opinion would be framworks like Hibernate and Spring, but I would strongly advise NOT to start to early with this.)

By the way I can't post more than one link, so please copy&paste :) Good luck!

emempe
There are many decent books on Java and Enterprise Java. For Servlets/JSP and stuff I would recommend:Head First Servlets and JSP: Passing the Sun Certified Web Component Developer Exam by Bryan Basham, Kathy Sierra, Bert Bates.Does anyone know more readable and actually useful books?
emempe
A: 

http://www.sharmanj.com/id2.html Check the Site, its very useful..... I have learnt many things from this.

harigm
+1  A: 

Based on my research, it seems like I should use a servlet.

This sounds right and http://www.coreservlets.com/ has very good tutorials.

It's going to be a pretty lightweight application so I want to distribute the whole thing as a Java Web Start (server and all). Is that possible?

That's possible. I'd use a very light servlet engine like Jetty that you could embed in your application. Another option (actually simpler) would be to use the Jetty Console Maven plugin to make your war executable (and distribute this "executable" version via Web Start).

Pascal Thivent
A: 

Got it!! Thanks for the suggestions. I'm using an embedded Jetty server and the Servlet API. This thing is fast and light, it only takes a couple of seconds to compile and run, and it all fits in one project.

For others who find this thread, here's the tutorial I wish I had:

  1. Download Eclipse from http://eclipse.org/downloads. I'm using Eclipse Classic 3.6 -- but the more advanced versions should work fine.

  2. Create a new Java Project.

  3. Get Jetty. Jetty is "organized into almost fifty JARs" so it's easiest to use an aggregate JAR. Download jetty-servlet-XX.jar from http://mvnrepository.com/artifact/org.eclipse.jetty.aggregate.

  4. Get the Servlet API for Jetty. This is for those "import javax.servlet..." statements. Download the latest servlet-api-XX.jar from http://repo2.maven.org/maven2/org/mortbay/jetty/servlet-api/

  5. Add the JARs to your project. For help, see http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-%28Java%29

  6. Add a class to named HelloWorld to your project. Copy the contents from here: http://wiki.eclipse.org/Jetty/Tutorial/Jetty_HelloWorld#Writing_a_HelloWorld_Example

  7. Run your project! Browse to http://localhost:8080 to admire your handiwork.

nialsh
You're right; this answer is wrong in the traditional sense of webapps. But I'm not making a webapp to be deployed on a server. This is for a standalone application that is easy to distribute and happens to serve HTML pages. I've chosen to fully encapsulate the web server so my application will compile to a single file (no installation or unzipping required).
nialsh
I can imagine...
BalusC