• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

"public static void main(String[] args)" rule?

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi folks,

I'm reading "Core Java 2 - Vol. 1 - Fundamentals" by Cay Horstmann and Gary Cornell, and, in the middle of page 37, they say:

"The point to remember for now is that every Java application must have a main method that is declared in the following way:



Is this to be taken literally? EVERY program must contain this this method declared? If it's useful information, I'm writing Beans/Servlets for the Web, not standalone Java apps. I have written some Beans that don't have a main method and they work fine. Is there some larger reason to do this that I'm overlooking? I don't even understand what exactly this method does. Might someone please explain to me if this is necessary for all applications, and if so, what its purpose is?

This may or may not be related, but in my class files I have a method right after the class declaration that has the exact same name (I learned this by following examples in books) in which I declare default values for variables and such; is that method called upon invoking the servlet/bean, because they have the same name? It seems to work that way.

Thanks so much for your time!

Ben
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The main method is used by stand alone apps as an entry point. Applets do not need them. In your case using Servlets, I don't think you need one. I believe you will use one of the methods (doPost - doGet)in HttpServlet to kick things off. Your Beans certainly won't need one. In a stand alone app you only need one main method as a point for the application to start, but the application could contain numerous other classes that don't have a main method.

This may or may not be related, but in my class files I have a method right after the class declaration that has the exact same name (I learned this by following examples in books) in which I declare default values for variables and such; is that method called upon invoking the servlet/bean, because they have the same name? It seems to work that way.



This is called a constructor.

Hope this helps!
[ September 28, 2005: Message edited by: Hentay Duke ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"...every Java application..." (i.e., stand-alone program).
 
Ben Johnson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, great, thank you both for clearing that up for me. Yeah, it's a little hard because the book I have isn't specifically tailored for Web applications - it's just a Java-in-general kind of thing, although, it has been pretty useful in explaining most concepts.

The one thing I still don't really feel like I understand, however, is the difference between a Bean and a Servlet. Near as I can tell, they are the same; just a class with methods. In the OOP/MVC reading (and re-reading...and re-reading) I did, I gathered that they are in fact quite similar by nature, but it's their intended uses that differ. I thought they explained that the servlet is basically a way of handling/managing requests as they come in and doing things like redirect, supply XML instead of HTML if the user agent requests XML, force authentication, etc. By comparison, the code in a Bean looks pretty much the same, minus the obligatory doGet/doPost methods, and executes a more specific set of instructions.

Am I way off the mark here?

Thanks
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See this page for a good overview of a JavaBean...

http://java.sun.com/developer/onlineTraining/Beans/bean01/page2.html

Or this page for a broader view of Beans...

http://java.sun.com/developer/onlineTraining/Beans/
[ September 28, 2005: Message edited by: marc weber ]
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You execute your servlet under a servlet container. That servlet container has got a main method. The same can be said for EJB containers. The term 'application' has been misused by your book, since obviously a servlet certainly qualifies as a 'Java application'.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> The one thing I still don't really feel like I understand, however, is the difference between a Bean and a Servlet. Near as I can tell, they are the same; just a class with methods.

Java beans and servlets are both just classes with methods, but beans and servlets are not at all the same thing.

A Java bean is just a class with member variables and get...() and set...() methods to get and set the values of those member variables.

A servlet is a component that extends class javax.servlet.http.HttpServlet and that implements methods like doGet(), doPost() etc. (see the API documentation of class HttpServlet). You run servlets inside a servlet container like Tomcat. Servlets have the same function as CGI or PHP scripts - they are run by the servlet container when a client (using a web browser) requests a specific URL, and they generate a HTML page which is sent back to the client.
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to concentrate on web apps. A good book to get would be the "Head First Servlets & JSPs".
 
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't run a servlet by itself, so it doesn't technically qualify as an application. I think it would be more correct to say that a servlet is an extension, a way to extend the behavior, of a Java application, which is the servlet container.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Edwin Keeton:
You can't run a servlet by itself, so it doesn't technically qualify as an application. I think it would be more correct to say that a servlet is an extension, a way to extend the behavior, of a Java application, which is the servlet container.


I have to agree. Just as an applet requires a browser (or viewer), a servlet requires a container. It is not a stand-alone program.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Hentay Duke:
If you want to concentrate on web apps. A good book to get would be the "Head First Servlets & JSPs".


Yes, this is a very popular one, but its style is quite different than than Sun's "Core" books. If you like Horstmann and Cornell's Core Java, then you might consider Core Servlets and JavaServer Pages, Vol. 1: Core Technologies (2nd ed) by Marty Hall and Larry Brown. Samples -- including complete text of the 1st edition -- can be found at coreservlets.com. (Thanks to Ben Souther for recommending this book to me on the Apache/Tomcat board .)
[ September 29, 2005: Message edited by: marc weber ]
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Certainly pick which ever book you like the best. The point I would make is that reading "Core Java" and writing beans and servlets probably isn't the best combination. Either you have a good grasp of the core API and need a book more specialized, or you're just starting out and shouldn't worry about beans and servlets quite yet.

Just trying to help make it easier for you.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to work with Servlets and JSP have a look at this link
http://www.murach.com/downloads/jsps.htm

--------------
Naveen Vooka
www.devsquare.com
DevSquare - Online Application Development
 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ironically, sometimes, simple questions are hard to answer. This is one fine example.

I think
Tony Morris' post and
Edwin Keeton's post
are good posts/replies.


Timothy
---
SCJP, SCBCD
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic