ISOM MIS 3150 Module 3 3-Tier Webapps using JSP/JDBC Arijit Sengupta.

Slides:



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

Michael Brockway Application Integration JavaServer Pages l Introduction & Overview l Implicit Objects l Scripting l Standard Actions l Directives l References.
Chapter 51 Scripting With JSP Elements JavaServer Pages By Xue Bai.
Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301.
28/1/2001 Seminar in Databases in the Internet Environment Introduction to J ava S erver P ages technology by Naomi Chen.
DT211/3 Internet Application Development
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
DT228/3 Web Development JSP: Directives and Scripting elements.
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.
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.
Introduction to Java web programming Dr Jim Briggs JWP intro1.
1 Guide to JSP common functions 1.Including the libraries as per a Java class, e.g. not having to refer to java.util.Date 2.Accessing & using external.
UNIT-V The MVC architecture and Struts Framework.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 37 - JavaServer Pages (JSP): Bonus for Java Developers Outline 37.1 Introduction 37.2 JavaServer.
Java Server Pages B.Ramamurthy. Topics for Discussion 8/20/20152 Inheritance and Polymorphism Develop an example for inheritance and polymorphism JSP.
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
ASHIMA KALRA.  INTRODUCTION TO JSP INTRODUCTION TO JSP  IMPLICIT OBJECTS IMPLICIT OBJECTS  COOKIES COOKIES.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
JSP Standard Tag Library
CSCI 6962: Server-side Design and Programming JDBC Database Programming.
Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 1 of 37 J2EE Web Components Pre-assessment Questions 1.The _____________ attribute of a JSP page.
Active Server Pages ASP is Microsoft’s server-side script engine for dynamically-generated web pages. Most common language used is VBScript. If you use.
Introduction to Java Server Pages (JSPs) Robert Thornton.
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.
Introduction to JavaServer Pages (JSP) Slides from Dr. Mark Llewellyn.
Java Server Pages Lecture July Java Server Pages Java Server Pages (JSPs) provide a way to separate the generation of dynamic content (java)
Jordan Anastasiade. All rights reserved.
 Embeds Java code  In HTML tags  When used well  Simple way to generate dynamic web-pages  When misused (complex embedded Java)  Terribly messy.
COMP 321 Week 7. Overview HTML and HTTP Basics Dynamic Web Content ServletsMVC Tomcat in Eclipse Demonstration Lab 7-1 Introduction.
Chapter 8 Script-free pages. Problem with scripting in JSP When you use scripting (declaration, scriplet, expressions) in your JSP, you actually put Java.
JSP Most of the web developers deploying web applications using servlets mixes the presentation logic and business logic. Separation of business logic.
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.
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content Java Server Pages™ A technology for serving dynamic web content Stanisław Osiński,
JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA James Faeldon CS 119 Enterprise Systems Programming.
JSTL Lec Umair©2006, All rights reserved JSTL (ni) Acronym of  JavaServer Pages Standard Tag Library JSTL (like JSP) is a specification, not an.
Introduction to JavaServer Pages. 2 JSP and Servlet Limitations of servlet  It’s inaccessible to non-programmers JSP is a complement to servlet  focuses.
Writing Enterprise Applications with J2EE (Fourth lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)
JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 27 JavaServer Page.
CSC 2720 Building Web Applications JavaServer Pages (JSP) The Basics.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
Copyright © 2002 ProsoftTraining. All rights reserved. JavaServer Pages.
3/6/00jsp00 1 Java Server Pages Nancy McCracken Northeast Parallel Architectures Center at Syracuse University.
CSC 2720 Building Web Applications JavaServer Pages (JSP) JSP Directives and Action Elements.
JSP. Types of JSP Scripting Elements Expressions of the form, which are evaluated and inserted into the servlet's output. Scriptlets of the form, which.
OOSSE Week 8 JSP models Format of lecture: Assignment context JSP models JSPs calling other JSPs i.e. breaking up work Parameter passing JSPs with Add.
JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.
CS562 Advanced Java and Internet Application Introduction to the Computer Warehouse Web Application. Java Server Pages (JSP) Technology. By Team Alpha.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
Basic JSP Celsina Bignoli Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped.
Database Access Using JDBC BCIS 3680 Enterprise Programming.
STRUCTURE OF JSP PRESENTED BY: SIDDHARTHA SINGH ( ) SOMYA SHRIVASTAV ( ) SONAM JINDAL ( )
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 43 JavaServer Page.
CSI 3125, Preliminaries, page 1 JSP (Java Server Pages)
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.
1 Chapter 27 JavaServer Page. 2 Objectives F To know what is a JSP page is processed (§27.2). F To comprehend how a JSP page is processed (§27.3). F To.
Chapter 6 Chapter 6 Server Side Programming (JSP) Part 1 1 (IS 203) WebProgramming (IS 203) Web Programming.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
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.
Bayu Priyambadha, S.Kom. Static content  Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML.
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.
A Presentation Presentation On JSP On JSP & Online Shopping Cart Online Shopping Cart.
Running a Forms Developer Application
Developing JavaServer Pages
Java Server Pages.
Scripted Page Web App Development (Java Server Pages)
Pre-assessment Questions
Knowledge Byte In this section, you will learn about:
Invoking Java Code from JSP
Scripted Page Web Application Development (Java Server Pages)
Presentation transcript:

ISOM MIS 3150 Module 3 3-Tier Webapps using JSP/JDBC Arijit Sengupta

ISOM Structure of this semester Database Fundamentals Relational Model Normalization Conceptual Modeling Query Languages Advanced SQL XML Databases Java DB Applications – JDBC/JSP Data Mining 0. Intro 1. Design 3. Applications 4. Advanced Topics NewbieUsersProfessionalsDesigners MIS Querying Developers

ISOM Today’s Buzzwords 3-Tier applications  Client – WebServer – ApplicationServer Basics of JDBC Basics of JSP Containers - Tomcat Web Applications using JSP

ISOM 3 Tier Architecture HTTP request HTTP response Web server JSP page JSP container compiles to a servlet URL request JavaBean Library DB properties, call methods HTTP page response Browser

ISOM JDBC  A platform-independent library of classes allowing database access from any Java application  Take advantages of Polymorphism JDBC is a set of interfaces –Driver, Connection, Statement, ResultSet, etc. Database vendors (not programmers) will implement these interfaces. If we switch from one database to another, we just need to load different driver (plug and play)! –YOU DON'T NEED TO MODIFY THE REST OF YOUR PROGRAM!

ISOM JDBC DriverManagerConnectionStatementResultSet Database Driver

ISOM JDBC DriverManagerConnectionStatementResultSet Driver Database

ISOM JDBC (Contd.) Register a JDBC driver Driver d = new oracle.jdbc.driver.OracleDriver(); DriverManager.registerDriver (d);  or DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver()); Or, use the Java reflection abilities Class.forName( "oracle.jdbc.driver.OracleDriver");  calling Class.forName() will create an instance of a driver and register it with the DriverManager automatically  This is better since we can use a constant: String DRIVER = "oracle.jdbc.driver.OracleDriver"; Class.forName(DRIVER); For mysql, use: com.mysql.jdbc.driver

ISOM JDBC DriverManagerConnectionStatementResultSet Driver Database

ISOM JDBC (Contd.) Make a connection String URL = 1521:ORA2"; // For Oracle String URL = ”jdbc:mysql://localhost/employees"; // For MySQL Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);

ISOM JDBC DriverManagerConnectionStatementResultSet Driver Database

ISOM JDBC (Contd.) Create a statement Statement st = conn.createStatement(); //default:TYPE_FORWARD_ONLY and CONCUR_READ_ONLY  or Statement st =conn.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); //the resultset will be scrollable and sensitive to changes made by others //we can update the resultset

ISOM JDBC DriverManagerConnectionStatementResultSet Driver Database

ISOM JDBC (Contd.) Execute a query String SQL = "INSERT INTO s" + " VALUES (' ')"; int result = st.executeUpdate(SQL); //either the row count for INSERT, UPDATE or DELETE or 0 for SQL statements that return nothing Execute a query and create a resultset String SQL = "SELECT * FROM Student"; ResultSet rec = st.executeQuery(SQL);

ISOM JDBC (Contd.) Process the resultset while(rec.next()) { System.out.println(rec.getString("snum")); }  or while(rec.next()) { System.out.println(rec.getString(1)); // first column of the resultset }  There are methods like getString(), getInt(), etc. that take either a column name or column position See for all JDBC Class documentation

ISOM Tomcat – a J2EE Container Open Source – integrated as an Apache.org project Can be obtained from Provides full JSP 2.0/Servlet 2.4 functionality

ISOM Elements of a Java Server Page  Directives: Provide global information to the page Import statements Scripting language u Declarations: For page-wide variable and method declarations

ISOM Elements of a Java Server Page Scriptlets:  This is the Java code embedded in the web pages Expressions:  Formats the expression as a string to be included in the output of the web page Comments: User readable comments, contents ignored and removed by the JSP Compiler

ISOM JSP Directives  General syntax:  Possible values for directives are:  Page - Information for the page Include - Specifies the files whose contents are to be included in the output  e.g., Taglib  The URI for a library of custom tags that may be used in the page

ISOM JSP Page Directive The page directive may take the following values:   This variable tells the JSP engine what language will be used in the file "java" is the only language supported by JSP in the current specification  Comma separated list of classes and packages that are used in the JSP page  Should appear at the top of the file

ISOM JSP Page Directive (Contd.)  true indicates that session data is available to the page  By default, this is set to true  Determines the size of the output stream buffer  Defaults to 8kb  Use with autoFlush  When set to true, flushes the output buffer when it is full, rather than raising an exception

ISOM JSP Page Directive (contd.)  Specifies the relative path of the page, where control would be transferred if any exceptions are thrown from this page  The error handling JSP page should have its isErrorPage directive set to true  Marks the page as an error handler

ISOM JSP Declarations Class and instance variables (of the generated servlet class) may be specified using the JSP Declaration tag: <%! String name = “Web Applications"; int index = 10; int count = 0; %> Methods may also be specified: <%! private int getNextIndex() { return index ++; } %>

ISOM JSP Scriptlets JSP scriptlets are defined as block of Java code embedded between a pair of tags,. Example: <% java.util.Date d = new java.util.Date(); out.println(d); %>

ISOM JSP Expressions Useful for embedding the result of a Java expression in a HTML page  The expression is encoded between the tags  The value of the expression is converted to a string and then displayed  Conversion of primitive types to string happens automatically  Example:  The date is

ISOM JSP Implicit Objects When writing scriptlets and expressions, the following objects (called implicit objects) are available by default: request javax.servlet.http.HttpServletRequest response javax.servlet.http.HttpServletResponse out javax.servlet.jsp.JspWriter session javax.servlet.http.HttpSession application javax.servlet.ServletContext exception java.lang.Throwable

ISOM Reading inputs from Forms/URLS Remember that parameters are passed to Web applications via one of two methods:  GET method: parameters are passed directly through the URL, encoded using the urlencoding method Quick to create and test – can be created without a form Can be bookmarked URL shows in plaintext – not secure  POST method: parameters are encoded and sent to the server separately from the URL Can only be created via forms (or advanced applications) Secure – parameters cannot be seen Reading a single parameter via name: String value = request.getParameter(paramname); String [] values = request.getParameterValues(paramname); /* for multivalued parameters like checkboxes/multiple selectable lists */ Better way – using “Beans” - shortly

ISOM JSP Session Example Visitor Count -- JSP Session Visitor Count This JSP page demonstrates session management by incrementing a counter each time a user accesses a page. <%! private int totalHits = 0; %> <% session = request.getSession(true); Integer ival = (Integer)session.getValue("jspsession.counter"); if (ival == null) ival = new Integer(1); else ival = new Integer(ival.intValue() + 1); session.putValue("jspsession.counter", ival); %> You have hit this page time, out of a total of page hit !

ISOM JSP Actions Actions are tags that may affect the runtime behavior of the JSP or affect the current out stream; they may also use, modify and/or create objects. JSP specification defines the following standard actions: 

ISOM JSP Actions  New action types are introduced by means of custom tags

ISOM What is a JavaBean? A Java class that (at a minimum)  has an empty constructor  has getters and setters for each of the properties in the class  implements the serializable interface

ISOM JSP Actions and Attributes JSP actions can define named attributes and associated values My name is: Name using Bean is:

ISOM Beans and HTML forms You may connect HTML form parameters to Bean properties <jsp:setProperty name="id" property="accountHolder" value = " /> There is a shorthand for the above:  This works if the property name was exactly the same as the form parametername

ISOM Beans and HTML forms If the form parameter name and the property name do not match, then use the following variant of "jsp:setProperty" Another powerful variation of "jsp:setProperty" examines all the parameter names from the request object and if some of them match with property names, it sets the appropriate properties

ISOM Beans and HTML (contd.) Name is: Age is: Weight is:

ISOM Including Files JSP supports two kinds of file inclusion:  Static (using the directive "include")  Dynamic (or Request-Time) Static inclusion is specified using the "include" directive  e.g.,  In static inclusion, the contents of "header.html" are included in the output of the containing JSP file during the JSP page compilation  If the contents of the "header.html" file change, these changes are not visible to the user of the containing JSP file  Static inclusion is fast (because inclusion is done at compile time)

ISOM Including Files (contd.) Dynamic inclusion is supported by the use of the tag "jsp:include"  e.g.,  Each time the containing JSP file is accessed, the JSP engine includes the contents of the latest copy of the file "news/headlines.jsp“  You may also pass parameters to the included file, e.g:...

ISOM Forwarding Files A request to a JSP file may be forwarded, transparently to the user, to another JSP file   This is used to redirect request from one page to another page  The calling JSP file relinquishes control to the forwarded page  The calling JSP cannot send any data back to the browser before invoking this tag  If data had already been sent by the calling JSP, invoking the "jsp:forward" tag causes an exception to be thrown

ISOM Forwarding Requests You may also pass parameters to the forwarded page  ...  Parameters passed in this fashion may be accessed by the forwarded JSP (catalog.jsp) by the following call:  request.getParameter("color");

ISOM Forwarding Requests Request forwarding is typically used for authentication <% if(request.getParameter("login-info") == null) %> <% else { /* Generate Error message – no login */ } %>

ISOM Summary JSP – JDBC provides a stable and well-tested platform for 3-tier web application design Supports all major databases and all platforms With Servlet compilation, performance is high Many major applications like Wiki, major Government/Funding organizations like NSF – are running on Java/JSP technology. Web services are well-supported J2EE provides a well-designed software design and development platform for enterprise systems.