Chengyu Sun California State University, Los Angeles

Slides:



Advertisements
Similar presentations
 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
Advertisements

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:
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.
JSP Architecture  JSP is a simple text file consisting of HTML or XML content along with JSP elements  JSP packages define the interface for the compiled.
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,
Applets & Servlets.
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.
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 6 Server-side Programming: Java Servlets
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.
Server-side Programming The combination of –HTML –JavaScript –DOM is sometimes referred to as Dynamic HTML (DHTML) Web pages that include scripting are.
Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering
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.
IS-907 Java EE World Wide Web - Overview. World Wide Web - History Tim Berners-Lee, CERN, 1990 Enable researchers to share information: Remote Access.
Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection.
CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles.
STRUCTURE OF JSP PRESENTED BY: SIDDHARTHA SINGH ( ) SOMYA SHRIVASTAV ( ) SONAM JINDAL ( )
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
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
CS3220 Web and Internet Programming Introduction to Java Servlets
Web Concepts Lesson 2 ITBS2203 E-Commerce for IT.
CS320 Web and Internet Programming Generating HTTP Responses
Java Server Pages By: Tejashri Udavant..
Scripted Page Web App Development (Java Server Pages)
CS3220 Web and Internet Programming Generating HTTP Responses
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
CS3220 Web and Internet Programming Cookies and Session Tracking
Chengyu Sun California State University, Los Angeles
CS3220 Web and Internet Programming Introduction to Java Servlets
CS3220 Web and Internet Programming Handling HTTP Requests
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
J2EE Lecture 1:Servlet and JSP
CS3220 Web and Internet Programming Cookies and Session Tracking
Chengyu Sun California State University, Los Angeles
CS4961 Software Design Laboratory Understand Aquila Backend
Rules and Tips for Good Web Programming (and Programming in General)
CS5220 Advanced Topics in Web Programming Course Overview
Scripted Page Web Application Development (Java Server Pages)
Chengyu Sun California State University, Los Angeles
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> Change default JSP encoding to UTF-8 in Eclipse Preferences

Access JSP Accessing JSP is similar to accessing static resources like HTML pages and images – it's based on path rather than URL mapping like for servlets

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