Chengyu Sun California State University, Los Angeles

Slides:



Advertisements
Similar presentations
Java Server Pages (JSP)
Advertisements

 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.
28/1/2001 Seminar in Databases in the Internet Environment Introduction to J ava S erver P ages technology by Naomi Chen.
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
CS320 Web and Internet Programming JSP Scripting Elements and Page Directives Chengyu Sun California State University, Los Angeles.
JSP Java Server Pages Reference:
Java Server Pages Russell Beale. What are Java Server Pages? Separates content from presentation Good to use when lots of HTML to be presented to user,
1 CS6320 – JSP L. Grewe 2 Java Server Pages Servlets require you to write out entire page delivered with print statements Servlets require you to write.
Three types of scripting elements: 1.Expressions 2.Scriptlets 3.Declarations Scripting elements.
UNIT-V The MVC architecture and Struts Framework.
Java Server Pages (JSP) Presented by: Ananth Prasad & Alex Ivanov May 10, 2001.
Overview of JSP Technology. The need of JSP With servlets, it is easy to – Read form data – Read HTTP request headers – Set HTTP status codes and response.
Netbeans – jsp.zip Introduction to JSP Netbeans – jsp.zip.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
Chapter 7 Java Server Pages. Objectives Explain how the separation of concerns principle applies to JSP Describe the operation and life-cycle of a JSP.
Java Server Pages Lecture July Java Server Pages Java Server Pages (JSPs) provide a way to separate the generation of dynamic content (java)
JAVA SERVER PAGES. 2 SERVLETS The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
Lecturer: Prof. Piero Fraternali, Teaching Assistant: Alessandro Bozzon, Advanced Web Technologies: Struts–
JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA James Faeldon CS 119 Enterprise Systems Programming.
Slides © Marty Hall, book © Sun Microsystems Press 1 JSP Scripting Elements Core Servlets & JSP book:
Introduction to JavaServer Pages. 2 JSP and Servlet Limitations of servlet  It’s inaccessible to non-programmers JSP is a complement to servlet  focuses.
CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.
Chapter 2 An Overview of Servlet and JSP Technology.
SE-2840 Dr. Mark L. Hornick 1 Java Server Pages. HTML/JSPs are intended to be used as the views in an MVC- based web application Model – represents an.
Jsp (Java Server Page) Is a server side program.
CS-4220 Dr. Mark L. Hornick 1 Java Server Pages. HTML/JSPs are intended to be used as the views in an MVC- based web application Model – represents an.
Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering
Chapter 11 Invoking Java Code with JSP Scripting Elements.
JSP Pages. What and Why of JSP? JSP = Java code imbedded in HTML or XML –Static portion of the page is HTML –Dynamic portion is Java Easy way to develop.
CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles.
Chapter 3 JSP Overview. The Problem with Servlets processing the request and generating the response are both handled by a single servlet class Java programming.
STRUCTURE OF JSP PRESENTED BY: SIDDHARTHA SINGH ( ) SOMYA SHRIVASTAV ( ) SONAM JINDAL ( )
Chapter 6 Chapter 6 Server Side Programming (JSP) Part 1 1 (IS 203) WebProgramming (IS 203) Web Programming.
CS320 Web and Internet Programming Web Application and MVC Chengyu Sun California State University, Los Angeles.
World Wide Web has been created to share the text document across the world. In static web pages the requesting user has no ability to interact with the.
CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles.
JSP java server pages.
Java Web Programimi Servlet and JSP demo.
Introduction to Servlets
Java Class and Servlet ISYS 350.
CS3220 Web and Internet Programming Introduction to Java Servlets
Chengyu Sun California State University, Los Angeles
Handling Errors in Web Applications
CS320 Web and Internet Programming Generating HTTP Responses
Java Server Pages (JSP)
Java Server Pages By: Tejashri Udavant..
Scripted Page Web App Development (Java Server Pages)
Chengyu Sun California State University, Los Angeles
CS5220 Advanced Topics in Web Programming Course Overview
Design and Maintenance of Web Applications in J2EE
Invoking Java Code from JSP
MSIS 655 Advanced Business Applications Programming
Servlets and JSP 20-Nov-18 servletsJSP.ppt.
CS320 Web and Internet Programming Cookies and Session Tracking
Java Server Pages (JSP)
CS320 Web and Internet Programming Java Beans
CS320 Web and Internet Programming MVC Architecture
JavaServer Faces: The Fundamentals
Java Servlets and JSP.
CS3220 Web and Internet Programming Introduction to Java Servlets
J2EE Lecture 1:Servlet and JSP
CS3220 Web and Internet Programming Cookies and Session Tracking
CS4961 Software Design Laboratory Understand Aquila Backend
Rules and Tips for Good Web Programming (and Programming in General)
Chengyu Sun California State University, Los Angeles
CS5220 Advanced Topics in Web Programming Course Overview
Scripted Page Web Application Development (Java Server Pages)
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Chengyu Sun California State University, Los Angeles CS3220 Web and Internet Programming Introduction to JSP and MVC Architecture Chengyu Sun California State University, Los Angeles

Java Server Page (JSP) Why? How? It’s tedious to generate HTML using println() Separate presentation from processing How? Code elements embedded in HTML documents

HelloJSP.jsp <!DOCTYPE html> <html> <head> <meta charset="UTF-8"><title>HelloJSP</title> </head> <body> <p>A JSP without J or S.</p> </body> </html> Current version of Eclipse does not have a JSP template based on HTML5, so to create a JSP in Eclipse, you can first create an HTML page, then rename it to .jsp.

How JSP Works … convert compile execute JSP Servlet ByteCode automatically done by server Look under $TOMCAT_HOME/work/Catalina/localhost/context_name

… How JSP Works Browser Application Server Client JSP Servlet HTTP request HTML HTTP response Client JSP Servlet JSP is processed by application server; HTML is processed by browser. Server

Two Ways to Use JSP JSP the old way (i.e. wrong way) HTML + Java code E.g. Add1.jsp JSP as a template engine HTML + EL/JSTL E.g. Add2.jsp

Add1.jsp <!DOCTYPE html> <html> <head><meta charset="UTF-8"><title>Add1</title></head> <body> <p>The sum of <%= request.getParameter("a") %> and <%= request.getParameter("b") %> is <%= Integer.parseInt(request.getParameter("a")) + Integer.parseInt(request.getParameter("b")) %> </p> </body> </html>

Add2.jsp <!DOCTYPE html> <html> <head> <meta charset="UTF-8"><title>Add2</title> </head> <body> <p>The sum of ${param.a} and ${param.b} is ${param.a + param.b}.</p> </body> </html>

JSP The Old Way – RequestCounter.jsp <%! int counter = 1; %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Request Counter</title> </head> <body>You are visitor # <%= counter %>. <% ++counter; %> </body> </html> JSP Declaration JSP Expression JSP Scriptlet

Understand JSP Scripting Elements int counter = 1; _jspService() { out.println(“<!DOCTYPE html>”); out.println(“<head>”); … out.println(counter); ++counter; out.println(“</body>”); out.println(“</html>”); }

Why The Old Way Is The Wrong Way Mixing presentation and processing Hard to read, debug, or maintain No clean and easy way to reuse code <% if( Math.random() < 0.5) { %> <H1>Have a <I>nice</I> day!</H1> <% } else { %> <H1>Have a <I>lousy</I> day!</H1> <% } %>

JSP As A Template Engine A template engine combines templates with data to produce documents In other words, JSP is used to display data, no more and no less Who’s going to do the processing? Where does data come from? How to display data without using Java?

The MVC Architecture A software architecture commonly used for web and GUI applications Model-View-Controller (MVC) Model: data Controller: for processing View: for display

MVC in a Web Application ... model 2 controller 1 3 browser view 4 Client Server

... MVC in a Web Application Browser sends a request to controller Controller processes the request, updates some data Controller forwards the request and data to view View generates the response that is sent back to the client

MVC in Java Web Application Model: regular Java classes (a.k.a. bean, or POJO – Plain Old Java Object) E.g. GuestBookEntry Controller: servlet View: JSP

Add Example Using MVC … Data Operations Integer a, b, and sum Display form Process input and display result a: The sum of 10 and 20 is 30. b: Add Back

... Add Example Using MVC Web Application = Data + Operations Create data model classes when necessary One servlet per operation, except for form processing operations doGet() to display form doPost() to process form submission One JSP per view

Forward Request From Controller to View request.getRequestDispatcher( “path_to_jsp" ) .forward( request, response );

Forward vs. Redirect servlet servlet browser browser jsp servlet request request. getRequest Dispatcher (“path_to_jsp”) .forward (request, response); response. sendRedirect (“another_url”) request response browser browser request response response request jsp servlet

Send Data From Controller to View Objects in application and session scope are shared by all servlets and JSPs of the application Additional data can be passed from servlet to JSP in request scope request.setAttribute( “objName”, obj ); request.getRequestDispatcher( “path_to_jsp" ) .forward( request, response );

More About the MVC Example Requests should always go to controllers first Hide JSPs under /WEB-INF so users cannot access them directly Controllers do not generate HTML No out.println() JSPs are only used for display No Java code in JSP