A Step-by-step Guide to Creating NetBeans Web Apps

Web application refers to a software solution that is deployed on a server and can be accessed online through a web browser. These types of applications often intertwine more than one technology; creating one from scratch has numerous approaches. The basic idea is that one should have a good understanding of the technologies involved and programming practices like MVC. NetBeans as an IDE provides many features to ease the development process. This article shows how to create a web application in NetBeans using the minimum of tools and technologies.

Enterprise or Web Application Project in NetBeans

Web applications basically run on a web server such as Tomcat (which is also a servlet container), while an enterprise application consists of EJB components and needs an application server such as GlassFish, Wildfly, etc., as an enterprise container. This is the basic difference between web and enterprise application projects from the NetBeans IDE perspective.

In NetBeans, when we create a web application project using a web server such as Tomcat, the choice is to use no – EJB technologies for server-side scripting. (One such technology, called Spring, has become a widely used and popular choice among developers.) However, if we choose to use an application server like Glassfish or Wildfly, we are open to using both Servlet/JSP and EJB etc. Therefore, if we use one of these application servers to create a pure web application, we are actually using more than we need. No problem, but it’s a waste of resources because application servers are clearly “heavier” than web servers.

Another distinguishing feature between them is that the final archive file created by a web application project is called a WAR and the final file created by the Enterprise Application Project is an EAR file. Therefore, they have their corresponding file extension, .WAR and .EAR, respectively.

Parts of a web application

A typical web application has three parts, called the front tier, The middle tier and the back-end tier or database tier.

See Also:  How to set up a shop on your blog to sell your own products

The front-end tier typically uses HTML, CSS, JavaScript, etc., for client-side rendering and processing . There are many other JavaScript-enabled client-side toolkits, such as Jquery, Flex, Vaadin, GWT, and the like, that can be used to get data from the server-side, or we can weave more than one technology together.

The middle tier contains the business logic of the application and does all the server-side processing with the web server, such as session management, data persistence, transaction management, request/response processing, etc. In Java, we can use a Servlet, JavaBeans, or EJB to implement middle-tier business logic.

The database tier interacts with the database with JDBC. It usually creates a bridge to interact with the middle tier for further processing.

This is how you design a web application project. Beyond this simplified view, there are many other technologies that can be embedded to improve the responsiveness and capabilities of a web application. But here, in the next project, we will stick to the use of minimal technologies. However, the idea can be used to include other technologies such as JPA, AJAX, JavaScript, web services, and EJBs.

Tomcat is an open source web server developed by the Apache Software Foundation (ASF). It is more than just a web server because it implements some of the Java EE specifications, such as Java Servlet, Java Server Pages (JSP), Java EL, and WebSocket, as well as being a pure HTTP web server. Because it does not implement the EJB specification, it cannot be called a fully Java EE compliant application server. However, there is an enterprise version of this web server called Apache TomEE; Java EE 7 is not yet supported at the time this article was written, but Java EE 6 is supported.

Problem Statement

With emphasis on the steps to create a Web application in NetBeans, the problem we are trying to implement is rudimentary. We’ll use JSF and HTML to send a number to the server, which will respond with an appropriate number converted to binary, hexadecimal, or octal. The result will be rendered in the browser along with the user input interface.

See Also:  How to create a watermark logo in i movie

Step 1: Create a web application project

  1. Open NetBeans. File → New Project…Starting a new projectFigure 1: Starting a new project
  2. Select Java Web from the categories and Web Application from the Projects list. Click Next.
  3. Provide a Project Name, for example WebProject1. Click Next.Name the projectFigure 2: Name the project
  4. Select the server, eg Apache Tomcat, leave all other values ​​as default. Click in Next.Selecting the serverFigure 3 : Selecting the server
  5. Select JavaServer Faces Web Application Framework from the list of frameworks. Leave the default settings. Click Finish.
  6. This will create the project structure as follows. Project Structure SelectionFigure 4: Project Structure Selection

Step 2: Create the Model Class

  1. Right-click the source package in the project tree structure and select New… → Java Class.Selecting a new class JavaFigure 5: Selecting a new Java class
  2. Enter the name of the class, for example Converter. Provide the package name, such as org.webproject1.model and click Finish. This will create a Java class file with the given name. The content of this file is given below.

package org.webproject1.model; public class converter { private int number; private string result; public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public String getResult() { return result; } public void setResult(String result) { this.result = result; } public void toBinary() { result = Integer.toBinaryString(number); } public void toHex() { result = Integer.toHexString(number); } public void toOctal() { result = Integer.toOctalString(number); } public void clear(){ result = “”; } }

Step 3: Create the controller class

Similar to the above, create another Java class called ConverterController in another package called org.webproject1.controller. The content of the file is listed below.

package org.webproject1.controller; import javax.faces.application.FacesMessage; import javax.faces.bean.ManagedBean; import javax.faces.component.UIInput; import javax.faces.component.UIPanel; import javax.faces.context.FacesContext; import org.example.model.Converter; @ManagedBean(name = “converterController”) public class ConverterController { private Converter converter = new Converter(); private UIPanel results panel; Private UI Entry Number Entry; public converter getConverter() { return converter; } public void setConverter(Converter converter) { this.converter = converter; } public UIPanel getResultPanel() { return resultPanel; } public void setResultPanel(UIPanel resultPanel) { this.resultPanel = resultPanel; } public UIInput getNumberInput() { return numberInput; } public void setNumberInput(UIInput numberInput) { this.numberInput = numberInput; } public String toBinary() { FacesContext ctx = FacesContext.getCurrentInstance(); try { converter.toBinary(); resultPanel.setRendered(true); ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, “Decimal number converted to binary”, null)); } catch (Exception ex) { resultPanel.setRendered(false); ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getMessage(), null)); } returns null; } public String toHex() { FacesContext ctx = FacesContext.getCurrentInstance(); try { converter.toHex(); resultPanel.setRendered(true); ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, “Decimal number converted to hexadecimal”, null)); } catch (Exception ex) { resultPanel.setRendered(false); ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getMessage(), null)); } returns null; } public String toOct() { FacesContext ctx = FacesContext.getCurrentInstance(); try { converter.toOctal(); resultPanel.setRendered(true); ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, “Decimal number converted to octal”, null)); } catch (Exception ex) { resultPanel.setRendered(false); ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getMessage(), null)); } returns null; } public String clear() { FacesContext ctx = FacesContext.getCurrentInstance(); try { converter.clear(); resultPanel.setRendered(false); ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, “Results Cleared”, null)); } catch (Exception ex) { resultPanel.setRendered(false); ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getMessage(), null)); } returns null; } }

Step 4: Create the View

Notice that the index.xhtml file is created automatically when we create the project. Open the file and add the following changes.

Facelet Title

Converter

Result

Step 4: Deploy and run the project

Select Run → Run Project from the NetBeans menu.This will automatically compile the Java classes, start the server idor Tomcat, it will deploy the project to the server and finally open the project in a browser. Here is the output we get from the above code.

 The project is runningFigure 6: The project is running

Conclusion

If we delve into the technologies used in a Web project, there are also lots and lots to understand about its use. Here, we emphasize implementing one from scratch. We could have used JSP and Servlet as well, but JSF seems to be the de facto technology of official Java web application development. It seems that JSF may take over for JSP/Servlet in the future. JSP/Servlet will either be deprecated or continue as a legacy technology with JSF-focused enhancements.

.

See Also:  Log to a File in Python

Leave a Reply

Your email address will not be published. Required fields are marked *