Ask User if They Wish to Play Again? Netbean
NetBeans For Java
How To Install and Get Started with Java Programming
(on Windows, Mac OS and Ubuntu)
NetBeans (@ http://netbeans.org) is an open-source Integrated Development Environment (IDE). NetBeans began in 1996 every bit a Java IDE student project at Charles University in Prague. Sun Microsystems acquired NetBeans in 1999. In 2010, Oracle acquired Sun (and thus NetBeans).
Compared with its rival Eclipse (http://www.elicpse.org) (both are open-source, and so I don't know what are they competing for?), NetBeans provides seamless back up for Java AWT/Swing, Java ME mobility pack, Java EE, and bundled with an splendid profiler for performance tuning.
NOTE (2018 Feb 01): NetBeans viii.ii might non work with JDK nine at this moment. Stick to JDK 8 now, if you want to use NetBeans.
How to Install NetBeans eight.2
How to Install NetBeans on Windows
Step 0: Install JDK
To use NetBeans for Java programming, you need to first install Coffee Evolution Kit (JDK). See "JDK - How to Install".
Pace 1: Download
Download "NetBeans IDE" installer from http://netbeans.org/downloads/alphabetize.html. There are many "bundles" available. For beginners, choose the 1st entry "Java SE" (e.g., "netbeans-8.2-javase-windows.exe" 95MB).
Stride ii: Run the Installer
Run the downloaded installer.
How to Install NetBeans on Mac Os 10
To utilise NetBeans for Java programming, yous need to first install JDK. Read "How to install JDK on Mac".
To install NetBeans:
- Download NetBeans from http://netbeans.org/downloads/. Set "Platform" to "Mac Bone X". There are many "bundles" bachelor. For beginners, choose "Java SE" (due east.g., "
netbeans-8.two-javase-macosx.dmg" 116MB). - Double-click the download Disk Image (DMG) file.
- Double-click the "
NetBeans 8.ten.mpkg", and follow the instructions to install NetBeans. NetBeans volition exist installed under "/Applications/NetBeans". - Squirt the Disk Prototype ("
.dmg").
You tin can launch NetBeans from the "Applications".
Notes: To uninstall NetBeans, drag the "/Applications/NetBeans" folder to trash.
How to Install NetBeans on Ubuntu Linux
To use NetBeans for Coffee programming, you demand to first install JDK. Read "How to install JDK on Ubuntu".
To install NetBeans:
- Download NetBeans from http://netbeans.org/downloads/. Choose platform "Linux (x86/x64)" ⇒ "Java SE". You shall receive a
shfile (e.chiliad., "netbeans-seven.x-ml-javase-linux.sh") in "~/Downloads". - Prepare the downloaded
shfile to executable and run theshfile. Open a Terminal:$ cd ~/Downloads $ chmod a+x netbeans-7.x-ml-javase-linux.sh $ ./netbeans-7.ten-ml-javase-linux.sh
Follow the instructions to install NetBeans.
To start NetBeans, run the script "netbeans" in the NetBeans' bin directory:
$ cd netbeans-bin-directory $ ./netbeans
Writing a Hello-world Coffee Program in NetBeans
Step 0: Launch NetBeans
Launch NetBeans. If the "Beginning Page" appears, close it past clicking the "cross" button adjacent to the "Showtime Folio" title.
Stride ane: Create a New Project
For each Coffee awarding, y'all need to create a "projection" to keep all the source files, classes and relevant resources.
- From "File" carte ⇒ Choose "New Project...".
- The "Choose Projection" diglog pops up ⇒ Under "Categories", choose "Coffee" ⇒ Under "Projects", choose "Java Application" ⇒ "Adjacent".
- The "Name and Location" dialog pops upwards ⇒ Under "Project Name", enter "
FirstProject" ⇒ In "Project Location", select a suitable directory to salvage your works ⇒ Uncheck "Use Dedicated Folder for Storing Libraries" ⇒ Uncheck "Create Chief class" ⇒ Finish.
Footstep 2: Write a Hello-world Java Plan
- Correct-click on "
FirstProject" ⇒ New ⇒ Java Class (OR choose the "File" menu ⇒ "New File..." ⇒ Categories: "Java", File Types: "Java Class" ⇒ "Side by side"). - The "Name and Location" dialog pops upwardly ⇒ In "Class Proper noun", enter "
Hello" ⇒ Delete the content in "Package" if it is not empty ⇒ "Finish". - The source file "
Hello.java" appears in the editor panel. Enter the post-obit codes:public course Hello { public static void main(Cord[] args) { Organization.out.println("Hello, earth"); } }
Pace 3: Compile & Execute
At that place is no need to "compile" the source code in NetBeans explicitly, as NetBeans performs the then-called incremental compilation (i.e., the source argument is compiled as and when it is entered).
To run the program, right-click anywhere in the source (or from the "Run" carte du jour) ⇒ Run File. Detect the output on the output console.
Notes:
- Y'all should create a NEW Coffee projection for EACH of your Java application.
- Nonetheless, NetBeans allows you to proceed more than than i programs in a project, which is handy for writing toy programs (such equally your tutorial exercises). To run a particular program, open and correct-click on the source file ⇒ Run File.
Correcting Syntax Fault
NetBeans performs incremented compilation, as and when a source line is entered. It marked a source line with syntax error with a RED Cross. Point your cursor at the RED Cantankerous to view the fault bulletin.
Y'all CANNOT RUN the program if at that place is any syntax error (marked by a RED CROSS before the filename). Right all the syntax errors; and RUN the program.
[TODO] Diagram
HINTS: In some cases, NetBeans shows a ORANGE LIGHT-Seedling (for HINTS) next to the ERROR Cherry-Cross (Line five in the above diagram). Y'all can click on the LIGHT-Seedling to get a list of HINTS to resolve this particular error, which may or may not work!
SYNTAX WARNING: marked by a orange triangular exclaimation sign. Different errors, warnings may or may not cause problems. Attempt to set these warnings likewise. But you tin can RUN your program with warnings.
Read the NetBeans Documentation
At a minimum, you SHOULD READ the "IDE Basics, Getting Started, Coffee Application", which is accessible via NetBeans's "Assistance" carte ⇒ Assist Contents. This will salvage you many agonizing hours trying to figure out how to practise somethings later on.
The "Assistance" ⇒ "Online Doc and Support" (@ http://netbeans.org/kb/alphabetize.html) contains many articles and tutorial on using NetBeans.
The NetBeans "Kickoff Page" also provides many useful links to get you lot started.
Debugging Programme in NetBeans
Step 0: Write a Java Program
The following program computes and prints the factorial of north (=1*2*iii*...*n ). The program, however, has a logical error and produce a wrong answer for n =20 ("The Factorial of xx is -2102132736" - a negative number?!).
ane 2 three 4 five 6 vii 8 9 10 11 12 xiii 14 | public course Factorial { public static void main(Cord[] args) { int n = twenty; int factorial = 1; for (int i = one; i <= n; i++) { factorial *= i; } System.out.println("The Factorial of " + north + " is " + factorial); } } |
Let united states utilise the graphic debugger to debug the program.
Step i: Set an initial Breakpoint
A breakpoint suspends plan execution for y'all to examine the internal states of the program. Before starting the debugger, you need to prepare at least one breakpoint to suspend the execution inside the plan. Set a breakpoint at main() method by clicking on the left-margin of the line containing main(). A blood-red circle or an inverted Triangle appears in the left-margin indicating a breakpoint is set at that line.
Step 2: Start Debugging
Correct click anywhere on the source code ⇒ "Debug File". The program begins execution simply suspends its operation at the breakpoint, i.e., the main() method.
Every bit illustrated in the following diagram, the highlighted line (also pointed to by a dark-green arrow) indicates the statement to be executed in the next pace.
Step 3: Step-Over and Watch the Variables and Outputs
Click the "Step Over" button (or select "Footstep Over" in "Debug" menu) to single-step thru your program. At each of the step, examine the value of the variables (in the "Variable" console) and the outputs produced by your programme (in the "Output" Panel), if any. Yous tin can also identify your cursor at whatever variable to inspect the content of the variable.
Single-stepping thru the program and watching the values of internal variables and the outputs produced is the ultimate mean in debugging programs - considering it is exactly how the figurer runs your program!
Footstep 4: Breakpoint, Run-To-Cursor, Proceed and Finish
As mentioned, a breakpoint suspends program execution and let you examine the internal states of the programme. To set a breakpoint on a particular statement, click on the left-margin of that line (or select "Toggle Breakpoint" from "Run" menu).
"Continue" resumes the program execution, upwards to the next breakpoint, or till the finish of the programme.
"Single-step" thru a loop with a large count is time-consuming. You could fix a breakpoint at the statement immediately outside the loop (e.1000., Line 11 of the higher up plan), and issue "Continue" to complete the loop.
Alternatively, yous can place the cursor on a item statement, and issue "Run-To-Cursor" to resume execution up to the line.
"Finish" ends the debugging session. Always stop your current debugging session using "Terminate" or "Proceed" till the cease of the programme.
Other Debugger's Features:
Modify the Value of a Variable
Y'all can modify the value of a variable by entering a new value in the "Variable" console. This is handy for temporarily modifying the behaviour of a plan, without irresolute the source code.
Step-Into and Step-Out
To debug a method, you need to use "Step-Into" to step into the first statement of the method. Y'all could use "Step-Out" to return back to the caller, anywhere within the method. Alternatively, you could prepare a breakpoint inside a method.
NetBeans - Tips & Tricks
General Usage
These are the features that I detect to be most useful in NetBeans:
- Maximizing Window (double-click): You tin can double-click on the "header" of any panel to maximize that particular panel, and double-click over again to restore it back. This is particularly useful for editing source lawmaking in full console.
- Code Auto-Consummate (or Intelli-Sense) (ctrl-infinite): Enter a fractional statement (eastward.g., Sys) and printing command-space to activate the auto-consummate, which displays all the available choices.
- Javadoc (ctrl-space, alt-F1): Identify the cursor on a method or form, and printing ctrl-space to view the javadoc; or right-click ⇒ Show Javadoc (alt-F1) to open up it on a browser.
- Lawmaking Shorthand (tab): For example, you tin enter "
sout" and press TAB for "Arrangement.out.println"; "psvm" for "public static void chief(Cord[] args) { }" or "fori" + tab for a for-loop. To view and configure code template, choose "Tools" menu ⇒ "Options" ⇒ "Editor" ⇒ "Code Templates". - Formatting Source Code (alt-shift-f): Correct-click on the source (or from the "Source" menu) ⇒ Cull "Format". NetBeans will layout your source codes with the proper indents and format. To configure the formatting, choose "Tools" bill of fare ⇒ "Options" ⇒ "Editor" ⇒ "Formatting".
You can too select the section of codes to be formatted, instead of the entire file. - Hints for Correcting Syntax Fault: If there is a syntax mistake on a statement, a red marking will prove upward on the left-margin on that statement. You could click on the "light bulb" to brandish the error bulletin, and also select from the available hints for correcting that syntax error.
- Rename (Refactor) (ctrl-r): To rename a variable, place the cursor on that variable, right-click ⇒ "Refactor" ⇒ "Rename" ⇒ Enter the new name. All the appearances of that variables in the project volition be renamed.
- Small Programs: You can continue many pocket-sized toy programs (with
main()) in one Java project instead of create a new project for each pocket-sized program. To run the desired program, on the "editor" console ⇒ right-click ⇒ "Run File". - Source Toggle Comment: To temporarily comment-off a cake of codes, choose "Source" ⇒ "Toggle Annotate".
- Error Bulletin Hyperlink: Click on an error bulletin will hyperlink to the respective source statement.
- Command-Line Arguments: To provide control-line arguments to your Coffee program in NetBeans, right-click on the "project" ⇒ "Set every bit Main Projection" ⇒ "Ready Configurations" ⇒ "Customize..." ⇒ "Run" ⇒ select the "Main" grade ⇒ blazon your control-line arguments inside the "Arguments" field ⇒ cull "Run" carte ⇒ "Run Chief Project".
- Line Numbers: To bear witness the line numbers, correct-click on the left-margin ⇒ "Show Line Numbers".
- Changing Font Face and Size: Tools ⇒ Options ⇒ Fonts & Colors ⇒ In "Category", select "Default" ⇒ In "Font", choose the font face up and size.
- Resetting Window View: If you mess up the window view (e.1000., y'all accidentally close a window and cannot detect information technology anymore), yous can reset the view via "Window" menu ⇒ "Reset Windows".
- Lawmaking Templates: For case, when yous create a new Coffee class, NetBeans retrieves the initial contents from the "Java Class" code template. To configure code templates, select "Tools" carte ⇒ "Templates" ⇒ Choose the desired template ⇒ "Open in Editor". To prepare a value of a variable used in the all the lawmaking templates (due east.g.,
$User), select "Tools" menu ⇒ "Templates" ⇒ "Settings". - Displaying Chinese Graphic symbol: Need to cull a font that support chinese character brandish, such as "Monospace", in Tools ⇒ Options ⇒ Fonts & Colors ⇒ Syntax ⇒ default.
- Changing the JDK Location: The Netbeans configuration file is located at "
etc\netbeans.conf". Edit the directive "netbeans_jdkhome". - Let me know if you have more tips to be included here.
Java Application Development
- Choosing the JDK version for your program: Right-click on your project ⇒ "Properties" ⇒ "Source" node ⇒ You can select the JDK level of your projection in pull-donw menu "Source/Binary Format".
- Enabling JDK seven back up: If JDK seven is already installed in your system, right-click on your Project ⇒ "Properties" ⇒ "Source" node ⇒ "Source/Binary Format" ⇒ Select "JDK 7". Likewise check "Libraries" ⇒ Java Platform ⇒ JDK 7.
If JDK 7 is non installed/configured, install JDK 7. Add together JDK 7 support to NetBeans via "Tool" menu ⇒ "Java Platforms" ⇒ "Add Platform...". - Choosing Default Charset: Right-click on your project ⇒ "Properties" ⇒ "Source" node ⇒ "Encoding" ⇒ choose your desired charset for the text-file I/O from the pull-downwards menu.
- Enabling Unicode Support for File Encoding: Right-click on your project ⇒ "Backdrop" ⇒ "Source" node ⇒ "Encoding" ⇒ choose your Unicode encoding (due east.g., UTF-8, UTF-16, UTF-16LE, UTF-16GE) for the text-file I/O.
- To include Javadoc/Source: Utilise "Library Manager" (select the "Tools" menu ⇒ "Libraries"); or "Java Platform Manager" (select "Tools" menu ⇒ "Java Platforms")
- Adding External JAR files & Native Libraries (".dll", ".lib", ".a", ".then"): Many external Coffee packages (such equally JOGL, Java3D, JAMA, etc) are bachelor to extend the functions of JDK. These packages typically provide a "
lib" directory containing JAR files (".jar") (Coffee Archive - a unmarried-file packet of Java classes) and native libraries (".dll", ".lib" for windows, ".a", ".and so" for Linux and Mac).
To include an external JAR file (".jar") into a project: Expand the project node ⇒ Correct-click on "Libraries" ⇒ "Add JAR/Folder..." ⇒ Select the desired JAR file or the folder containing the classes.
If the external parcel contains many JAR files, you could create a user library to contain all the JAR files, and add the library to all the projects that required these JAR files. From "Tools" menu ⇒ "Libraries" ⇒ "New Library..." ⇒ Enter a library name ⇒ Use "Add JAR/Folder..." to add JAR files into this library.
Many JAR files come up with native libraries in the form of ".dll", ".lib" (for Windows) and ".a", ".so" for Linux/Mac. The directory path of these libraries must be included in JRE'southward property "java.library.path". This can be done via right-click the projection ⇒ Prepare Configuration ⇒ Customize... ⇒ Run ⇒ In "VM options", enter "-Djava.library.path=xxx", wherexxxis path of the native libraries.
Notes: The JAR files must be included in theCLASSPATH. The native library directories must exist included in JRE's property "java.library.path", which commonly merely not necessarily includes all the paths from thePATHenvironment variable. Read "External JAR files and Native Libraries".
Writing Java GUI (AWT/Swing) Application in NetBeans
Step 0: Read
- Java GUI Application Learning Trail @ http://world wide web.netbeans.org/kb/trails/matisse.html.
- Swing Tutorial's "Learning Swing with the NetBeans IDE" @ http://docs.oracle.com/javase/tutorial/uiswing/learn/index.html.
Step 1: Create a New "Java Application" Project
- Launch NetBeans ⇒ File ⇒ New Project...
- Under "Categories", choose "Java" ⇒ Under "Projects", choose "Coffee Application" ⇒ Side by side.
- In "Project Name", enter "
FirstNetBeansGUI" ⇒ Choose a suitable directory for your "Project Location" ⇒ Uncheck the "Create Main grade" box ⇒ Finish.
Footstep ii: Write a Java File "JFrame Form"
- Right-click on the project "
FirstNetBeansGUI" ⇒ "New" ⇒ "JFrame Form..." (or "Others" ⇒ "Swing GUI Forms" ⇒ "JFrame Form"). - In "Grade Name", enter "
NetBeansSwingCounter" ⇒ Finish. - Create the GUI Components visually:
- From the "Platte" panel ⇒ "Swing Controls" ⇒ Drag and drop a "
Characterization", "TextField", and "Button" into the blueprint console. - Click on the "
jLabel1" ⇒ In the "Properties" console, enter "Count" in "text" (Y'all can also unmarried-click on thejLabel1to change the text). Right-click on thejLable1⇒ Change Variable Name ⇒ In "New Name", enter "lblCount". - Similarly, for "
jTextField1" ⇒ Change the "text" to 0, and alter the "Variable Name" to "tfCount" ⇒ Resize the text field if necessary. - For "
jButton1" ⇒ Change the "text" to "Count", and alter the "Variable Proper name" to "btnCount".
- From the "Platte" panel ⇒ "Swing Controls" ⇒ Drag and drop a "
- Write the issue handler for the push button by double-clicking the button and enter the following codes:
private void btnCountActionPerformed(java.awt.issue.ActionEvent evt) { count++; tfCount.setText(count + ""); } - Create an case variable
count(just below the form declaration) as follows:public course Counter extends javax.swing.JFrame { int count = 0;
Step 3: Compile & Execute
Right-click the source and select "Run File".
Step iv: Study the Generated Source Code
Expand the "Generated Code" and study how the GUI builder declare, allocate and initialize the GUI Components in the initComponents(). Note how the JButton registers an ActionEvent listener and how an inner class is used as the listener and provide the result handler actionPerformed(). Too notice that the main() method uses a Swing's worker to run the GUI on the Event-Dispatcher thread, instead of the principal thread, for thread-safe operations.
public form NetBeansSwingCounter extends javax.swing.JFrame { int count = 0; public NetBeansSwingCounter() { initComponents(); } private void initComponents() { lblCount = new javax.swing.JLabel(); tfCount = new javax.swing.JTextField(); btnCount = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); lblCount.setText("Counter"); tfCount.setText("0"); btnCount.setText("Count"); btnCount.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.outcome.ActionEvent evt) { btnCountActionPerformed(evt); } }); pack(); } private void btnCountActionPerformed(coffee.awt.event.ActionEvent evt) { count++; tfCount.setText(count + ""); } public static void master(Cord args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new NetBeansSwingCounter().setVisible(true); } }); } individual javax.swing.JButton btnCount; private javax.swing.JLabel lblCount; private javax.swing.JTextField tfCount; } NetBeans and MySQL
Reference: "Connecting to a MySQL Database" @ http://netbeans.org/kb/docs/ide/mysql.html.
NetBeans (JavaEE) provides direct support to MySQL server. You can utilize NetBeans as a GUI customer to admission a MySQL server, every bit well as an authoritative tool (eastward.g., starting and stopping the server).
Configuring NetBeans to Back up MySQL
From NetBeans "Window" card ⇒ Select "Services". The "Services" tab shall announced on the left pane
- Correct-click on the "Databases" node ⇒ "Register MySQL Server". (If you have already registered a MySQL server, you can right-click on Server node "
MySQL Server at hostname:port" ⇒ Properties, to change its properties.) - Select the "Bones Backdrop" tab, enter the hostname, port number, root user and password.
- Select the "Admin Properties" tab:
- Leave the "Path/URL to admin tool" empty.
- In "Path to kickoff control", enter "
<MYSQL_HOME>\bin\mysqld.exe"; in the "Arguments", enter "--console" - In "Path to stop command", enter "
<MYSQL_HOME>\bin\mysqladmin.exe", in the "Arguments", enter "-u root -ppassword shutdown".
- A server node "
MySQL Server at hostname:port" appears.
Database Assistants - Start/Terminate the Server and Create Databases
- You can start the MySQL server by right-clicking on the server node ⇒ select "beginning". [There seems to be a problem here. If a "connectedness refused: connect" error occurs, enter the password again.]
- Once the MySQL server is started and connected, you tin can encounter the list of databases by expanding the MySQL server node. You can create a new database by correct-clicking on it and choose "Create Database...".
Create a new Connexion
You need a connection to manipulate data. You can create multiple connections with different users and default databases.
- Right-click on the "Databases" ⇒ "New Connexion..." ⇒ Select the driver "MySQL Connector/J" ⇒ Adjacent ⇒ Enter hostname, port number, default database, a general username and password ⇒ "Examination Connection" (make sure that MySQL is started) ⇒ Finish.
- A connection node "
jdbc:mysql://hostname:port/defaultDatabase" appears.
Manipulating Data via a Connection
- Right-click on a connection node (east.g., "
jdbc:mysql://hostname:port/defaultDatabase") ⇒ Choose "Connect" (if not connected, provided that the MySQL server has been started). - Y'all can expand the connexion node to view all the databases.
- Expand an existing database. There are 3 sub-nodes "Tables", "View" and "Procedures". Right-click on the "Tables" to create table or execute command. Similarly, right-click on the "View" and "Procedures".
- To view/manipulate the records in a table, right-click on the selected table ⇒ You can choose to "View Information...", "Execute Command...", etc.
- You can right-click on the connection to "connect" or "disconnect" from the server.
Create a SQL Script and Run the Script
You can create a SQL script past right-clicking on a project ⇒ New ⇒ "SQL File". You can run the script by right-clicking on the SQL script ⇒ "Run File" ⇒ Select an existing connectedness (or create a new connexion) to run the script. You could as well run a unmarried statement (correct-click on the statement ⇒ Run Argument) or a selected group of statements (highlight the statements ⇒ Right-click ⇒ Run Choice).
Developing and Deploying Spider web Awarding in NetBeans
Read:
- "Introduction to Developing Web Applications" @ http://netbeans.org/kb/docs/web/quickstart-webapps.html.
- More articles in "Java EE & Coffee Web Learning Trail" @ http://netbeans.org/kb/trails/java-ee.html.
Web (HTTP) Servers
Configuring Spider web Server
You could configure the web server via "Tools" menu ⇒ "Servers".
Tomcat Server
To configure Tomcat Server, select "Tools" carte ⇒ "Servers" ⇒ click "Add Servers":
- Choose Server: Select the desired Tomcat version ⇒ Next.
- Installation and Login Details: In "Server Location", fill in the Tomcat installation directory ($CATALINA_HOME) ⇒ Enter the username/password of a tomcat user with "director" office. You could either cheque the "create user if it does not exist" or define the tomcat user in "
$CATALINA_HOME\conf\tomcat-users.xml" as follows:<tomcat-users> <role rolename="manager"/> <user username="tomcatmanager" password="xxxx" roles="director,managing director-script,admin" /> </tomcat-users>
Running the Web Server
Choose "Services" ⇒ Expand "Servers" node ⇒ Right-click on the desired server ⇒ Showtime/Terminate/Restart.
MySQL Database Server
You can as well manage the MySQL database server directly from Tomcat. Read "NetBeans and MySQL" Section.
Writing a Hello-World Servlet/JSP Web Application
Create a New Servlet/JSP Project
- From "File" carte du jour ⇒ choose "New Project...".
- "Choose Projection" ⇒ Under "Categories", choose "Java Web" ⇒ Nether "Projects", choose "Web Application" ⇒ "Side by side".
- "Proper name and Location" ⇒ In "Projection Name", enter "
HelloServletJSP" ⇒ In "Project Location", select a suitable directory to save your works ⇒ Check "Set every bit Main Project" ⇒ Adjacent. - "Server and settings" ⇒ Choose your server, or "add" a new server ⇒ Next.
- "Frameworks" ⇒ Select none for pure servlet/JSP awarding ⇒ Finish.
Writing a Hi-World JSP
A JSP page chosen "index.jsp" is automatically created, which says "Hello world!". To execute this JSP, right-click on the projection ⇒ "Run". The URL is http://localhost:8080/HelloServletJSP/alphabetize.jsp.
Writing a Hi-World Servlet
- Correct-click on the project "
HelloServletJSP" ⇒ New ⇒ Servlet. - "Proper noun and Location" ⇒ In "Form Proper name", enter "
HelloServlet" ⇒ In "Parcel", enter "hello" ⇒ Next. - "Configure Servlet Deployment" ⇒ In "Servlet Name", enter "
HelloServletExample" ⇒ In "URL Blueprint", enter "sayhello" ⇒ Terminate. - Enter the following codes for "
HelloServlet.java":package hello; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HelloServlet extends HttpServlet { @Override public void doGet(HttpServletRequest asking, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { out.println("<!DOCTYPE html>"); out.println("<html>"); out.println("<head><title>Hello Servlet</title></head>"); out.println("<body><h1>Hello, World (from Java Servlet)!</h1></trunk>"); out.println("</html>"); } finally { out.close(); } } } - To execute the servlet: Right-click on the project ⇒ run ⇒ Change the URL to
http://localhost:8080/HelloServletJSP/sayhello.
Generating a WAR-file for a Web Application
A State of war (Web Archive) file is basically a zip file for distributing web application in single file. You can use WinZip or WinRAR to inspect or unzip the state of war file.
To distribute the project as a state of war-file, right-click project ⇒ "Clean and Build". The war file is created in the "dist" directory. You lot can deploy the web awarding past dropping the war-file into Tomcat'due south "webapps" directory. Tomcat will automatically unzip the state of war-file and deploy the awarding upon startup.
Debugging Spider web Application
The about important reason for using IDE is to utilise the graphic debugger for debugging the program. You can set a breakpoint in your server-side Java codes, and "Debug" a spider web application, similar to a standalone application.
Writing a How-do-you-do-world JSF two.0 Web Application
Create a New JSF ii.0 Project
- From "File" menu ⇒ choose "New Project...".
- "Choose Project" ⇒ Under "Categories", choose "Java Web" ⇒ Under "Projects", choose "Web Application" ⇒ "Adjacent".
- "Proper name and Location" ⇒ In "Project Name", enter "
HelloJSF20" ⇒ In "Project Location", select a suitable directory to salvage your works ⇒ Check "Set as Main Project" ⇒ Adjacent. - "Server and settings" ⇒ Choose your server, or "add" a new server ⇒ Adjacent.
- "Frameworks" ⇒ Cheque "JavaServer Faces" ⇒ In "Libraries", "Registered Libraries", select "JSF two.0" ⇒ Finish.
- An "
index.xhtml" JSF page is generated, every bit follows:<?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://coffee.sunday.com/jsf/html"> <h:head> <championship>Facelet Title</title> </h:caput> <h:body> How-do-you-do from Facelets </h:trunk> </html>
To run this facelet, right-click on the projection ⇒ Run.
Create a new JSF 2.0 Facelet
- Right-click on the projection ⇒ New ⇒ "Other..."
- "Choose File Type" ⇒ Nether "Category", select "JavaServer Faces" ⇒ Under "File Blazon", select "JSF Page" ⇒ Next.
- "Name and Location" ⇒ In "File Proper noun", enter "
HelloJSF20" ⇒ In "Options", check "Facelets" ⇒ Finish. - In "
HelloJSF20.xhtml", enter the following codes:<?xml version='i.0' encoding='UTF-8' ?> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"> <h:head> <championship>Hello JSF 2.0</championship> </h:head> <h:body> <h1>Hello from Facelets</h1> </h:body> </html>
- To execute the JSF page, right-click on the projection ⇒ Run ⇒ Modify the URL to
http://localhost:8080/HelloJSF20/HelloJSF20.xhtml.
Writing a Hello-globe JSF 1.2 Spider web Application
Create a New JSF one.2 Project
- From "File" menu ⇒ choose "New Projection...".
- "Choose Project" ⇒ In "Categories", choose "Java Spider web" ⇒ In "Projects", cull "Web Awarding" ⇒ "Side by side".
- "Name and Location" ⇒ In "Project Proper name", enter "
HelloJSF12" ⇒ In "Projection Location", select a suitable directory to save your works ⇒ Bank check "Set as Chief Project" ⇒ Side by side. - "Server and settings" ⇒ choose your server, or "add together" a new server ⇒ Next.
- "Frameworks" ⇒ Check "JavaServer Faces" ⇒ In "Libraries", "Registered Libraries", select "JSF 1.2" ⇒ Finish.
- A "
WelcomeJSF.jsp" page is generated, every bit follows:<%@folio contentType="text/html" pageEncoding="UTF-8"%> <%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%> <%@taglib prefix="h" uri="http://java.lord's day.com/jsf/html"%> <!DOCTYPE html> <f:view> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-eight"/> <title>JSP Page</title> </head> <trunk> <h1><h:outputText value="JavaServer Faces"/></h1> </body> </html> </f:view>
To run this folio, right-click on the project ⇒ Run.
Create a new JSF ane.2 Folio
- Right-click on the project ⇒ New ⇒ "Other..."
- "Cull File Type" ⇒ In "Category", select "JavaServer Faces" ⇒ In "File Type", select "JSF Page" ⇒ Side by side.
- "Name and Location" ⇒ In "File Proper noun", enter "
HelloJSF12" ⇒ In "Options", check "JSP File (Standard Syntax)" ⇒ End. - In "
HelloJSF12.jsp", enter the post-obit codes:<%@page contentType="text/html" pageEncoding="UTF-eight"%> <%@taglib prefix="f" uri="http://coffee.dominicus.com/jsf/cadre"%> <%@taglib prefix="h" uri="http://coffee.sunday.com/jsf/html"%> <!DOCTYPE html> <f:view> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-eight"/> <title>Hi JSF 1.ii</championship> </head> <body> <h1><h:outputText value="Hullo World!"/></h1> </trunk> </html> </f:view>
- To execute the JSF page, correct-click on the project ⇒ Run ⇒ Alter the URL to
http://localhost:8080/HelloJSF12/faces/HelloJSF12.jsp.
Debugging Web Applications in NetBeans
You tin can debug a webapp just similar standalone awarding. For example, you can set breakpoints, single-stride through the programs, etc.
REFERENCES & Resources
- NetBeans mother site @ http://netbeans.org.
- NetBeans "Help" menu.
bratcheryousbantor.blogspot.com
Source: https://www3.ntu.edu.sg/home/ehchua/programming/howto/netbeans_howto.html
0 Response to "Ask User if They Wish to Play Again? Netbean"
Post a Comment