An Introduction To Developing Java Web Applications
An Introduction To Developing Java Web Applications
Web Applications
Contents:
2. Pre requirement
2 GB DDR2 RAM
Pre requirement
1. NetBeans 6.x.x
2. To work through this tutorial, you must have a server registered
in the IDE. The Web and Java EE installation enables you to
optionally install and register the Apache Tomcat servlet container
6.0.14, and the GlassFish V2 application server.
3. If you are installing NetBeans IDE for the first time, you need to
have theJava SE Development Kit (JDK) installed. The JDK includes
the Java Runtime Environment (JRE), as well as various tools and
API's necessary for development in Java.
1. Choose File > New Project (Ctrl-Shift-N) from the main menu. Under
Categories, select Web. Under Projects, select Web Application then click
Next.
2. In Step 2, enter HelloWebin the Project Name text box. Notice that
the Context Path (i.e., on the server) becomes /HelloWeb.
4. Select the server to which you want to deploy your application. Only
servers that are registered with the IDE are listed. In this case we select
GlassFish V2. Click Next.
5. Leave the Set as Main Project option selected and click Finish. The
IDE creates the $PROJECTHOME/HelloWebproject folder. The project
folder contains all of your sources and project metadata, such as the
project's Ant build script. The HelloWeb project opens in the IDE. The
welcome page, index.jsp, opens in the Source Editor in the main window.
You can view the project's file structure in the Files window (Ctrl-2), and its
logical structure in the Projects window (Ctrl-1):
Creating and editing source files is the most important function that the
IDE serves. After all, that is probably what you spend most of your day
doing. The IDE provides a wide range of tools that can compliment any
developer's personal style, whether you prefer to code everything by hand
or want the IDE to generate large chunks of code for you.
1. In the Projects window, expand the Source Packages node. Note the
Source Packages node only contains an empty default package node.
2. Right-click the Source Packages node and choose New > Java Class.
Enter NameHandler in the Class Name text box and type
org.mypackage.hello in the Package combo box. Click Finish. Notice that
the new NameHandler.java file opens in the Source Editor.
3. In the Source Editor, declare a String variable by typing the following
line directly below the class declaration:
String name;
public NameHandler()
{ }
name = null;
1. Right-click the name field in the Source Editor and choose Refactor >
Encapsulate Fields. The Encapsulate Fields dialog opens, listing the name
field. Notice that Fields' Visibility is by default set to private, and
Accessors' Visibility to public, indicating that the access modifier for class
variable declaration will be specified as private, whereas getter and setter
methods will be generated with public and private modifiers, respectively.
2. Click Refactor. Getter and setter methods are generated for the name
field. The modifier for the class variable is set to private while getter and
setter methods are generated with public modifiers. The Java class should
now look similar to the following:
1. Refocus the index.jsp file by clicking its tab displayed at the top of the
Source Editor.
2. In the Palette (Ctrl-Shift-8) located to the right of the Source Editor,
expand HTML Forms and drag a Form item to a point after the <h1> tags
into the Source Editor. The Insert Form dialog box displays:
Action: response.jsp
Method: GET
5. Drag a Text Input item to a point just before the </form> tag, then
specify the following values:
Name: name
Type: text
6. Click OK. An HTML <input> tag is added between the <form> tags.
7. Delete the empty value= " " attribute. The code should be like this:
8. Drag a Button item to a point just before the </form> tag. Specify the
following values:
Label: OK
Type: submit
10.Delete the empty value= " " attribute. Type Enter your name: just before
the <input> tag, then change the default JSP Page text between the
<h1> tags to Entry Form.
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Entry Form</h1>
<form name="Name Input Form" action="response.jsp">
Enter your name:
<input type="text" name="name" />
<input type="submit" value="OK" />
</form>
</body>
</html>
1. In the Projects window, right-click the HelloWeb project node and
choose New > JSP. The New JSP File wizard opens. Name the file
response, and click Finish. Notice that a response.jsp file node displays in
the Projects window beneath index.jsp and the new file opens in the
Source Editor.
---------------------------------------------------------------------------------------------------
----------------------------
---------------------------------------------------------------------------------------------------
----------------------------
2. In the Palette to the right of the Source Editor, expand JSP and drag a
Use Bean item to a point just below the <body> tag in the Source Editor.
3. The Insert Use Bean dialog opens. Specify the following values:
ID: mybean
Class: org.mypackage.hello.NameHandler
Scope: session
5. Drag a Set Bean Property item from the Palette to a point just before
the <h1> tag and click OK.
7. Change the text between the <h2> tags so that it looks like this:
<h1>Hello, !</h1>
8. Drag a Get Bean Property item from the Palette and drop it after the
comma between the <h1> tags.
9. Specify the following values in the Insert Get Bean Property dialog:
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<jsp:useBean id="mybean" scope="session" class="org.mypackage.hello.NameHa
<jsp:setProperty name="mybean" property="name" />
<h1>Hello,<jsp:getProperty name="mybean" property="name" /> !</h1>
</body>
</html>
The IDE uses an Ant build script to build and run your web applications.
The IDE generates the build script based on the options you specify in the
New Project wizard, as well as those from the project's Project Properties
dialog box (In the Projects window, choose Properties from the project
node's right click menu).
1. In the Projects window, right-click the HelloWeb project node and
choose Run (F6). The IDE builds the web application and deploys it to the
server you specified when creating the project. The index.jsp page opens
in your default browser: