Creating Servlet With Intellij V9

coldwaterphewΔιακομιστές

17 Νοε 2013 (πριν από 3 χρόνια και 6 μήνες)

83 εμφανίσεις

Bibek Prajapati
Creating Servlet With Intellij V9


1


Creating Servlet With Intellij V9


Abstract


This
write
-
up

has been done as a mid
-
term assignment for SW410 course for
spring

2010. This
paper describes the process of configuring servlets and running a simple jsp with Intellij version
9.

This write
-
up act
s as a tutorial on simple enterprise Java configuration and deployment.


1.

The Problem


The IDE manufacturer must find a way to deploy applications to a wide variety of application
servers. Each is different from the other, resulting in different procedures
for packaging and
deployment.


Typically, a programmer will select an application
server
, set it up, verify its operation, select an
IDE, develop a sample application, deploy it and test.


2.
The Application Server


Apache Tomcat is an open source software

implementation of the

Java Servlet

and

Java Server

Pages

technologies. You can download the latest version of Apache Tomcat from
http://tomcat.apache.org
.


On windows, download

the zip file of Apache Tomcat

and
un
zip it at any location. If it is

unzip
ped
at folder “C:
\
apache tomcat” then go to the
installed folder

and select the folder n
amed
“bin” where
tomcat

can be started

by running the startup.bat file.
There must be
JDK installed
before
starting

Tomcat. The JD
K contains the Java runtime engine. The environment variable
JAVA_HOME
tells Tomcat where to find
JDK.


For example, if JDK
is installed
in the C:
\
Program Files
\
Java
\
jdk1.6.0_11 directory,
right click
on Computer and select Properties.



Fig 2
-
1: Select
Properties on Computer

Bibek Prajapati
Creating Servlet With Intellij V9


2


Then, the
following Screen of the Control Panel’s System

will appear
:




Fig 2
-
2:
Screen
shot

of the Control Panel’s System


Select Advanced S
ystem Settings and
the new dialog box of System Properties.
Then, click

the
button Environm
ent Variables in System Properties
dialog;

the followi
ng Environment Variables
Dialog box will appear:




Fig 2
-
3: Environment Variable Dialog Box


In user variables, select “New..
”.


Bibek Prajapati
Creating Servlet With Intellij V9


3


T
he New User Variable dialog box
will be displayed
as follows:




Fig
2
-
4: New User Variable Dialog Box



T
he variable name and value for that variable

can be set in this dialog box
. Give variable name
as “JAVA_HOME” and value as “
C:
\
Program Files
\
Java
\
jdk1.6.0_11
” in the dialog box and
click ok.


After Starting Tomcat by ru
nning the startup.bat file,
open
browser and type the following URL:
http://localhost:8080. Tomcat uses port 8080 by default.
When Tomcat starts successfully, an
introduction page in browser

can be seen
similar to

following page:




Fig 2
-
5: Introduction
Page of Tomcat


S
hut down Tomcat using shutdown.bat file from bin folder of Tomcat.


Bibek Prajapati
Creating Servlet With Intellij V9


4


Creating user in Tomcat:


T
o create new user fo
r Tomcat with manager role,
go to conf folder of Tomcat installation folder
and then open tomcat
-
user.xml file and append th
e following line in it:


<role rolename="manager"/>

<user username="bibek" password="bibek" roles="manager"/>


This

will create a new user with username as “bibek”, password as “bibek” and role as a
manager.




Fig 2
-
6: tomcat
-
user.xml file in Conf Folder

Bibek Prajapati
Creating Servlet With Intellij V9


5


3.

The Intellij

Idea


This section describes the set
-
up for deploying a servlet to Tomcat using Intellij Idea 9.0.1. First
of all, start the Intellij Idea 9.0.1. Then go to File menu and select “New Project…”.
T
he
following dialog

will appear
:




Fig 3
-
1:
New Project Dialog Box


Select “Create Project from scratch” and click next.




Bibek Prajapati
Creating Servlet With Intellij V9


6


T
he following dialog

box

will appear
:


Fig
-
3
-
2: Dialog Box for providing project name and project file location

In this dialog,
provide a project name

(HelloWorldServlet)

and

project
file location.

Then select
next.



Bibek Prajapati
Creating Servlet With Intellij V9


7


T
he following dialog

box

will appear
:


Fig 3
-
3: New Project Dialog Box for creation of new source directory

This dialog enables the creation of new source directory called
src
. Select Next.




Bibek Prajapati
Creating Servlet With Intellij V9


8


T
he following t
echnology dialog box

will appear
:


Fig 3
-
4: Technology Dialog Box for select technology for the new project

This dialog box enables to select the technology. Select Web Application as technology. Then
select “Finish”. After clicking finish, IDE will creat
es new project.



Bibek Prajapati
Creating Servlet With Intellij V9


9


The new project window will be shown as follows:


Fig 3
-
5: The New Project Window

When we get this new project window, we are ready to write our first servlet in Intellij Idea
9.0.1.

Bibek Prajapati
Creating Servlet With Intellij V9


10


4.

Creating First Servlet

In this
section, we will desc
ribe how to write servlet using the Intellij Idea 9.0.1. Select the “src”
folder in project window and right click. Select “New” which will pop up sub menu. Then select
“S
ervlet”. The s
c
reensh
ot of the process is shown
below:


Fig: 4
-
1: Screenshot of crea
ting new servlet

Bibek Prajapati
Creating Servlet With Intellij V9


11


After servlet is selected, we will get the following dialog box:


Fig 4
-
2: New Servlet Dialog Box

We can provide a servlet name, package name and servlet class in the new servlet dialog box. In
the above dialog box, we have provided servl
et name (HelloServlet) and servlet Class
(HelloServlet). We have left package name as blank which means the servlet class will be
cre
ated in default folder,
src.

Press Ok

to create new servlet.













Bibek Prajapati
Creating Servlet With Intellij V9


12


Select HelloServlet from src folder. We will get f
ollowing screen.




Fig 4
-
3:
Keying in the

new servlet

The above
screen

shows

user entering into new servlet.

Bibek Prajapati
Creating Servlet With Intellij V9


13


Some
library file from Apache Tomcat must be

added

to

the project. For that, right click on the
project name

in project window

and select modul
e settings as shown in the following figure

below
:


Fig 4
-
4: Screenshot for selecting module setting

Select “module setting”.

Bibek Prajapati
Creating Servlet With Intellij V9


14


When “module setting” is selected
, the project
structure dialog box

will be displayed as follows:


Fig 4
-
5: The Project Structu
re dialog box


Select Libraries and press “+” sign to add new project library. The Create Library dialog box
appears as follows:


Fig 4
-
6: Create Library dialog box where library name is provided

Provide any library name. In fig 4
-
6, library name is provi
ded as j4p. Then press OK.



Bibek Prajapati
Creating Servlet With Intellij V9


15


After selecting OK in Create Library dialog box,

Choose modules


dialog box appears as
follow:


Fig 4
-
7: Choose Module dialog box

Since, the library is going to be added to HelloWorldServlet module. Select OK.
T
he project
st
ructure will look like following:


Fig: 4
-
8: Project Structure Dialog Box after creating library

Select “Attach Classes…” in the Project Library “j4p” section of Project Structure dialog box.


Bibek Prajapati
Creating Servlet With Intellij V9


16


After s
elect
ing

“Attach Classes…”, the “Select Path” dialog bo
x will appear as follows:


Fig 4
-
9: Select Path dialog box to select jar/zip files where library class are located

In Select Path dialog box, select the lib folder of apache tomcat installation folder and then select
all jar files in the lib folder. Then
Select OK. The project
library “j4p” in Project Structure dialog
box appears

as follows:


Fig: 4
-
10: Project Library j4p after adding jar file from lib folder of Apache Tomcat

Select Ok. Then all the red marks in code editor as show in fig 4
-
3 will disapp
ear.

Bibek Prajapati
Creating Servlet With Intellij V9


17


5.

Configuration of
the
IDE for Deployment

In this
section,
we will

configure Intellij Idea

9.0.1 for deployment of the application. First of all,
go to Run menu and select “Edit Configurations”. Press the + sign to add new configuration and
select Tomca
t Server. In Tomcat server, select local.


Fig 5
-
1: Selecting the local server





Bibek Prajapati
Creating Servlet With Intellij V9


18


After Se
lecting Tomcat Server configuration as “Local”, you will get following dialog:


Fig 5
-
2: Tomcat Configuration dialog box

If application server is not configured th
en select configure button.






Bibek Prajapati
Creating Servlet With Intellij V9


19


We will get a dialog box to add application server which looks like following:


Fig 5
-
3: Configuring Application Server

Select the “+” sign to add application server. You will get the following dialog box:


Fig: 5
-
4: Co
nfiguring Tomcat Server

Here, we have to provide the home directory of Tomcat and select OK.


Bibek Prajapati
Creating Servlet With Intellij V9


20


Then we will get the following application server dialog:


Fig 5
-
5: Application Server Configuration Dialog Box for Tomcat 6

Select Ok, and then application ser
ver for Tomcat is successfully configured.









Bibek Prajapati
Creating Servlet With Intellij V9


21


When appli
cation server is configured, we

will get the Run/Debug Configuration as follows:


Fig 5
-
6: Run/Debug Configuration Dialog Box with Tomcat 6 as Application Server

Provide the name (HelloServlet)
for the configuration. Always check the Start Browser and
select the startup page (
http://localhost:8080/helloworld
). Then select

deployment tab

and check
“HelloWorldS
ervlet:war exploded”. Then,

select OK.

After Completing Configuration, the other thing th
at we have
to
do is add a servlet mapping. For
this, select the web.xml file that is in “web/WEB
-
INF/” directory. After opening web.xml file,
select assembly descriptor.



Bibek Prajapati
Creating Servlet With Intellij V9


22


The IDE will
look as following:


Fig 5
-
7: Screenshot of editor opening assembly de
scriptor of web.xml file

In servlet mapping section, select “+” sign to add new servlet mapping. Provide a URL pattern
(/helloworld). While providing URL Pattern, we must not forget to provide the initial “/”. Select
the servlet (HelloServlet).


Bibek Prajapati
Creating Servlet With Intellij V9


23


After th
e configuration is done, run the configuration or press Shift+10. The following page will
be displayed in the browser:


Fig 5
-
8: Running HelloServlet in browser

Bibek Prajapati
Creating Servlet With Intellij V9


24


6.

Running Simple JSP on Intellij V9

Select Web folder in the project explorer. The web folder c
ontains index.jsp file. To add another
jsp page, select and right click on web folder in project explorer. Then select New in the
popup

menu and then select jsp as shown in the figure below:


Fig 6
-
1: Creating New jsp page

Bibek Prajapati
Creating Servlet With Intellij V9


25


After selecting Jsp, new Jsp Dia
log box opens where the Jsp file name is entered as shown in the
following diagram:


Fig 6
-
2: New Jsp Dialog Box

Select ok after providing name for the Jsp file. Then new jsp file named demo will be created
inside the web folder. Double click demo.jsp fil
e to open it. Then this jsp file can be modified or
new content can be added to this file as shown in the figure below:


Fig 6
-
3: Adding Content in demo.jsp page

After adding content to the demo.jsp page, the next step is to edit the configuration so that

demo.jsp page can be shown in web browser. For that,
we have to select “Run” menu and then
select “Edit Configuration”.

Bibek Prajapati
Creating Servlet With Intellij V9


26


After selecting to edit configuration, we get following Run/Debug Configuration dialog
box:


Fig: 6
-
4: Run/Debug Configuration Dialog

Box where startup page has been changed to
demo.jsp.

We can add new configuration or modify the existing Tomcat Configuration. The only thing that
has to be changed is the startup page. Just select the startup page to be demo.jsp. So, if we have
startup p
age like “
http://localhost:8080/
helloworld” then replace the helloworld with demo.jsp as

http://localhost:8080/demo.jsp
”.

Select Apply and then Ok to save the configuration.

Bibek Prajapati
Creating Servlet With Intellij V9


27


Then run the program and you should get your web page in browser as:


Fig: 6
-
5:

Browser displaying demo.jsp page