This is a short introduction to help you set up Tomcat and deploy Java Server Pages (JSPs). For more information, please refer to Apache Tomcat.
The first two sections help you setup a container engine (Tomcat) that supports JSPs. If you already set up Tomcat, please proceed to Working with JSP
(Please tell me if you find any errors or omissions in the document —Upsorn Praphamontripong, 11-January-2018)
apache-tomcat
zip file
C:\apache-tomcat
Applications/apache-tomcat
C:\apache-tomcat\bin\startup.bat
, orC:\apache-tomcat\bin\
then
execute startup.bat
Applications/apache-tomcat/bin/
then
execute ./startup.sh
http://localhost:8080/
C:\apache-tomcat\bin\shutdown.bat
, orC:\apache-tomcat\bin\
then
execute shutdown.bat
Applications/apache-tomcat/bin/
then
execute ./shutdown.sh
Servers
tab.
Servers
tab does not appear on the screen, open the view using
menu bar Window > Show View > Servers
Servers
tab, right click and select New > Server
Finish
Start
.java
file and
select Run As > Run on Server
,
Eclipse will prompt a window to start the server.
You can also choose to have the server starts automatically as you run the servlet.
Stop
.jsp
file)
sevlet-api.jar
in classpath.
Some IDEs automatically include a path to servlet-api.jar
while
some require that you set class path to servlet-api.jar
Create a new Java EE Web Project
or
using the menu bar File > New > Dynamic Web Project
Next
and Next
to configure Web Module
.
Note: You may choose to click Finish
and
generate web.xml
to configure Web Module
later.
Web Module
window, select
Generate web.xml deployment descriptor
.
Note: If you choose to manually create web.xml
later, you may leave the checkbox blank.
WebContent
folder, select
New > JSP file
to create a new .jsp
file.
File name
.
If you choose to have a package for your JSPs, right click on the WebContent
,
select New > Folder
, enter the folder (or package) name in the
Folder name
.
New > JSP file
to create a new .jsp
file.
servlet-api.jar
to your project.
servlet-api.jar
to your project
Properties
, or
Project > Properties
Java Build Path
Libraries
tabAdd External JARs...
button
servlet-api.jar
.
Note: By default, servlet-api.jar
is part of Apache Tomcat and
is usually in
C:\apache-tomcat\lib
Applications/apache-tomcat/lib
servlet-api.jar
Open
button.
servlet-api.jar
should appear under
JARs and class folders on the build path panel
<% X ... %>
where X is one of the following:
package
command in your code
(unlike a Java program where you explicitly add package package-name;
at the beginning of the file).
Tomcat will add that for you when it converts the JSP to a Java servlet.
.java
file) and
then compiles the Java servlet (i.e., resulting in a .class
file).
We do not need to compile a JSP file.
C:\apache-tomcat\webapps
Applications/apache-tomcat/webapps
/tomcat/webapps/project-name/
.
You may create a directory the way you normally do, using the features available on your machine.
Alternatively, if you use a terminal,
mkdir /tomcat/webapps/project-name/
file-name.jsp
) into that directory: cp file-name.jsp /tomcat/webapps/project-name/
cd
ln -s /tomcat/webapps/project-name/
name-of-link
cs4640
:
Directory | Contains |
\cs4640 |
This is the root directory of the web application. All JSP and HTML files are stored here. |
\cs4640\WEB-INF\classes |
This directory is where servlet and utility classes are located. |
\cs4640\WEB-INF |
This directory contains all resources related to the application
that are not in the document root of the application.
This is where your web application deployment descriptor (web.xml ) is located.
Note that the WEB-INF directory is not part of the public document.
No files contained in this directory can be served directly
to a client. |
\cs4640\WEB-INF\lib |
This directory contains Java Archive (JAR) files that the web application depends upon. For example, this is where you would place a JAR file that contains a JDBC driver. |
http://localhost:8080/project-name/file-name.jsp
.jsp
file), select Run As > Run on Server
.
Eclipse will access the servlet using a virtual browser.
X.jsp
, typically the servlet Java file (X_jsp.java
)
and servlet class file (X_jsp.class
) are in
/tomcat/work/Catalina/localhost/project-name/org/apache/jsp/
X_jsp.java
and X_jsp.class
may not be in the default Catalina directory.
You may use a terminal to find the file:
find . | grep "X_jsp.java"
/tomcat/webapps/project-name/WEB-INF/classes/
package-name
under the classes/
directory.
Assume your package name is examples
,
your bean class is named mybean.java
, and it is in your home directory.
Modify mybean.java
to add the statement package examples;
in the first line.
Then create the package:
cd /tomcat/webapps/project-name/WEB-INF/classes
mkdir examples
cp ~/mybean.class examples
mybean
",
and the JSP is called "myUseBean.jsp
":
myUseBean.jsp
:
<%@ page import="examples.mybean" %>
mybean
named Val
, set the scope (based on the nature of your web application):
<jsp:useBean id="Val" class="examples.mybean" scope="page" />