Introduction to netbeans Web Application Development

Source: Internet
Author: User
Tags glassfish netbeans

To learn this tutorial, you must have the following software and resources.

Software or resources Required version
Netbeans ide Web & java ee Installation
Release 6.1Or version 6.0
Java Development Kit (JDK) Version 6Or
Version 5
Glassfish Application Server
Or
Tomcat Servlet Container
V2
 
Version 6.x

Note:

  • When installing web & java ee, you can install the glassfish V2 application server and the Apache Tomcat Servlet Container of version 6.0.x. You must install one of them to complete this tutorial.
  • To use the Java ee 5 feature of netbeans IDE, use an application server that fully complies with Java ee 5 specifications, suchGlassfish V2 ur2 Application Server. If you are using another server, please referRelease NotesAndFAQsTo learn about known issues and solutions. For more information about supported servers and Java EE platforms, seeRelease Notes.
  • If you want to compare projects with work solutions, you canDownload the sample application.

Set a web application project
  1. Select "file"> "new project" (CTRL-shift-N) from the main menu ). Select "Web" under "category ". Under project, select Web application and click Next ".
  2. In step 2, enterHelloweb. Note that the context path on the server will change/Helloweb.
  3. Specify the "Project location" as any directory on the computer. In this tutorial$ Projecthome.

    Note:Netbeans ide 6.1 provides some new options for creating projects. You can retain the default settings for these options. For example,Use dedicated folders to store Libraries"Check box is retained to unselected.

  4. If you are using netbeans ide 6.1, click "Next"; otherwise, continue with Step 5.
  5. Select the server in which the application is to be deployed. Only servers registered in IDE are listed here.
  6. Select the Java EE version to use with the application, and click "Next ".
  7. On the framework panel, click Finish to create a project.

    IDE will create$ Projecthome/HellowebProject folder. This project folder contains all source code and project meta data, such as the ant generation script of the project. In IDE, The helloweb project is opened. In the source code editor of the main window, the welcome page index. jsp is displayed. You can view the file structure of a project in the files window (Ctrl-2) and view its logical structure in the projects window (Ctrl-1.

Create and edit web application source files

Creating and editing source files is the most important feature provided by IDE. After all, this is probably the most time-consuming job. IDE provides various tools to cater to the individual style of any developer, whether you are willing to write all the code manually or want ide to generate a large amount of code for you.

Create a Java package and a Java source file
  1. In the project window, expand the source package node. Note that the "source package" node only contains an empty default package node.
  2. Right-click the "source package" node and choose "New"> "Java class ". In the "Class Name" text box, enterNamehandlerAnd in the package combo box, typeOrg. mypackage. Hello. Click Finish ". Note that the newNamehandler. JavaFile.
  3. In the source code editor, you can declareStringVariable:
    String name;
  4. Add the following constructor to the class:
    public NameHandler()
  5. InNamehandler ()Add the following code lines to the constructor:
    name = null;

Generate getter and setter Methods
  1. In the source code editor, right-clickNameField, and then select "refactor"> "encapsulate field ". The "encapsulate fields" dialog box is displayed, listingNameField. Note that by default, "field visibility" is set to "private", and "access method visibility" is set to "public ", this indicates that the access modifier declared by the class variable will be specified as private, and the getter and setter methods will respectivelyPublicAndPrivateModifier generation.
  2. Click "refactor ". TheNameThe getter and setter methods for generating fields. The modifier of the class variable will be setPrivateThe getter and setter methods are generated with the public modifier. The Java class should now be similar to the following code:
    package org.mypackage.hello;/** * * @author nbuser */public class NameHandler {    private String name;    /** Creates a new instance of NameHandler */    public NameHandler() {       name = null;    }    public String getName() {       return name;    }    public void setName(String name) {       this.name = name;    }}

Edit the default assumerver pages File
  1. Click index. jsp at the top of the source code editor"To re-select the file.
  2. In the component Panel (Ctrl-Shift-8) located at the right of the source code editor, expand HTML forms and drag a form item to the source code editor<H2>Mark the location after the tag. The Insert Form dialog box is displayed:

    Specify the following values:

    • Operation:Response. jsp
    • Method:Get
    • Name:Name input form

    Click OK ". InIndex. jspAdd an HTML form to the file.

  3. Drag a text input item to the nearest</Form>Mark the previous position and specify the following values:
    • Name:Name
    • Type:Text

    Click OK ". In<Form>Add an html<Input>Mark.

  4. Drag a button item to the nearest</Form>Mark the previous position. Specify the following values:
    • Tags:OK
    • Type:Submit

    Click OK ". In<Form>Add an HTML button between tags.

  5. In<Input>MarkEnter your name:, And then<H2>Default between tagsHello world!Text changedEntry Form.
  6. Right-click the source code editor and select "format code" (Alt-shift-F) to organize the code format.Index. jspThe file should now be similar to the following code:
        <body>        

Create the assumerver pages File
  1. In the "project" window, right-click the "helloweb" project node and choose "New"> "jsp ". The new JSP file Wizard will open. Name the fileResponseAnd then click Finish ". Note that in the "project" window, "index. jsp""Response. jsp" is displayed below"File node, and the new file will be opened in the source code editor.
  2. In the component panel on the right of the source code editor, expand "jsp" and drag a bean entry to the source code editor.<Body>Mark the position below. The insert and use bean dialog box is displayed. Specify the following values:

    • ID:Mybean
    • Class:Org. mypackage. Hello. namehandler
    • Range:Session

    Click OK ". Note that<Body>Add below the tag<JSP: usebean>Mark.

  3. Drag a set bean property item from the component panel to the nearest<H2>Mark the previous position and click OK ". In<JSP: setproperty>Mark, delete emptyValueProperty, and then edit it as the following code:
    <jsp:setProperty name="mybean" property="name" />

    As<JSP: setproperty>As described in this document, you can set attribute values in various ways. In this example,Index. jspUser input on the page will be passedRequestObject Name/value pair. When using<JSP: setproperty>When setting attributes, you canRequestThe property name in the object to specify the value. ThereforePropertySetNameTo retrieve the value specified by user input.

  4. Change the text between the <H2> flag to make it as follows:
  5. Drag and Drop a bean property item from the component panel<H2>Comma between tags. In the insert get Bean Properties dialog box, specify the following values:
    • Bean Name:Mybean
    • Property Name:Name

    Click OK ". Note that<H2>Add between tags<JSP: getproperty>Mark.

  6. Right-click the source code editor and select "format code" (Alt-shift-F) to organize the code format.Response. jspFile<Body>The tag should now be similar to the following code:
    <body>    <jsp:useBean id="mybean" scope="session" class="org.mypackage.hello.NameHandler" />    <jsp:setProperty name="mybean" property="name" />    

Generate and run a web application project

IDE uses the ant generation script to generate and run web applications. This script is generated by IDE Based on the options you specified in the "New Project" Wizard and the "Project Properties" dialog box of the project (in the "project" window, right-click the project node, select "attribute" from the menu that appears.

  1. In the "project" window, right-click the "helloweb" project node and select "run" (F6 ). IDE will generate a web application and deploy it on the server you specified when creating this project. Will be opened in the default browserIndex. jspPage:
  2. Enter your name in the text box and click OK ". Will appearResponse. jspPage to show you a simple greeting:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.