First Simple Interactive JSP example

Similar documents
Advantage of JSP over Servlet

Unit 5 JSP (Java Server Pages)

Experiment No: Group B_2

A JavaBean is a class file that stores Java code for a JSP

CE212 Web Application Programming Part 3

Java E-Commerce Martin Cooke,

Servlets1. What are Servlets? Where are they? Their job. Servlet container. Only Http?

PES INSTITUTE OF TECHNOLOGY, SOUTH CAMPUS DEPARTMENT OF MCA INTERNAL TEST (SCHEME AND SOLUTION) II

Java Server Pages JSP

COMP9321 Web Application Engineering

Chapter Two Bonus Lesson: JavaDoc

Y ou must have access to a JSP-compatible Web server

Java Enterprise Edition. Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 1

JSP - SYNTAX. Any text, HTML tags, or JSP elements you write must be outside the scriptlet. Following is the simple and first example for JSP:

Java4340r: Review. R.G. (Dick) Baldwin. 1 Table of Contents. 2 Preface

JSP MOCK TEST JSP MOCK TEST III

FINALTERM EXAMINATION Spring 2009 CS506- Web Design and Development Solved by Tahseen Anwar

COMP9321 Web Application Engineering

Configuring Tomcat for a Web Application

Module 3 Web Component

JSP. Basic Elements. For a Tutorial, see:

JavaServer Pages. What is JavaServer Pages?

directive attribute1= value1 attribute2= value2... attributen= valuen %>

Ch04 JavaServer Pages (JSP)

About the Authors. Who Should Read This Book. How This Book Is Organized

Life Without NetBeans

One application has servlet context(s).

Internet Technologies. Lab Introduction

Servlet Fudamentals. Celsina Bignoli

Chapter 15 Java Server Pages (JSP)

UNIT -5. Java Server Page

Objec&ves. Servlets Review JSPs Web Applica&on Organiza&on Version Control. May 3, 2016 Sprenkle - CS335 1

CGI Programming. What is "CGI"?

CS506 Web Design & Development Final Term Solved MCQs with Reference

servlets and Java JSP murach s (Chapter 4) TRAINING & REFERENCE Mike Murach & Associates Andrea Steelman Joel Murach

6- JSP pages. Juan M. Gimeno, Josep M. Ribó. January, 2008

CIS 3308 Logon Homework

Web Focused Programming With PHP

Database Applications Recitation 6. Project 3: CMUQFlix CMUQ s Movies Recommendation System

SSC - Web development Model-View-Controller for Java Servlet

Command-driven, event-driven, and web-based software

Module 4: SERVLET and JSP

Lab 4: Basic PHP Tutorial, Part 2

To follow the Deitel publishing program, sign-up now for the DEITEL BUZZ ON-

Database Systems Lab. 11. JSP I 충남대학교컴퓨터공학과 데이타베이스시스템연구실

Chapter 10 Servlets and Java Server Pages

Forms, CGI. Objectives

welcome to BOILERCAMP HOW TO WEB DEV

Servlets by Example. Joe Howse 7 June 2011

Distributed Databases and Remote Access to a Database

CS 268 Lab 6 Eclipse Test Server and JSPs

EMC White Paper. BPS http Listener. Installing and Configuring

Session 16. JavaScript Part 1. Reading

Writing Servlets and JSPs p. 1 Writing a Servlet p. 1 Writing a JSP p. 7 Compiling a Servlet p. 10 Packaging Servlets and JSPs p.

Lab 1 - Introduction to Angular

SECTION I: JAVA SERVLETS AND JAVA SERVER PAGES

COMP9321 Web Application Engineering

Java Servlets. Preparing your System

SESM Components and Techniques

Stateless Session Bean

Introduction. This course Software Architecture with Java will discuss the following topics:

JSP (Java Server Page)

JSP source code runs on the web server via JSP Servlet Engine. JSP files are HTML files with special Tags

S imilar to JavaBeans, custom tags provide a way for

COMP519 Practical 5 JavaScript (1)

Developing Ajax Applications using EWD and Python. Tutorial: Part 2

HTML Advanced Portlets. Your Guides: Ben Rimmasch, Rahul Agrawal

15-415: Database Applications Project 2. CMUQFlix - CMUQ s Movie Recommendation System

Group A: Assignment No 2

HTML Forms. By Jaroslav Mohapl

Introduction. Literature: Steelman & Murach, Murach s Java Servlets and JSP. Mike Murach & Associates Inc, 2003

Server-Side Web Programming: Python (Part 1) Copyright 2017 by Robert M. Dondero, Ph.D. Princeton University

HTML 5 Form Processing

Session 6. JavaScript Part 1. Reading

Servlet and JSP Review

CS 112 Introduction to Programming

Introduction to Java Server Pages. Enabling Technologies - Plug-ins Scripted Pages

Java Server Page (JSP)

<form>. input elements. </form>

Unit 4 Java Server Pages

web.py Tutorial Tom Kelliher, CS 317 This tutorial is the tutorial from the web.py web site, with a few revisions for our local environment.

CP215 Application Design

By completing this practical, the students will learn how to accomplish the following tasks:

Exercise. (1) Which of the following can not be used as the scope when using a JavaBean with JSP? a. application b. session c. request d.

SSC - Web applications and development Introduction and Java Servlet (I)

JavaServer Pages (JSP)

Liferay Themes: Customizing Liferay s Look & Feel

Using the VisualAge for Java WebSphere Test Environment

c360 Web Connect Configuration Guide Microsoft Dynamics CRM 2011 compatible c360 Solutions, Inc. c360 Solutions

PRODUCT DOCUMENTATION. Installing and Implementing Enterprise Contact Center Chat RELEASE 5.1

SECTION II: JAVA SERVLETS

The Basic Web Server CGI. CGI: Illustration. Web based Applications, Tomcat and Servlets - Lab 3 - CMPUT 391 Database Management Systems 4

Running SQL in Java and PHP

A QUICK GUIDE TO PROGRAMMING FOR THE WEB. ssh (then type your UBIT password when prompted)

Forms, CGI. Cristian Bogdan 2D2052 / 2D1335 F5 1

Web Programming. Based on Notes by D. Hollinger Also Java Network Programming and Distributed Computing, Chs.. 9,10 Also Online Java Tutorial, Sun.

Principles and Techniques of DBMS 6 JSP & Servlet

Information Retrieval CS Lecture 13. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Author - Ashfaque Ahmed

CSC 8205 Advanced Java

Transcription:

Let s look at our first simple interactive JSP example named hellojsp.jsp. In his Hello User example, the HTML page takes a user name from a HTML form and sends a request to a JSP page, and JSP page generates a dynamic HTML page based on the data which comes with the request Basically, this JSP page takes a user name input from a Web client and generates a dynamic page to greet the user. The request may come from a Web form page request or from a query string following an URL address of this JSP page. First, examine the Web form HTML file named index.html placed in the JSP directory which is this Web application ROOT directory under webapps.

<html> <head> <title>demo1</title> </head> <body> <h3>please enter the user name :</h3><p> <form action="/jsp/hellojsp.jsp"> UserName : <input type="text" name="username"><br><br> <input type="submit" value="submit"> </form> </body> </html> Since index.html is the default html name fortomcat that you even don t need to specify the index.html in the URL address of the browser.

This HTML takes a string of a user name from the HTML form and submits a request to hellojsp.jsp as specified in the action attribute of the HTML form. For example, a user types SPSU in the form and pushes the Submit button.

The page hellojsp.jsp is placed in the same directory as index.html and WEB-INF. All HTML tags can be used in a JSP page since JSP is an extension of HTML. A JSP file can be placed anywhere an HTML file can be placed. <%@ page import="java.util.*" info = This is a simplest JSP with Java code embedded contenttype= text/html %> <%! Date today; %> <html> <%-- This is a Simplest JSP (comment from JSP tag -- %> <body> <!-- This is a simplest JSP (Comment from HTML tag --> Hello <%= request.getparameter("username") %>! <br> <% today = new Date(); %> <%= today %> </body> </html>

The first line is a JSP is a JSP directive element tag (<%@... %>) which directs the JSP engine to set the page structure. It will not result in any Servlet code, just like the import directive in Java or #include directive in C. This directive element tells the JSP engine to import all classes in the java.util package to allow use of the Date class and tells JSP engine that content type of this page is text/html not text/xml or some other Multipurpose Internet Mail Extension(MIME) type.

The second line is a JSP declaration element <%!... %> which tells the JSP engine to insert the enclosed java code into the generated Servlet java source code somewhere outside of any method. Variable declarations or method declarations may be placed here. A Date class variable (class reference) is declared here. Of course you can have an in-line variable declaration with its assignment statement instead of the wo separate scripting elements in above JSP example.

You see two different comments here. <%--... %> is a JSP comment scripting element which stays on the server while <!--... --> is a regular HTML comment which is sent back to the client. The client can view the HTML comment with the view-source option of Internet browsers. Everything not within JSP element tags will be sent to the client browser as literal data. The word Hello is displayed on the client browser screen followed by the string returned from the next JSP expression scripting element and the!.

The expression scripting element <%=... %> will display values of the enclosed expression in string format. In this JSP example, the hellojsp.jsp gets the parameter input string by the getparameter() method of HttpServletRequest object. The input string either comes from the input text field of the request form or the URL query string of this JSP address. The username of the getparameter() method matches the username text input component name in the HTML form.

The next JSP scripting element is scriptlet which is simply a Java Servlet code fragment. It is inserted into the service() method of the generated Servlet code. Here, JSP instantiates a new instance of Date class and the current date is displayed in the next expression scripting element.

In this JSP you have seen two types of JSP constructs: directive elements and scripting elements (including expression scripting, declaration scripting, scriptlets scripting). We will discuss another type constructor, the action tag element. soon.

The following screen shots show the directory structure of this JSP Web application on Tomcat. The index.html and hellojsp.jsp files are placed in the JSP subdirectory of webapps directory.

You can use the view option of the browser to view the source HTML code generated by JSP. You can see the HTML comments but you don t see the JSP comments.

We can also use JSP expression language of the JS P 2.0 to rewrite the hellojsp.jsp as follows. <%@ page iselignored = "false" %> <html> <body> Hello, ${param['username']}! </body> </html> where username is the parameter name defined in the HTML file.

The expression language makes data access much easier. You can use the expression ${param['username']} instead of <%= request.getparameter("username") %> to get the username parameter on the HTML form. From the simple JSP example above you know the JSP works. You can even incorporate the index.html into the hellojsp.jsp so that a single JSP page can display an HTML form for a user to interact with and then respond with the result to clients by itself.