Creating a simple web application and deploying it to Tomcat

Source: Internet
Author: User
Creating a simple web application and deploying it to Tomcat Current Revision (Unreviewed)
Contents

[Hide]

  • 1 Introduction
  • 2 Prerequisites
    • 2.1 Integrating Tomcat server with intellij idea
  • 3 Creating a new project
  • 4 Processing a web application
  • 5 Creating elements of your application
    • 5.1 Creating a helloworld class
    • 5.2 Defining a styles.css style sheet
    • 5.3 Developing an application index. jsp Home Page
  • 6 Packing the application data to deploy
  • 7 Getting ready for deployment
  • 8 Running a web application on a local Tomcat server
  • 9 More information

Introduction

Intellij idea supports integration with the Apache Tomcat Servlet Container and makes it possible to develop and deploy Web applications right from within the IDE. this spares you from the need to download any libraries and create a deployment descriptor manually. intellij idea also arranges the sources of your application so they comply with the requirements to the runtime application organization.

Intellijidea helps:

    • Integrate intellij idea and the local Tomcat server.
    • Create a web application using the new project wizard and get all the facilities required for web development.
    • Using e a web application as a tree-view of files and folders.
    • Create elements of a Web application.
    • Pack the data to deploy. For this purpose, intellij idea uses an artifact which defines the files and resources to be packed and where to put them after packing.
    • Create a run configuration.

Prerequisites
    • You are workingIntellij idea Ultimate EditionVersion9Or higher.
    • Tomcat 6 server is installed on your machine and integrated with intellij idea. Let us configure this integration in the next section.

 

Integrating Tomcat server with intellij idea

Before we start working on an application, let's integrate Tomcat server and intellijidea using the application server configuration. this can be done even without an open project, because application server settings belong to the level of your workspace.

To do that, pressCTRL + ALT + S, And then underIDE settings, ClickApplication Servers. On the Application Servers page, click (1), and selectTomcat server. In the Tomcat server dialog box, specify two things:

    • Tomcat home(2)-a directory where your Tomcat server is installed.
    • Tomcat base Directory(3)-a directory where Tomcat config files are located (by default the tomcat installation directory is used ).

Having defined the paths for your local Tomcat server, click OK and return toApplication ServersPage. Let's assign some meaningful name to our server configuration-let it be Tomcat 6.

If you so need, you can extend the configuration with the varous additional libraries, but for our example they are not required.

That is all; your local Tomcat server is ready for running applications from intellij idea, and we'll now start with the project.

Creating a new project

The first step in developing a web application in intellij idea is to create a project:

ChooseFile|New projectOn the main menu or clickCreate new projectIcon on the welcome screen.

On the first page of the new project wizard, make sure that the optionCreate project from scratchIs selected.

On the second page of the wizard, specify the name of the project. SelectCreate moduleCheck box, and chooseJava ModuleAs the module type. The name of this module will beHello_world.

On the third page of the wizard, chooseCreate source directoryOption and accept the default nameSRCFor It.

On the fourth page of the wizard, We'll enable Web development through the dedicatedWeb facet. To do that, let's selectWeb ApplicationCheck box.

Note: The termFacetIs used exclusively in intellij idea. A facet determines the way intellij idea treats the contents of a module, the set of components to be downloaded and configured, And the descriptors to be generated.

Now the skeleton of our application is ready. First, let's look deeper into its structure.

Processing a web application

To export e our web application, we'll use the project tool window that shows the following files and folders:

The. Idea (1) folder contains a number of subfolders, mainly with internal intellij idea information.

The Web (2) folder contains a WEB-INF subfolder with the Web. XML (3) Application descriptor.

Index. jsp file (4) generated by intellij idea represents the home page of your application.

The external libraries (5) folder contains all the libraries required for web development.

Besides generating the above structure and data, intellij idea has produced an artifact to deploy. so we can start the application straight away: In the project tool window, right-click index. JSP and choose run index. JSP on the context menu. intellij idea creates a temporary run configuration index. JSP on the fly and launches the application.

Your default browser displays the following page:

Congratulations! You have created and launched a web application. Now it is just a stub, and we are going to expand its functionality, which is described in details in the following section.

Creating elements of your application

To iterate strate the mainstream web development workflow in intellij idea, let's develop a simple "Hello world !" Application that will consist of the following elements:

    • A Java class with one method that returns a "Hello, world !" String.
    • An application home page that displays the result of executing this Java code.
    • A style sheet to define the layout of the homepage.

Creating a helloworld class

Let's start with creating a helloworld Java class inside a package.

Create a sample package: Right-clickSRCNode, chooseNew|PackageOn the context menu, and typeSampleInNew packageDialog box.

Right-clickSampleNode, chooseNew|Java class, And typeHelloworldInCreate new classDialog box.

Intellij idea creates a stub class helloworld with the package statement and class declaration:

Type the following code of the getmessage method inside the stub class:

Public static string getmessage (){

Return "Hello, world !";

}

Defining a styles.css style sheet

Now let's configure the layout of our home page by defining a style sheet. We won't configure a sophisticated Layout-defining the color and positioning for messages seem enough to define strate the process.

Create a style sheet: Right-click the Web node, chooseNew|FileOn the context menu, and typeStyles.cssInNew FileDialog Box. intellij idea opens the new styles.css file in the editor.

Let's make the messages green and centered-for this purpose, type the following code:

. Message {color: green; text-align: center ;}

Developing an application index. jsp Home Page

On creating the project, intellij idea has produced a stubIndex. jspFile which will be used as the home page of your application. Let's add more flesh to its code, to show the result of executing the helloworld class.

    • OpenIndex. jspIn the Editor (F4 ).
    • Add a link to the style sheet:

<LINK rel = "stylesheet" type = "text/CSS" href = "styles.css"/> (2)

    • Replace the default text inside the <body> </body> tag as follows (3 ):

<H3 class = "message"> <% = helloworld. getmessage () %>

As you type, intellij idea suggests you to import the sample. helloworld class.

Press Alt + enter. Intellij idea inserts the following import statement:

<% @ Page import = "sample. helloworld" %>

Now we have a Java class, a style sheet and a JSP page ready. Our next step is to prepare the data to be deployed to the Tomcat server, which is discussed in the next section.

Packing the application data to deploy

To tell the Tomcat server what to process and where to find the application data, you need to define an artifact. an artifact can be an archive (WAR) or an exploded directory. in our case, when the project output is stored and processed locally, it is enough to submit the data in the exploded form.

During the project setup, intellij idea has created a default artifactHello_world: war exploded.

Let's examine the contents of this artifact more closely:

Press CTRL + ALT + Shift + S and click artifacts. In the artifacts page, selectShow CONTENTS OF ELEMENTSCheck box (1 ).

According to this artifact definition, the following will be submitted to the server:
    • The result of compilingHello_world Module(2): The compiled class sample. helloworld.
    • The contents of the web folder (3 ):Index. jspAndStyles.css.
    • The deployment descriptorWeb. xml(4 ).
The location of the application data to be processed by the Tomcat server is defined in Output directory Field (5 ).

Note: When you run Web applications on a local Tomcat server, the application sources are not copied to the Tomcat server installation folder. intellij idea stores all the project output data required for running an application in a local folder which emulates the organization of a Web application on the server.

This artifact definition is enough to have our application successfully deployed. you do not need to change anything here, provided that all the required static content resources (.jpg ,. PNG ,. CSS, etc .) are stored inWebFolder root.

We are getting closer to our goal-deploying Hello world application. In the next section, let us prepare everything for deployment.

Getting ready for deployment

Let us now create a run configuration, which will contain the parameters required to deploy and launch your application. To do that, chooseRun|Edit configurations.

InRun/debug deploymentsDialog box, click, and from the list of available run/debug invocations, selectTomcat-Local:

The page for the Tomcat server consists of four tabs; in our example we'll use just two of them.

InServerTab, let's specify the following:

    • Name of the run configuration (1): hello_world_local.
    • Application Server to use (2): Tomcat 6.
    • URL address at which the home page of the application will be available (3): http: // localhost: 8080.

To have the default browser opened automatically upon the application start, selectStart BrowserCheck box (4 ).

Next, inDeploymentTab, let's choose the artifact to be deployed: select the check box next toHello_world: war explodedArtifact (1 ):

Next, specify the application context root. InApplication ContextField (2), leave the default/(slash ).

SelectBuild artifacts'Check box (3 ).

Now the run/debug configuration is ready, we can save it, and proceed with launching our application.

Running a web application on a local Tomcat server

FromSelect Run/debug ConfigurationDrop-down list (1) on the toolbar, chooseHello_world_local, And click on the toolbar (2 ).

Intellij idea opens the http: // localhost: 8080/index. jsp page in the default browser:

Congrats! You have created and deployed a web application on a Tomcat server.

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.