JSP CSCI 201 Principles of Software Development

Similar documents
JavaScript CSCI 201 Principles of Software Development

COMP9321 Web Application Engineering

Copyright 2011 Sakun Sharma

COMP9321 Web Application Engineering

JavaServer Pages (JSP)

COMP9321 Web Application Engineering

Basic Principles of JSPs

JavaServer Pages. What is JavaServer Pages?

Demonstration of Servlet, JSP with Tomcat, JavaDB in NetBeans

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:

Unit 5 JSP (Java Server Pages)

Principles and Techniques of DBMS 6 JSP & Servlet

CSc31800: Internet Programming, CS-CCNY, Spring 2004 Jinzhong Niu May 9, JSPs 1

Servlet and JSP Review

CE212 Web Application Programming Part 3

Java E-Commerce Martin Cooke,

Java Server Pages JSP

Fast Track to Java EE 5 with Servlets, JSP & JDBC

PSD1B Advance Java Programming Unit : I-V. PSD1B- Advance Java Programming

JAVA 2 ENTERPRISE EDITION (J2EE)

Sun Sun Certified Web Component Developer for J2EE 5 Version 4.0

Unit 4. CRM - Web Marketing 4-1

JavaServer Pages. Juan Cruz Kevin Hessels Ian Moon

One application has servlet context(s).

UNIT -5. Java Server Page

Advance Java. Configuring and Getting Servlet Init Parameters per servlet

This course is intended for Java programmers who wish to write programs using many of the advanced Java features.

Creating a Simple Web Application Using a MySQL Database

Fast Track to Java EE

A.1 JSP A.2 JSP JSP JSP. MyDate.jsp page contenttype="text/html; charset=windows-31j" import="java.util.calendar" %>

CS 112 Introduction to Programming

ADVANCED JAVA COURSE CURRICULUM

Chapter 15 Java Server Pages (JSP)

JSP. Basic Elements. For a Tutorial, see:

Server-side Web Programming

01KPS BF Progettazione di applicazioni web

Introduction to JSP and Servlets Training 5-days

Advanced Web Systems 3- Portlet and JSP-JSTL. A. Venturini

Adv. Web Technology 3) Java Server Pages

Actual4Test. Actual4test - actual test exam dumps-pass for IT exams

1. Introduction. 2. Life Cycle Why JSP is preferred over Servlets? 2.1. Translation. Java Server Pages (JSP) THETOPPERSWAY.

/smlcodes /smlcodes /smlcodes JSP. Java Server Pages. Small Codes. Programming Simplified. A SmlCodes.Com Small presentation

Advantage of JSP over Servlet

First Simple Interactive JSP example

Oracle JDeveloper 10g: Empowering J2EE Development HARSHAD OAK

Chapter 10 Servlets and Java Server Pages

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.

Java Server Pages. Copyright , Xiaoping Jia. 7-01/54

Unit 4 Java Server Pages

STRUTS 2 - VALIDATIONS FRAMEWORK

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

DEZVOLTAREA APLICATIILOR WEB LAB 4. Lect. Univ. Dr. Mihai Stancu

CS506 Web Design & Development Final Term Solved MCQs with Reference

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

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

Databases CSCI 201 Principles of Software Development

AIM. 10 September

SQL CSCI 201 Principles of Software Development

Servlets by Example. Joe Howse 7 June 2011

SNS COLLEGE OF ENGINEERING, Coimbatore

Java.. servlets and. murach's TRAINING & REFERENCE 2ND EDITION. Joel Murach Andrea Steelman. IlB MIKE MURACH & ASSOCIATES, INC.

2. Follow the installation directions and install the server on ccc. 3. We will call the root of your installation as $TOMCAT_DIR

JAVA MICROSERVICES. Java Language Environment. Java Set Up. Java Fundamentals. Packages. Operations

Session 12. JSP Tag Library (JSTL) Reading & Reference

DVS WEB INFOTECH DEVELOPMENT TRAINING RESEARCH CENTER

Computer Science E-1. Understanding Computers and the Internet. Lecture 10: Website Development Wednesday, 29 November 2006

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

ユーザー入力およびユーザーに 出力処理入門. Ivan Tanev

Watch Core Java and Advanced Java Demo Video Here:

JSP: JavaServer Pages

Creating a New Project with Struts 2

JSP Scripting Elements

BCSL057: WEB PROGRAMMING LAB

Contents at a Glance

STRUTS 2 - HELLO WORLD EXAMPLE

Java TM. JavaServer Faces. Jaroslav Porubän 2008

sessionx Desarrollo de Aplicaciones en Red EL (2) EL (1) Implicit objects in EL Literals José Rafael Rojano Cáceres

Session 21. Expression Languages. Reading. Java EE 7 Chapter 9 in the Tutorial. Session 21 Expression Languages 11/7/ Robert Kelly,

Oracle EXAM - 1Z Java Enterprise Edition 5 Web Component Developer Certified Professional Exam. Buy Full Product

LTBP INDUSTRIAL TRAINING INSTITUTE

Web applications and JSP. Carl Nettelblad

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

Session 18. JSP Access to an XML Document XPath. Reading

LTBP INDUSTRIAL TRAINING INSTITUTE

Oracle 10g: Build J2EE Applications

Module 5 Developing with JavaServer Pages Technology

Vendor: SUN. Exam Code: Exam Name: Sun Certified Web Component Developer for J2EE 5. Version: Demo

Document for Consuming Web-Service In.NET & JAVA

Islamic University of Gaza Faculty of Engineering Department of Computer Engineering ECOM Advanced Internet Technology Lab.

Chapter 2 How to structure a web application with the MVC pattern

SECTION I: JAVA SERVLETS AND JAVA SERVER PAGES

JSP: Servlets Turned Inside Out

COMP201 Java Programming

Welcome To PhillyJUG. 6:30-7:00 pm - Network, eat, find a seat 7:00-7:15 pm - Brief announcements 7:15-8:30 pm - Tom Janofsky's presentation

Experiment No: Group B_2

Ch04 JavaServer Pages (JSP)

AJP. CHAPTER 5: SERVLET -20 marks

NAVAL POSTGRADUATE SCHOOL THESIS

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Transcription:

JSP CSCI 201 Principles of Software Development Jeffrey Miller, Ph.D. jeffrey.miller@usc.edu

Outline JSP Program USC CSCI 201L

JSP 3-Tier Architecture Client Server Web/Application Server Database USC CSCI 201L 3/18

JSP Because embedding HTML, CSS, and JavaScript as the output of a servlet is quite tedious, Java has a way to instead embed Java in your HTML file We can do this through a Java Server Page (JSP) JSPs get converted to servlets the first time they are accessed, compiled, and made available dynamically by the application server A JSP will be a file that looks like client-side code with embedded Java within special tags Declaration <%! %> Directive <%@ %> Expression <%= %> Scriptlet <% %> JSTL explained in a later slide USC CSCI 201L 4/18

My First JSP 1 <%@ page language="java" contenttype="text/html; charset=iso-8859-1" 2 pageencoding="iso-8859-1"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <title>my First JSP</title> 7 </head> 8 <body> 9 <h1>hello CSCI 201</h1> 10 <% System.out.println("hello world"); %> 11 </body> 12 </html> USC CSCI 201L 5/18

Generated Servlet (partial service method) 1 try { 2 response.setcontenttype("text/html; charset=iso-8859-1"); 3 pagecontext = _jspxfactory.getpagecontext(this, request, response, null, 4 true, 8192, true); 5 _jspx_page_context = pagecontext; 6 application = pagecontext.getservletcontext(); 7 config = pagecontext.getservletconfig(); 8 session = pagecontext.getsession(); 9 out = pagecontext.getout(); 10 _jspx_out = out; 11 out.write("\r\n"); 12 out.write("<!doctype html>\r\n"); 13 out.write("<html>\r\n"); 14 out.write(" <head>\r\n"); 15 out.write(" <title>my First JSP</title>\r\n"); 16 out.write(" </head>\r\n"); 17 out.write(" <body>\r\n"); 18 out.write(" <h1>hello CSCI 201</h1>\r\n"); 19 out.write(" "); 20 System.out.println("hello world"); 21 out.write("\r\n"); 22 out.write(" </body>\r\n"); 23 out.write("</html>\r\n"); Location of compiled JSP C:\Users\jeffadmin\workspace\.metadata\.plugins\ org.eclipse.wst.server.core\tmp1\ work\catalina\localhost\ TestWeb2\org\apache\jsp USC CSCI 201L 6/18

My Second JSP 1 <%@ page language="java" contenttype="text/html; charset=iso-8859-1" 2 pageencoding="iso-8859-1"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <title>loops</title> 7 </head> 8 <body> 9 <h1>hello CSCI 201</h1> 10 <% 11 for (int i=0; i < 4; i++) { 12 %> 13 <font size="+<%= i %>">Font Size of +<%= i %></font><br /> 14 <% 15 } 16 %> 17 </body> 18 </html> USC CSCI 201L 7/18

Color JSP 1 <%@ page language="java" 2 <!DOCTYPE html> 3 <html> 26 27 28 <table> <tr> <th>red</th> 4 <head> 28 <th>green</th> 5 <title>color JSP</title> 29 <th>blue</th> 6 <%! 30 <th>color</th> 7 String getcolor(int r, int g, int b) { 31 </tr> 8 String color = ""; 32 <% 9 color += makehex(r); 33 for (int red=0; red < 255; red+=50) { 10 color += makehex(g); 34 for (int green=0; green < 255; green+=50) { 11 color += makehex(b); 35 for (int blue=0; blue < 255; blue+=50) { 12 return color; 36 %> 13 } 37 <tr> 14 15 String makehex(int c) { 38 39 <td><%= red %></td> <td><%= green %></td> 16 String hexstring = Integer.toHexString(c); 40 <td><%= blue %></td> 17 if (hexstring.length() == 1) { 41 <% 18 hexstring = "0" + hexstring; 42 String color = getcolor(red, green, blue); 19 } 43 %> 20 return hexstring; 44 <td style="background-color:#<%= color %>;"> </td> 21 } 45 </tr> 22 %> 46 <% 23 </head> 47 } 24 <body> 48 } 25 <h1>color Table</h1> 49 } 50 %> 51 </table> 52 </body> 53 </html> USC CSCI 201L 8/18

Color JSP Generated HTML 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>color JSP</title> 5 </head> 6 <body> 7 <h1>color Table</h1> 8 <table> 9 <tr><th>red</th><th>green</th><th>blue</th><th>color</th></tr> 10 <tr> 11 <td>0</td> 12 <td>0</td> 13 <td>0</td> 14 <td style="background-color:#000000;"> </td> 15 </tr> 16 <tr> 17 <td>0</td> 18 <td>0</td> 19 <td>50</td> 20 <td style="background-color:#000032;"> </td> 21 </tr> 22 <tr> 23 <td>0</td> 24 <td>0</td> 25 <td>100</td> 26 <td style="background-color:#000064;"> </td> 27 </tr>... 1743 </table> 1744 </body> 1745 </html> USC CSCI 201L 9/18

JSPs and HTML Forms JSPs (which get converted to servlets) can be used to process the data submitted from an HTML form through the request variable JSPs have a number of implicit variables HttpServletRequest request HttpServletResponse response PrintWriter out HttpSession session ServletContext application ServletConfig config JSPWriter pagecontext HttpServlet this Exception exception USC CSCI 201L 10/18

JSP Form Example 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>sample Form</title> 5 </head> 6 <body> 7 <form name="myform" method="get" action="formservlet"> 8 First Name <input type="text" name="fname" /><br /> 9 Last Name <input type="text" name="lname" /><br /> 10 <input type="submit" name="submit" value="submit" /> 11 </form> 12 </body> 13 </html> USC CSCI 201L 11/18

JSP Form Example 1 <%@ page language="java" contenttype="text/html; charset=iso-8859-1" 2 pageencoding="iso-8859-1"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <title>form Data Processing</title> 7 </head> 8 <body> 9 <% 10 String fname = request.getparameter("fname"); 11 String lname = request.getparameter("lname"); 12 %> 13 <h1>submitted Data</h1> 14 First Name:<strong><%= fname %></strong><br /> 15 Last Name:<strong><%= lname %></strong> 16 </body> 17 </html> USC CSCI 201L 12/18

JSTL The Java Server Pages Standard Tag Library (JSTL) is a collection of useful JSP tags that have core functionality for many JSP applications The idea with JSTL was to keep front-end programmers from having to learn Java while allowing them to write code with similar functionality There are five tag library groups in JSTL Core tags - <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> Formatting tags - <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> SQL tags - <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %> XML tags - <%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %> JSTL functions - <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> To use JSTL, you must place jstl-1.2.jar in the lib directory of your Tomcat installation, then restart Tomcat USC CSCI 201L 13/18

JSTL Example 1 <%@ page language="java" contenttype="text/html; charset=iso-8859-1" 2 pageencoding="iso-8859-1"%> 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <title>form Data Processing</title> 8 </head> 9 <body> 10 <c:set var="fname" value="${param.fname}" /> 11 <c:set var="lname" value="${param.lname}" /> 12 <h1>submitted Data</h1> 13 First Name:<strong><c:out value="${fname}" /></strong><br /> 14 Last Name:<strong><c:out value="${lname}" /></strong> 15 </body> 16 </html> USC CSCI 201L 14/18

JSP Scope Different variables can have different scope in JSPs There are four different scopes in JSPs Application Session Page Request The following variable would remain in scope as long as the session is valid <c:set var="fname" value="${param.fname}" scope="session /> USC CSCI 201L 15/18

More JSP For more information on JSPs Go to http://docs.oracle.com/javaee/5/tutorial/doc/bnagy.html Go through one of the many JSP tutorials online For more information on JSTL Go to http://docs.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/ Go to https://jstl.java.net/ 16/18

Outline JSP Program USC CSCI 201L

Program Modify the program you created during the Servlet lecture to have the HTML form submit to a JSP instead. Program USC CSCI 201L 18/18