Académique Documents
Professionnel Documents
Culture Documents
The aim of this lab session is to develop simple JSP web applications that run on Tomcat web
server.
Equipments:
Two networked PCs with Red Hat Linux operating systems, logbook, and a USB memory stick for
saving your work.
Exercises 1:
1. Create a JSP file called date1.jsp, and save it in the /opt/tomcat/webapps/test directory.
<head><title>Date 1</title></head>
<body><%
// This is a scriptlet. Notice that the "date"
// variable we declare here is available in the
// embedded expression later on.
System.out.println( "Evaluating date now" );
java.util.Date date = new java.util.Date();
%>
Hello! The time is now <%= date %>
</body>
</html>
2. From the second computer, go to graphical desktop, open a web browser, and type in
following URL: http://xxx.xxx.xxx.xxx:8080/test/date1.jsp , where xxx.xxx.xxx.xxx is the
first computer’s IP address or domain name. Comment the results.
3. Create another JSP file called date2.jsp, and save it in the /opt/tomcat/webapps/test
directory.
<%@ page import=”java.util.*” %>
<%@ page import=”java.text.*” %>
<html>
<head><title>Date 2</title></head>
<body><%
Date date = new Date();
String today=DateFormat.getDateInstance().format(date);
%>
Today is:
<em> <%= today %> </em>
</body>
</html>
4. From the second computer, go to graphical desktop, open a web browser, and type in
following URL: http://xxx.xxx.xxx.xxx:8080/test/date2.jsp , where xxx.xxx.xxx.xxx is the
first computer’s IP address or domain name. Comment the results.
5. Create another JSP file called random.jsp, and save it in the /opt/tomcat/webapps/test
directory.
<%@ page language=”java” %>
<%@ page import=”java.util.Random” %>
<head><title>Random Number</title></head>
<body>
<%! Random rand = new Random(); %>
<%! public double getRandomNumber(){
return (rand.nextDouble()*1000);
} %>
<p>Here is a random number: <%=getRandomNumber() %></p>
<p>And another: <%=getRandomNumber() %></p>
<p>and one more: <%=getRandomNumber() %></p>
</body>
</html>
6. From the second computer, go to graphical desktop, open a web browser, and type in
following URL: http://xxx.xxx.xxx.xxx:8080/test/random.jsp , where xxx.xxx.xxx.xxx is
the first computer’s IP address or domain name. Comment the results.
7. Base on the above example, create a JSP web application called lottery.jsp, that can give
six lottery numbers each time when you load the page, it should also show the date and
time.
Exercises 2:
2. Create a JSP file called Login1.jsp, and save it in the /opt/tomcat/webapps/test directory.
<html>
<head><title>JSP Login Page</title></head>
<body>
3. From the second computer, go to graphical desktop, open a web browser, and type in
following URL: http://xxx.xxx.xxx.xxx:8080/test/Login1.html, where xxx.xxx.xxx.xxx is
the first computer’s IP address or domain name. Comment the results.
4. Modify the above two programs, so if login is successful it will display a welcome message
and the system time (welcome.jsp), and if failed it will display an error message
(error.html). Hint: use pageContext.forward(“welcome.jsp”) or
pageContext.forward(“error.html”) within the if else structure.
References:
1. http://java.sun.com
2. http://tomcat.apache.org/
3. http://www.linux-sxs.org/internet_serving/
4. http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/JSPIntro.html
5. http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Servlets.html
6. http://www.jsptut.com/
7. http://eent3.sbu.ac.uk/staff/xiaop
8. David Harms, “JSP, Servlets, and MySQL”, ISBN: 0764547879, M&T Book, 2001
(http://www.covecomm.com/java).
9. Barry Burd, JSP: JavaServer Pages, ISBN:0764535358, M & T Books, 2001
Questions: