Now that you have added
your Java Application directory, the following directory structure will have
been created for you automatically:
| Directory |
Contains |
|
onjava
|
This is the root directory of the web application. All
JSP and XHTML files are stored here. |
|
onjava/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 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. |
|
onjava/WEB-INF/classes
|
This directory is where servlet and utility classes are
located. |
|
onjava/WEB-INF/lib
|
This directory contains Java Archive files that the web
application depends upon. For example, this is where you would place a JAR
file that contained a JDBC driver. |
Begin to load your files. Your "web.xml" file should
be created when you add the "onjava" Java application.
It is located in the
/home/$username/www/webapps/onjava/WEB-INF directory.
Please add the contents in between the <web-app> tags, to the "web.xml" file as shown below:
web.xml code
<?xml
version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application
2.3//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.onjava.login</servlet-class>
</servlet>
</web-app>
Next,
we will create the html interface for the browser and put it into the
/home/$username/www/webapps/onjava/ directory.
login.jsp code
<html>
<head>
<title>OnJava Demo</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#66CCFF"
onLoad="document.loginForm.username.focus()">
<table width="500"
border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<table width="500"
border="0" cellspacing="0" cellpadding="0">
<form name="loginForm"
method="post" action="servlet/com.onjava.login">
<tr>
<td
width="401"><div align="right">User
Name: </div></td>
<td width="399"><input
type="text" name="username"></td>
</tr>
<tr>
<td
width="401"><div
align="right">Password: </div></td>
<td
width="399"><input type="password"
name="password"></td>
</tr>
<tr>
<td
width="401"> </td>
<td
width="399"><br><input type="Submit"
name="Submit"></td>
</tr>
</form>
</table>
</td>
</tr>
</table>
</body>
</html>
You should be able to access the "login.jsp" page as shown below with your browser.
http://$domainname/webapps/onjava/login.jsp
You will be prompted for a
username and password (any username and password would do for the purpose of this guide),
click the "Submit" button.
Then a "Welcome" page will display on your browser. To create this Welcome page, make a "welcome.jsp" file, with the contents shown below. This page is to be put in the same directory as the login.jsp file,
/home/$username/www/webapps/onjava/
If a database of usernames and passwords
existed, the database would be searched and a welcome page would return with
the correct person's name.
Welcome.jsp code:
<html>
<head>
<title>OnJava
Demo</title>
<meta
http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
</head>
<b>Welcome : <%= request.getAttribute("USER")
%>
</html>
Now it is time to Add and compile the script:
You must first create the following
new directories (These are created for the purpose of this Guide only and are not necessary for your own JSP scripts):
com
onjava
Next, please create the following
"login.java" file, into the
/home/$username/www/webapps/onjava/WEB-INF/classes/com/onjava directory.
login.java code:
package
com.onjava;
import
javax.servlet.*;
import
javax.servlet.http.*;
import
java.io.*;
import
java.util.*;
public
class login extends HttpServlet {
private
String target = "/welcome.jsp";
private
String getUser(String username, String password) {
// Just
return a static name
//
If this was reality, we would perform a SQL lookup
return
"Frodo";
}
public
void init(ServletConfig config)
throws
ServletException {
super.init(config);
}
public
void doGet(HttpServletRequest request,
HttpServletResponse
response)
throws
ServletException, IOException {
//
If it is a get request forward to doPost()
doPost(request,
response);
}
public
void doPost(HttpServletRequest request,
HttpServletResponse
response)
throws
ServletException, IOException {
//
Get the username from the request
String
username = request.getParameter("username");
//
Get the password from the request
String
password = request.getParameter("password");
String
user = getUser(username, password);
//
Add the fake user to the request
request.setAttribute("USER",
user);
//
Forward the request to the target named
ServletContext
context = getServletContext();
RequestDispatcher
dispatcher =
context.getRequestDispatcher(target);
dispatcher.forward(request,
response);
}
public
void destroy() {
}
}
After you load the above login.java file into the directory, you should compile from the same directory that the file is located, the syntax is as follows:
javac login.java
This will create a compiled file called:
login.class
THAT'S IT!
If you can access the above http://$domainname/webapps/onjava/login.jsp and get the welcome page as a result, then you were successful in this "Getting Started With JSP" guide.
IMPORTANT NOTES:
In order to eliminate the need to restart "Tomcat" when new Java applications are added,
Please add your ".war" file or servlet application, including any compiling of .java code
BEFORE using the Control Panel's "JSP Manager" interface to actually activate the application.
Please know that you may add stand alone JSP pages at any time without the
need to use the "JSP Manager" interface or restart Tomcat.
Under these conditions though, the stand alone JSP page must be placed in
the webapps directory.
Also, Moving a Java servlet from a non-JSP server to a JSP server requires rewriting of the
servlets. Servlets require different configurations once they are moved to a JSP server.
|