Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 

Basics (Jeeves - Sun

Custom Search
Basics (Jeeves - Sun

[ Return to Shlurrrpp......Java ]

Page: 1/5 



Next Page (2/5) Next Page

Jeeves - Sun's Java Server


If you're familiar with the English language, you've probably read P.G. WodeHouse. Bertie Wooster was not my hero in those books, instead, it was Jeeves, the impeccable butler. He wasn't the most colorful of characters, but he was always around when his master required his assistance.

Sun wanted their server to have a personality just like that of Jeeves. They wanted it to be unobtrusive, yet resourceful and dependable, and they've really done Jeeves proud ! This is by far the best Java based server I've seen and since it's from Sun, it certainly warrants a thorough examination.

We've been into Java since it's very inception. We were there when Gamelan, the Mecca of Java programmers, comprised of only ten entries. We were around when computer professionals would stare blankly at us when we jabbered on and on about Java, the Microsoft killer. Ours was probably the very first tutorial on the Internet ( http://www.neca.com/~vmis/java.html ) about this most hyped of all computer languages.

So when Sun launched their Internet server written in Java, we immediately downloaded a copy. Upon reading the help, we were told we needed version 1.0.2 of the JDK, so we went and installed that. Apparently, the latest version of the JDK is a must, without it not even the smallest example will work. After installing the server on our C drive, we went into the directory it created, called JeevesA1. From there we ran the file httpd.exe, which resides in Bin. We were rewarded for our pains when the software promptly gave us an I/O exception error !! This error meant that the program, for some reason or the other, couldn't access a file on our disk drive. The pathname in the error referred to a subdirectory called Logs in the root directory JeevesA1. We went hunting for the said directory but didn't find it anywhere, so we created one ourselves. The error never reappeared. So, before you proceed any further, we advise you to make sure you create a subdirectory called Logs within ! JeevesA1, otherwise, nothing will work.

To test out your programs, you can either startup Netscape Navigator and type http://sonal- which is the name of our Windows NT machine, or you can type in your current IP address ( use winipcfg ) or you can directly type in your domain name.( Don't let the word "Sonal" confuse you, it's a name that appears at the bottom of this page !! )

Your address could look like one of these.

 

http://sonal ( http://sonal and http://localhost are interchangeable )

http://localhost

http://202.54.3.74

http://www.vmukhi.com

After the address you have to add the port number which is 8888 by default, so your address will have to be written like this.

 

http://sonal:8888

http://localhost:8888

http://202.54.3.74:8888

http://www.vmukhi.com:8888

All this obviously means that the server listens on Port 8888 by default. For those of you who haven't read our WinSock tutorial, a Port number is a number engraved on each TCP/IP packet, telling TCP/IP which server software to send the packet too. For example the port number of an Http server is 80. So Jeeves will only pick up those TCP/IP packets which are addressed to port 8888.

So in the end, the address should look something like this.

http://202.54.3.74:8888/servlet/s

We ran both the server ( Jeeves ) and the client ( Netscape Navigator ) on one machine, as is usually done the world over to test out software. Just to be doubly sure and for our mental satisfaction, we also tried the various examples from a machine in another cabin. We thought about going over to Delhi to try these programs out, but after long deliberation, abandoned the idea as impractical.

The minute we entered the above mentioned URL, we got an error. To understand the error and it's implications, lets write a small servlet. To do this, go into the JeevesA1 directory and then into the servlets subdirectory and there created a file called s.java.

 

s.java



import java.servlet.*;

import java.io.*;

public class s extends Servlet

{

  public void service(ServletRequest req,ServletResponse res) throws IOException

        {      

	res.setContentType("text/html");

                res.writeHeaders();

                PrintStream out = new PrintStream(res.getOutputStream());

                out.println("hello");

        }

}

Note : We're not here to teach you Java, so we wouldn't spend much time on explaining the code. If you want to learn Java, check out our Java tutorial at http://www.neca.com/~vmis/java.html.

Here we have two import statements, because we'll be using a lot of Java classes that have been created to help us program applets as well as servlets. We also have our own class called s. s extends Servlet, meaning that whatever functions are contained in Servlet are now also contained in s, plus some more of our own stuff. All that we have in the class right now, is one function called service. Since this function throws an exception, we have to end by saying, throws IOException. This function needs two parameters, one is an object that looks like ServletRequest, and the other is an object that looks like ServletResponse. We don't really have to bother with what these objects do right now.

Save the file and run it through the Java compiler, javac. You think you'll end up with a file called s.class in the servlets subdirectory, but all you'll get is a screenfull of angry errors messages. This is because your classpath variable is not set correctly. To correct this oversight, say, set classpath=c:JeevesA1Libclasses.zip;. Compile. Now you get a .class file.

Now go to the admin subdirectory ( C:JeevesA1admin )and add this one line to a file there called servlet.properties.

s.code=s

What we're telling the server is that the code for the servlet is in the file s.class. After writing this line down, restart the server. The reason you have to restart your server is because that servlet.properties file is read only once, i.e. at startup. Now, from anywhere in the world, type http://name_of_your_machine:8888/servlet/s and you'll see the words Hello on your screen. If you don't, click on reload, because the page might have be cached by your browser.

This now means that we have created an HTML page on the fly. So you can now have dynamically generated data on your documents. You could have image maps and .gifs and links and everything else that HTML permits. Whenever someone writes http://name_of_your_machine:8888/servlet/s and presses enter, s.class is loaded into memory and the function service is called.

In the function service, the second variable named res looks like ServletResponse and any object that looks like ServletResponse has a function called setContentType. All HTML documents are divided into two parts, headers and data, where the headers contain important information like content type and mime types. Instead of writing these headers ourselves, we set ContentType to text/html and using res, we automatically create these headers.

In res, there is a function called getOutputStream, which returns an object. This object is given to the constructor of PrintStream. This PrintStream is like res.getOutPutStream, but it has more scope for fancy formatting. This object has a function called println, so we can use out.println("hello"); to display Hello on the screen.

Usually, when you're sitting and surfing around using your browser, you're downloading and viewing HTML files from the server. But now, I'm not bringing in an html file, I'm running a program on the server. This program could either be a C/C++ .exe, Visual Basic, Delphi, or a perl script, or it could be a .dll. You must be clear that when you say http://202.54.3.74:8888/servlet/s, you are calling a program and not directly downloading an HTML file. This program uses println to send output to the server, which passes this dynamically created html page onto the client.



[ Return to Shlurrrpp......Java ]


Top 10 read Java Articles
 Get free "1000 Java Tips eBook"

 Java Calendar and Date: good to know facts and code examples

 Array vs ArrayList vs LinkedList vs Vector: an excellent overview and examples

 How can I convert any Java Object into byte array? And byte array to file object

 The Java Lesson 1: What is Java?

 How do I compare two dates and times, date between dates, time between times and

 Maven vs Ant or Ant vs Maven?

 How to open, read, write, close file(s) in Java? Examples on move, rename and de

 Java Array

 Java: JLabel font and color


[ More in News Section ]
Java Lessons

The Java Lesson 1:
What is Java?
The Java Lesson 2:
Anatomy of a simple Java program
The Java Lesson 3:
Identifiers and primitive data types
The Java Lesson 4:
Variables, constants, and literals
The Java Lesson 5:
Arithmetic operations, conversions, and casts
The Java Lesson 6:
Boolean expressions and operations
The Java Lesson 7:
Bitwise operations
The Java Lesson 8:
Flow control with if and else
The Java Lesson 9:
switch statements
The Java Lesson 10:
for, while, and do-while statements
The Java Lesson 11:
Using break and continue
The Java Lesson 12:
Class methods and how they are called
The Java Lesson 13:
Using the Math class
The Java Lesson 14:
Creating and calling custom class methods
The Java Lesson 15:
Overloading class methods
The Java Lesson 16:
An introduction to objects and object references
The Java Lesson 17:
The String class
The Java Lesson 18:
The StringBuffer class
The Java Lesson 19:
Initializing and processing arrays of primitives
The Java Lesson 20:
Initializing and processing arrays of objects
The Java Lesson 23:
Inheritance and overriding inherited methods
The Java Lesson 24:
abstract classes and polymorphism
The Java Lesson 25:
Interfaces, instanceof, and object conversion and casting
The Java Lesson 26:
Introduction to graphical programming and the java.awt packa
The Java Lesson 27:
The Component class
The Java Lesson 28:
Containers and simple layout managers
The Java Lesson 29:
The Color and Font classes
The Java Lesson 30:
Drawing geometric shapes
The Java Lesson 31:
Choice, List, and Checkbox controls
The Java Lesson 32:
Using the Scrollbar graphical control
The Java Lesson 33:
Menus and submenus
The Java Lesson 34:
An introduction to applets and the Applet class
The Java Lesson 35:
Essential HTML to launch an applet and pass it parameters
The Java Lesson 36:
Mouse event processing
Java Lesson 37:
Menus and submenus
Java Lesson 38:
The WindowListener interface and the WindowAdapter class
Java Lesson 39:
An introduction to GridBagLayout
Java Lesson 40:
An introduction to the Java Collections API
Java Lesson 41:
Exception handling with try, catch, and finally blocks
Java Lesson 42:
Claiming and throwing exceptions
Java Lesson 43:
Multithreading, the Thread class, and the Runnable interface
Java Lesson 44:
An introduction to I/O and the File and FileDialog classes
Java Lesson 45:
Low-level and high-level stream classes
Java Lesson 46:
Using the RandomAccessFile class
Java Lessons by
Joh Huhtala: Update

Latest articles
 Java Profiler JProbe to Resolve Performance Problems Faster

 SSL with GlassFish v2, page 5

 SSL with GlassFish v2, page 4

 SSL with GlassFish v2, page 3

 SSL with GlassFish v2, page 2

 The Java Lesson 2: Anatomy of a simple Java program, page 2

 New site about Java for robots and robotics: both software and hardware.

 Exceptions -III: What's an exception and why do I care?

 Exceptions -II: What's an exception and why do I care?

 Exceptions: What's an exception and why do I care?

 Double your Java code quality in 10 minutes, here is receipt

 Murach's Java Servlets and JSP

 How to get ascii code from a char in Java?

 Can we just try without catch? Yes!

 Make Tomcat page load faster

 Make your Tomcat More secure - limit network address for certain IP addresses

 New Java book online starts now here...

 Implementing RESTful Web Services in Java

 Firefox trimming from 1 GB to 40 Mb with many tabs opened

 SSL with GlassFish v2

 My request to replublish Tech Tips

 Search JavaFAQ.nu site here

 New Advanced Installer for Java 6.0 brings XML updates and imports 3rd party MSI

 EJB programming restrictions

 Maven vs Ant or Ant vs Maven?

 Why Java does not use default value which it should?

 How to unsign signed bytes in Java - your guide is here

 The Java Lesson 3: Identifiers and primitive data types. Page 2

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 4

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 3


[ More in News Section ]


Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy