Vous êtes sur la page 1sur 3

Faculty of ESBE Internet Protocols & Technologies

Lab 2: JSP Web Applications

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.

Dr. Perry XIAO Copyright © 2000-2009, London South Bank University 1


Faculty of ESBE Internet Protocols & Technologies

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:

1. Create a HTML file called Login1.html, and save it in the /opt/tomcat/webapps/test


directory.
<html>
<head><title>Login Page</title></head>
<body>
<form method=post action="Login1.jsp">
<table>
<tr><td>Login name:</td>
<td><input type=text name=j_username sixe=20></td></tr>
<tr><td>Password :</td>
<td><input type=password name=j_password sixe=20></td></tr>
</table>
<p><input type=submit value=Login>
</form>
</body>
</html>

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>

Dr. Perry XIAO Copyright © 2000-2009, London South Bank University 2


Faculty of ESBE Internet Protocols & Technologies

<% String uname = request.getParameter("j_username");


String upass = request.getParameter("j_password");
%>
<%! public String login(String uname, String upass){
String t="";
if (uname.compareTo("xiaop")==0 && upass.compareTo("letmein")==0){
t="Successful!";
}
else{
t="Failed!";
}
return t;
} %>
<p>Login <%= login(uname, upass) %></p>
</body>
</html>

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:

1. What are the main differences between JSP and Servlet?


2. What is the Container in the context of JSP/Servlet technologies?
3. How to declare variables in JSP?
4. How to pass information between a HTML file and a JSP file?

Dr. Perry XIAO Copyright © 2000-2009, London South Bank University 3

Vous aimerez peut-être aussi