JSP Presented by K.Venkata Ratnam HOD MCA (Dept) Newton’s Institute of Engineering.

Slides:



Advertisements
Similar presentations
Java Server Pages (JSP)
Advertisements

JSP1 Java Server Pages (JSP) Introducing JavaServer Pages TM (JSP TM ) JSP scripting elements.
Servlets Stoney Jackson
An introduction to Java Servlet Programming
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
JSP Java Server Pages Reference:
2/16/2004 Dynamic Content February 16, /16/2004 Assignments Due – Message of the Day Part 1 Due – Reading and Warmup Work on Message of the Day.
Web Development in Java Andrew Simpson. Overview Background Language Details Java Server Pages (JSP) Servlets Database Connectivity (JDBC) Samples and.
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,
Introduction to Servlet & JSP
Java database Programming JDBC Trademarked name of a Java API that supports Java programs that access relational databases Stand for Java DataBase Connectivity.
Three types of scripting elements: 1.Expressions 2.Scriptlets 3.Declarations Scripting elements.
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems JavaServer Pages (JSP)
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.
CSCI 6962: Server-side Design and Programming History and Background.
Java Servlets. What Are Servlets? Basically, a java program that runs on the server Basically, a java program that runs on the server Creates dynamic.
Servlet and JSP Programming: An Introduction Spiros Papadimitriou
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
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.
Netbeans – jsp.zip Introduction to JSP Netbeans – jsp.zip.
Getting connected.  Java application calls the JDBC library.  JDBC loads a driver which talks to the database.  We can change database engines without.
1 Servlet How can a HTML page, displayed using a browser, cause a program on a server to be executed?
Servlets. - Java technology for Common Gateway Interface (CGI) programming. - It is a Java class that dynamically extends the function of a web server.
JSP Java Server Pages Softsmith Infotech.
Java support for WWW Babak Esfandiari (sources: Qusay Mahmoud, Roger Impey, textbook)
Java Servlets and Java Server Pages Carol Wolf Computer Science.
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.
 Embeds Java code  In HTML tags  When used well  Simple way to generate dynamic web-pages  When misused (complex embedded Java)  Terribly messy.
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems Web based Applications,
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:
Chapter 3 Servlet Basics. 1.Recall the Servlet Role 2.Basic Servlet Structure 3.A simple servlet that generates plain text 4.A servlet that generates.
Servlets Database Access. Agenda:  Setup Java Environment  Install Database  Install Database Drivers  Create Table and add records  Accessing a.
Java Servlets & Java Server Pages Lecture July 2013.
Java Servlets Lec 27. Creating a Simple Web Application in Tomcat.
JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP.
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.
A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing.
Java Servlets and Java Server Pages Norman White Stern School of Business.
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.
Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, Responds oriented other.
1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.
Basic JSP Celsina Bignoli Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
STRUCTURE OF JSP PRESENTED BY: SIDDHARTHA SINGH ( ) SOMYA SHRIVASTAV ( ) SONAM JINDAL ( )
©SoftMoore ConsultingSlide 1 Overview of Servlets and JavaServer Pages (JSP)
Java Servlets and 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.
 Java Server Pages (JSP) By Offir Golan. What is JSP?  A technology that allows for the creation of dynamically generated web pages based on HTML, XML,
JSP JavaServer Pages. What is JSP? Java based technology that simplifies the development of dynamic web sites JSP pages are HTML pages with embedded code.
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.
Apr 3, 2013 JSP Java Server Pages. 2 A “Hello World” servlet (from the Tomcat installation documentation) public class HelloServlet extends HttpServlet.
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.
S ERVLETS Form Data 19-Mar-16. F ORM P ROCESSING You must have come across many situations when you need to pass some information from your browser to.
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.
CS 562 Advanced Java and Internet Application Computer Warehouse Web Application By Team Alpha :-  Puja Mehta (102163)  Mona Nagpure (102147)
Introduction to Servlets
Developing JavaServer Pages
Servlet Fudamentals.
Java Server Pages.
Servlets and Java Server Pages
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Java Servlets Servlet Overview Servlets and HTML Forms Servlet Basics
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Presentation transcript:

JSP Presented by K.Venkata Ratnam HOD MCA (Dept) Newton’s Institute of Engineering

JSP: Java Server Page JSP helps in generating dynamic content, based on user input, time of day, or any other runtime conditions. Web application: a program on the server processes requests and generates response. Problems with servlets: 1. Detailed Java programming knowledge is needed. 2. To change the look and feel, change the servlet code and recompile. recompile. 3. Restart the server and run the servlet program

Hello world servlet public class HelloWorldServlet implements Servlet{ public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { response.setContentType(“text/html”); PrintWriter out=response.getWriter(); out.println(“ ”); out.println (“ Hello world ”); out.println(“ ”); out.println(“ Hello World ”); out.println(“ It's ”+ (new java.util.date().toString()) + “and all is well”); out.println(“ ”); }

Hello world JSP Page Hello world Hello World It,s and all is well.

What is JSP? Mostly HTML page, with extension.jsp Include JSP tags to enable dynamic content creation Translation: JSP → Servlet class Compiled at Request time (first request, a little slow) Execution: Request → JSP Servlet's service method

Anatomy of a JSP Page A JSP page is a regular web page with JSP elements for generating the parts of the page that differ for each request. JSP separates the request processing and the business logic code from the presentation. In servlet, HTML is embedded, here JSP elements are added to genearate the dynamic content.

Life Cycle A JSP page is translated into a Java Servlet And then compiled On Tomcat, the compilation happens the first time a page is requested Above three requests are processed in first request Afterwards, just as fast as a Servlet (because it is then a servlet)

Hello World <html> Hello JSP Hello JSP <body> Hello World: Hello World: </p></body></html> See also: Date_jsp.java – the Servlet this page is translated to Date_jsp.java

Date_jsp.java (extract) This extract shows the part that produces the output – compare it with the JSP: out = pageContext.getOut(); out = pageContext.getOut(); _jspx_out = out; _jspx_out = out; out.write(" \r\n"); out.write(" \r\n"); out.write(" "); out.write(" "); out.write(" Hello JSP "); out.write(" Hello JSP "); out.write(" "); out.write(" "); out.write(" \r\n"); out.write(" \r\n"); out.write(" Hello World:\r\n "); out.write(" Hello World:\r\n "); out.print( new java.util.Date() ); out.print( new java.util.Date() ); out.write("\r\n"); out.write("\r\n");

Produced

JSP scripting elements The expression is evaluated and the result is inserted into the HTML page The code is inserted into the servlet's service method This construction is called a scriptlet The declarations are inserted into the servlet class, not into a method Used to declare variables and methods

Example for Expression Hello! The time is now Note: The tag is used, because we are computing a value and inserting it into the HTML The fully qualified name (java.util.Date) is used, instead of the short name (Date)

Scriptlets: It is difficult to do much programming just by putting Java expressions inside HTML. JSP also allows you to write blocks of Java code inside the JSP, by placing your Java code between characters (without the = sign at the start of the sequence.) This block of code is known as a "scriptlet". A scriptlet contains Java code that is executed every time the JSP is invoked. <% // This is a scriptlet. Notice that the "date" // variable we declare here is available in the // embedded expression later on. System.out.println( "Evaluating date now" ); java.util.Date date = new java.util.Date(); %> Hello! The time is now

Mixing Scriptlets and HTML <% int n=5; for ( int i = 0; i < n; i++ ) { %> Number <% } %>

Example for JSP Declarations Square root of (x)= <% for(int i=1;i<=10;i++) { out.print("Hello JSP World! "); } %> <%! int mat(int x, int y) { return x*y; }%> <% out.println("Multiplication of(x,y)="); out.println(mat(3,4)+" "); %> Example for Declarations

Directives Instructions to the compiler Directives affect the servlet class itself A directive has the form: A directive has the form: The most useful directive is page, which lets you import packages Example: Example:

Example for page directive <% System.out.println( "Evaluating date now" ); Date date = new Date(); %> Hello! The time is now

The include directive The include directive inserts another file into the file being parsed The included file is treated as just one more JSP, hence it can include static HTML Syntax: The URL is treated as relative to the JSP page If the URL begins with a slash, it is treated as relative to the home directory of the Web server The include directive is especially useful for inserting things like navigation bars The include directive is used to physically include the contents of another file (compile-time). Example:

Example for include directive Going to include time.jsp...

Actions Actions are XML-syntax tags used to control the servlet engine Inserts the indicated relative URL at execution time (not at compile time, like the include directive does) This is great for rapidly changing data " /> Jump to the (static) URL or the (dynamically computed) JavaExpression resulting in a URL

Example for Example for Example for the

login.jsp Login User Authentication User Login <form name=f1 action=“ method="post"> UserName : Password :

Variables JSP provides several predefined variables request : The HttpServletRequest parameter response : The HttpServletResponse parameter session : The HttpSession associated with the request, or null if there is none out : A JspWriter (like a PrintWriter) used to send output to the client Example: Your hostname:

Request and Response Each JSP page has access to two special objects The Request object carries information passed by the HTTP request (e.g. made by the browser) This includes any submitted form data The Response object is used to pass information back to the Client (browser) E.g. response.getWriter() provides an output stream for direct writing to the client

Example for Request and Response login.html Login User Authentication User Login UserName : Password :

logincheck.jsp Login User Authentication <% response.setContentType(“text/html”); String name=request.getParameter(“uname”); String pass=request.getParameter(“pwd”); if(name==“venkat” && pass==“sneha”) { %> <% } else{ %>

JSP to Servlet Communication Example login.jsp Login User Authentication User Login <form name=f1 action= method=“get"> <table border=1 borderstyle=rigid width=20% cellpadding=5 cellspacing=0 bordercolor=“white”> UserName : Password :

LoginServlet.java import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class LoginServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String name=request.getParameter("uname"); out.println(" "); out.println(" Hello World! "); out.println(" "); out.println(" Welcome Mr/Ms:"+name+“ to our website "); out.println(" "); }

JDBC – Java Database Connectivity JDBC is used for accessing databases from Java applications Information is transferred from relations to objects and vice-versa databases optimized for searching/indexing objects optimized for engineering/flexibility JDBC Architecture Java Application JDBC Oracle DB2 MySQL Oracle Driver DB2 Driver MySQL Driver Network We will use this one…

ApplicationJDBCDriver Java code calls JDBC library JDBC loads a driver Driver talks to a particular database An application can work with several databases by using all corresponding drivers Ideal: can change database engines without changing any application code (not always in practice)

Seven Steps Load the driver Define the connection URL Establish the connection Create a Statement object Execute a query using the Statement Process the result Close the connection

Loading the Driver We can register the driver indirectly using the statement Class.forName("oracle.jdbc.driver.OracleDriver"); Class.forName loads the specified class When OracleDriver is loaded, it automatically creates an instance of itself registers this instance with the DriverManager Hence, the driver class can be given as an argument of the application

An Example // A driver for imaginary1 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // A driver for Oracle Driver driver = new oracle.jdbc.driver.OracleDriver(); DriverManager.registerDriver(driver); //A driver for MySQL Class.forName("com.mysql.jdbc.Driver"); Oracle MS-Access Registered Drivers MySQL

JDBC DRIVERS Type 1 Driver JDBC-ODBC Bridge driver The Type 1 driver translates all JDBC calls into ODBC calls and sends them to the ODBC driver. ODBC is a generic API. Type 2 Driver Native-API/partly Java driver The distinctive characteristic of type 2 jdbc drivers are that Type 2 drivers convert JDBC calls into database-specific calls i.e. this driver is specific to a particular database. Ex: Oracle will have oracle native API. Type 3 Driver All Java/Net-protocol driver Type 3 database requests are passed through the network to the middle-tier server. The middle-tier then translates the request to the database. If the middle-tier server can in turn use Type1, Type 2 or Type 4 drivers. Type 4 Driver Native-protocol/all-Java driver The Type 4 uses java networking libraries to communicate directly with the database server.

Get the user details from the Database getusers.jsp Department of IT Registered User Details <% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:itweb"); PreparedStatement psmt =con.prepareStatement("select * from registration"); ResultSet rs=psmt.executeQuery(); out.println(" "); out.println("<table width=65% border=0 cellpadding=0 cellspacing=2 bgcolor=lightblue>"); out.println(" Name Password "); out.println(" ID Phone No Sex "); out.println(" Date of Birth Language Address /tr>");

while(rs.next()) { out.println(" "); out.println(rs.getString(1)); out.println(" "); out.println(rs.getString(2)); out.println(" "); out.println(rs.getString(3)); out.println(" "); out.println(rs.getString(4)); out.println(" "); out.println(rs.getString(5)); out.println(" "); out.println(rs.getString(6)); out.println(" "); out.println(rs.getString(7)); out.println(" "); out.println(rs.getString(8)); out.println(" "); } out.println(" "); %>

addcart.html Welcome to Amazon.com Enter Book Details BookName: Price: Quantity: Amount: &nbsp

addcart.jsp <% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:mca"); PreparedStatement psmt =con.prepareStatement("insert into books values(?,?,?,?)"); String bname=request.getParameter("bname"); String price=request.getParameter("price"); String quantity=request.getParameter("quant"); String amount=request.getParameter("amt"); psmt.clearParameters(); psmt.setString(1,bname); psmt.setString(2,price); psmt.setString(3,quantity); psmt.setString(4,amount); psmt.executeUpdate(); out.println("Your Books are added to the Cart successfully"); %>

THANKQ TO ALL For Any queries please contact: K.VenkataRatnam MCA, M.Tech(CSE) Assoc.Prof & HOD MCA Mobile: