Capital University of Science and Technology Islamabad Department of Computing Faculty of Computer Science
Capital University of Science and Technology Islamabad Department of Computing Faculty of Computer Science
Capital University of Science and Technology Islamabad Department of Computing Faculty of Computer Science
1
Lab 05: Java Server Pages- Part A
Table of Contents
1. INTRODUCTION 3
2. RELEVANT LECTURE READING 3
3. ACTIVITY TIME BOXING 3
4. OBJECTIVE OF THE EXPERIMENT 3
5. CONCEPT MAP 4
5.1. Introduction to JSP 4
5.2. In the end a JSP becomes a Servlet 4
5.3. Why JSP is preferred over servlets? 4
5.4. Advantage of JSP 4
5.5. JSP Scripting Element 4
5.6. Five different types of scripting elements 5
5.7. JSP Comment 5
6. HOMEWORK BEFORE LAB 6
6.1. Home Work 1 6
6.2. Home Work 2 6
7. TOOLS AND PROCEDURE. 7
7.1. Tools 7
7.2. Setting up to use IntelliJ Idea and Tomcat Server. 7
7.3. Installing PostgreSql 7
8. WALKTHROUGH TASK. 7
8.1. Walkthrough task 1 7
8.2. Walkthrough task 2 7
9. PRACTICE TASKS 9
9.1. Practice Task 1 9
10. EVALUATION TASK (UNSEEN) 9
11. FURTHER READING 9
2
Lab 05: Java Server Pages- Part A
1. Introduction
Java Server Pages (JSP) is a server-side programming technology that enables the creation
of dynamic, platform-independent method for building Web-based applications. JSP have
access to the entire family of Java APIs, including the JDBC API to access enterprise
databases.
This Lab will teach you how to use Java Server Pages to develop your web applications in
simple and easy steps.
Task
Activity Name Activity time Total Time
No.
6.2 Setting up to use HTML 10 min 10 min
6.3 Walkthrough Tasks 65 min 65min
As per time specified for
7 Practice Tasks 45 min
each task
3
Lab 05: Java Server Pages- Part A
5. Concept Map
4
Lab 05: Java Server Pages- Part A
Example:
<html>
<head>
<title>My First JSP Page</title>
</head>
<%
int count = 0;
%>
<body>
Page Count is <% out.println(++count); %>
</body>
Just to experiment, try removing the <% %> scriplet tag from the above code and run it as
JSP. You will see that everything is printed as it is on the browser, because without the
scriplet tag, everything is considered plain HTML code.
JSP Comment is used when you are creating a JSP page and want to put in comments about
what you are doing. JSP comments are only seen in the JSP page. These comments are not
included in servlet source code during translation phase, nor do they appear in the HTTP
response. Syntax of JSP comment is as follows:
5
Lab 05: Java Server Pages- Part A
From now on it is assumed that you know how to configure environment, create project
and manage database.
For help revise Lab02 and Lab03.
When the user clicks on the register button, redirect to a new page named as display.jsp
that displays all the values on the web page that user entered in the text fields.
6
Lab 05: Java Server Pages- Part A
8. Walkthrough task.
8.1. Walkthrough task 1
To create project and set environment and configure IDEA, refer to walkthrough task 1 of
Lab02.
In order to create database refer to walkthrough task of Lab03.
<html>
<head>
<title></title>
</head>
<body>
<form action="show.jsp" method="post">
<div>
<table>
<tr>
<td>Name :</td>
<td><input type="text" name="name"/><br><br></td>
</tr>
<tr>
<td> Father Name :</td>
<td><input type="text" name="father"/><br><br></td>
</tr>
<tr>
<td>Registration Number :</td>
<td><input type="text" name="reg"/><br><br></td>
</tr>
<tr>
<td><input type="submit" value="Show" name="buttonshow"/></td>
</tr>
</table>
</div>
</form>
</body>
</html>
7
Lab 05: Java Server Pages- Part A
Understand the above code carefully that how the form is created using tables. Also the
form tag is having two attributes action=”show.jsp” and method=”post”.
After that Create a new page named as show.jsp and type the following code into it.
<%
String user = request.getParameter("name");
String father = request.getParameter("father");
String reg = request.getParameter("reg");
PrintWriter pw=response.getWriter();
pw.write("<html>\n" +
"<head>\n" +
" <title></title>\n" +
"</head>\n" +
"<body><br>" +
"Student name is : "+user+"<br>"+
"Father name is : "+father+"<br>"+
"Registration number is : "+reg+"<br>"+
"</body>\n" +
"</html>");
%>
When the user clicks show button, control is directed to a new page where all the values
are extracted from the request object and then displayed on the web page using
PrintWriter.
8
Lab 05: Java Server Pages- Part A
9. Practice Tasks