Creating a Simple JSP Project

As JSPs were discussed in the last post, let us look into how to create a simple JSP application with the help of NetBeans and Apache Tomcat.

Adding Tomcat Server to NetBeans

After downloading the Tomcat Server, the following steps can be followed to add the server to NetBeans.

1. Open NetBeans.
2. Navigate to Window -> Services
3. In the left hand pane, right click Servers and click Add Server

4. In the dialog box, select Apache Tomcat or TomEE and click Next.

5. Click Browse and select the Apache Tomcat folder from the location folder.

6. Enter the manager-script username and password (Deselect "Create User" check box) 

7. Click Finish. 


Now that you have added the Tomcat Server, let us look into creating a JSP application to print the current date on the screen.

Creating JSP Application

1. In NetBeans, create a new project. Navigate to Java Web and select Web Application to create it. 
Name the project and click Next.


2. In the dialog box, select Apache Tomcat or TomEE as the Server and click Finish.


3. When you expand the created project, there will be a folder named Web Pages.
Right click Web Pages and select New -> JSP
Name the JSP file and click Finish.


4. Type the following code into the JSP file.

<%@page contentType="text/html" pageEncoding="UTF-8"%>                     
<%@page import="java.util.Date" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Bank System</title>
    </head>
    <%
            Date newDate = new Date();
             
            %>
    <body>
        <h1>Welcome to the Bank System</h1>
        <p>Today's date is <%= newDate %></p>
        
    </body>
</html>



Tips:

  • When typing import statements in JSP files, the page tag should be used as shown above, within the markers of Java code. 
  • If the file shows errors even when the Java code is correct, try saving the file or compiling the file (F9) to get rid of the errors.
5. After the code has been compiled and the build is successful, right click the file name to run it.


This action will start the Tomcat server and build the file.

6. The file will then open in the default browser of NetBeans.


Happy Coding!




Comments

Popular Posts