Introduction To Web Applications
Introduction To Web Applications
This document takes you through the basics of using NetBeans IDE to develop web applications.
It demonstrates how to create a simple web application, deploy it to a server, and view its
presentation in a browser. The application employs a JavaServer Pages™ (JSP) page to ask you
to input your name. It then uses a JavaBeans™ component to persist the name during the HTTP
session, and retrieves the name for output on a second JSP page.
Contents
To follow this tutorial, you need the following software and resources.
Notes:
• The Web and Java EE installation enables you to optionally install the GlassFish V2
application server and the Apache Tomcat servlet container 6.0.x. You must install one of
these to work through this tutorial.
• To take advantage of NetBeans IDE's Java EE 5 capabilities, use an application server
that is fully compliant with the Java EE 5 specification, such as the GlassFish Application
Server V2 UR2. If you are using a different server, consult the Release Notes and FAQs
for known problems and workarounds. For detailed information about the supported
servers and Java EE platform, see the Release Notes.
The IDE creates the $PROJECTHOME/HelloWeb project 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).
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.
3. package org.mypackage.hello;
4.
5. /**
6. *
7. * @author nbuser
8. */
9.
10. public class NameHandler {
11.
12. private String name;
13.
14. /** Creates a new instance of NameHandler */
15. public NameHandler() {
16. name = null;
17. }
18.
19. public String getName() {
20. return name;
21. }
22.
23. public void setName(String name) {
24. this.name = name;
25. }
26.
}
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 in the Source Editor.
The Insert Form dialog box displays.
4. Drag a Text Input item to a point just before the </form> tag, then specify the following
values:
o Name: name
o Type: text
Click OK. An HTML <input> tag is added between the <form> tags.
5. Drag a Button item to a point just before the </form> tag. Specify the following values:
o Label: OK
o Type: submit
6. Type Enter your name: just before the first <input> tag, then change the default Hello
World! text between the <h1> tags to Entry Form.
7. Right-click within the Source Editor and choose Format (Alt-Shift-F) to tidy the format
of your code. Your index.jsp file should now appear similar to the following:
8. <html>
9. <head>
10. <meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
11. <title>JSP Page</title>
12. </head>
13. <body>
14. <h1>Entry Form</h1>
15.
16. <form name="Name Input Form" action="response.jsp">
17. Enter your name:
18. <input type="text" name="name" />
19. <input type="submit" value="OK" />
20. </form>
21. </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. The Insert Use Bean dialog opens.
Specify the values shown in the following figure.
o ID: mybean
o Class: org.mypackage.hello.NameHandler
o Scope: session
Click OK. Notice that the <jsp:useBean> tag is added beneath the <body> tag.
3. Drag a Set Bean Property item from the Palette to a point just before the <h1> tag and
click OK. In the <jsp:setProperty> tag that appears, delete the empty value attribute
and edit as follows.
4. Change the text between the <h1> tags so that it looks like this:
<h1>Hello, !</h1>
5. Drag a Get Bean Property item from the Palette and drop it after the comma between the
<h1> tags. Specify the following values in the Insert Get Bean Property dialog:
o Bean Name: mybean
o Property Name: name
Click OK. Notice that <jsp:getProperty> tag is now added between the <h1> tags.
6. Right-click within the Source Editor and choose Format (Alt-Shift-F) to tidy the format
of your code. The <body> tags of your response.jsp file should now appear similar to
the following:
7. <body>
8. <jsp:useBean id="mybean" scope="session"
class="org.mypackage.hello.NameHandler" />
9. <jsp:setProperty name="mybean" property="name" />
10. <h1>Hello, <jsp:getProperty name="mybean" property="name" />!</h1>
</body>
1. In the Projects window, right-click the HelloWeb project node and choose Run (F6).
Note: By default, the project has been created with the Compile on Save feature enabled,
so you do not need to compile your code first in order to run the application in the IDE.
The index.jsp page opens in your default browser
2. Enter your name in the text box, then click OK. The response.jsp page displays,
providing you with a simple greeting.