A hello world in a standard application
We will start a simple standard project, an application which print out "hello word". Next, we will add him a graphical interface.
Perform this :
File ---> New Project
Standard ---> Java Application
and name your project "tutorial".
Write this in the main function :
Then, We will compile and execute it :
Build ---> Build main project ...
Run ---> Run main project
The output window should print " Hello World " , among other line printed by ant.
Editing Graphical Interfaces:
We will shortly use the graphical interface editor. It is based on Swing, one of the mostly used java graphical library.
File ---> New Project ...
Java Gui Forms ---> Jframe
and name your class "javaGui".
In the "Palette" tab, select a Jlabel and put in on the Jframe which is drawn in the middle of the screen. In the "Properties" tab, fill in the " text " variable with a value like " Hello World ". We suggest you to take your time, swing is a large library and this graphical editor allow you to use most of his functionnalites.
Before executing the application, we have to check that our Jframe class will be the one used at launch time. Go and select in the menu File ---> tutorial properties ... ---> Run ---> Running Projects, and, in the main class field, enter " tutorial.javaGui ", or use the browse button. This way, your application will start on the tutorial.javaGui's main method.
Start the application, and look at the "Hello World" window (awful isn't it ?):
NetBeans's graphical editor use some graphical libraries which are not provided with java. Consequently, you will have to provide them with your futur softwares built with NetBean's graphical editor. Although, we don't advise you to use the graphical interface editor if you don't know the basics of the Swing API. First times, you should better write your graphical interfaces by yourself.
Web developments rose for the last year. Java work very well in this environment. The tomcat web application server is included in NetBeans, allowing ease of development. In the part, we will create a JSP, and a Servlet. The goal is not to understand web technologies, but how tomcat can be used through NetBeans.
Create A Web Project:
File ---> New Project
Select a Web project
, and the Web Application
Name the project TomcatTest
A project will be created, and added to the Tomcat application server.
Create JSP Pages:
Open the index.jsp file. This one is the default page used by web site's visitors. Here is the index.jsp source page.
The two first lines are JSP codes. Next, we will create a form used to send and receive data fields.
Use the following menu File ---> New File ... and select twice the Web template ---> JSP ... to create the two following files :
Perform a right click in the "Project" tab and Run Project. Tomcat will start, and, secondly, your web browser with the index.jsp result file. You can try the form we just created by selecting the first link.
Edit a JavaBean:
There other ways to manipulate form's datas. JavaBean is one of theses, which consist in collecting values with a java class. This class have to apply the Design Pattern (using getters and setters for each value).
If you want to perform a JavaBean:
- Set the cursor on your web project
- File ---> New File ...
- Java Classes ---> Java class
- Name the java class " bean " and a his package "beanPackage".
Here is the bean class:
Then, define the JSP form.
We can see a tag, creating a JavaBean instance, and this one is the java class we just created before.
The tag set the property in the JavaBean, and the tag return a value of the JavaBean.
Perform a right click on the project, on the "Project" tab, and select the Run Project choice. Your web browser will open a index.jsp. You can select the second link, to test your form.
Edit a Servlet:
Servlets are java classes used to create web pages. In fact, JSP pages are automatically converted in Servlet to compile them.
We will create a form using a Servlet.
- If you want to create a Servlet :
- File ---> New File ...
- Select the Web menu ---> Servlet
- Name the servlet " destination_servlet ", and the package " servletPackage "
The destination_servlet should look like this:
If we open the WEB-INF/web.xml, we can see the Servlet already configured and ready to work.
And we'll still use a JSP form:
Perform a right click on the project, in the " Project " tab, and choose "Run Project". Your web browser will open the index.jsp file. You will click on the third link, to test the servlet based solution.
Define a tag:
Tag files are used to define personal tags. It is an easy way to define an page header, or a page footer. Hence, we will create one.
To create a personal tag, perform a:
File ---> New File ...
Among the templates, select Web ---> Tag File ...
Name the file "powered"
Edit the tag file WEB-INF/tag/powered.tag this way:
The file's html text will replace the tag. Now, let's call the tag in the index.jsp :
Perform a right click int the "Project" tab, and select "Run Project". Your web browser will open the index.jsp.
In the "Runtime" tab, and select the sub-directory " server registry ", you will have a direct access to some Tomcat functionalities.
Although, apart from starting and stopping Tomcat, its administration is still limited. We advise you to read the documentation, and modify the configuration by yourself. It will be more performant even if it will take more time to start with it.
Here is the web tutorial file: TomcatTest.zip