Java Servlets Java Server Pages (JSP)

Slides:



Advertisements
Similar presentations
Servlets & JSPs - Sharad Ballepu.
Advertisements

4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets.
JSP and Servelets.
CGI programming. Common Gateway Interface interface between web server and other programs (cgi scripts) information passed as environment variables passed.
M-V-C for web applications. Model for Web Applications model consists of data and system state database tables –persistent data session information –current.
1 Web Search Interfaces. 2 Web Search Interface Web search engines of course need a web-based interface. Search page must accept a query string and submit.
Servlets, JSP and JavaBeans Joshua Scotton.  Getting Started  Servlets  JSP  JavaBeans  MVC  Conclusion.
JLab Lattice Portal – Data Grid Web Service Ying Chen, Chip Watson Thomas Jefferson National Accelerator Facility.
Exercises of the Tutorial on Advanced Web Programming Authors: Miroslava Mitrovic Dragan Milicev Nino.
1 Servlets Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun.
Apache Tomcat as a container for Servlets and JSP
Java Server Pages (JSP)
Chapter 25 JavaServer Pages and Servlets. CHAPTER GOALS To implement dynamic web pages with JavaServer Pages technology To learn the syntactical elements.
 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 24: Servlets Outline 24.1 Introduction 24.2 Servlet Overview and Architecture Interface.
Java Servlet & JSP © copyright 2005 SNU OOPSLA Lab.
J.Sant Servlets Joseph Sant Sheridan Institute of Technology.
Objectives Ch. D - 1 At the end of this chapter students will: Know the general architecture and purpose of servlets Understand how to create a basic servlet.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 34 Servlets.
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.
Introduction to Servlet & JSP
Comp2513 Java Servlet Basics Daniel L. Silver, Ph.D.
Java Server and Servlet CS616 Team 9 Kim Doyle, Susan Kroha, Arunima Palchowdhury, Wei Xu.
Servlets Compiled by Dr. Billy B. L. Lim. Servlets Servlets are Java programs which are invoked to service client requests on a Web server. Servlets extend.
Servlets. Our Project 3-tier application Develop our own multi-threaded server Socket level communication.
SE-2840 Dr. Mark L. Hornick1 Java Servlet-based web apps Servlet Architecture.
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.
Servlets. - Java technology for Common Gateway Interface (CGI) programming. - It is a Java class that dynamically extends the function of a web server.
Java support for WWW Babak Esfandiari (sources: Qusay Mahmoud, Roger Impey, textbook)
CSC 2720 Building Web Applications
Java Servlets and Java Server Pages Carol Wolf Computer Science.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
SKT-SSU IT Training Center Servlet and JSP. Chapter Three: Servlet Basics.
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems Web based Applications,
Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some.
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.
Java Servlets Outline 24.1 Introduction 24.2 Servlet Overview and Architecture Interface Servlet and the Servlet Life Cycle HttpServlet Class.
Java Servlets Lec 27. Creating a Simple Web Application in Tomcat.
20-Nov-15introServlets.ppt Intro to servlets. 20-Nov-15introServlets.ppt typical web page – source Hello Hello.
Java Servlet API CGI / HTTP Concepts Java Servlet API.
@2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING.
Introduction to Server-Side Web Development Introduction to Server-Side Web Development Session II: Introduction to Server-Side Web Development with Servlets.
L.MARIA MICHAEL VISUWASAM UNIT-4
1 Java Servlets l Servlets : programs that run within the context of a server, analogous to applets that run within the context of a browser. l Used to.
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.
JS (Java Servlets). Internet evolution [1] The internet Internet started of as a static content dispersal and delivery mechanism, where files residing.
Advanced Java Session 6 New York University School of Continuing and Professional Studies.
CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
Java Servlets Java Server Pages (JSP)
Advanced Java Session 6 New York University School of Continuing and Professional Studies.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 21 Java Servlets Wed. 11/22/00 based on material.
How CGI and Java Servlets are Run By David Stein 14 November 2006.
 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,
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.
CS 562 Advanced Java and Internet Application Computer Warehouse Web Application By Team Alpha :-  Puja Mehta (102163)  Mona Nagpure (102147)
Java Servlets References: Karen Anewalt, Mary Washington College.
Introduction to Servlets
Servlet Fudamentals.
Net-centric Computing
JDBC & Servlet CSE 4504/6504 Lab.
HTTP Servlet Overview Servlets are modules that extend request/response-oriented servers, such as Java-enabled web servers. For example, a servlet might.
Chapter 26 Servlets.
Servlet APIs Every servlet must implement javax.servlet.Servlet interface Most servlets implement the interface by extending one of these classes javax.servlet.GenericServlet.
CS122B: Projects in Databases and Web Applications Winter 2019
Java Chapter 7 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Java Servlets Java Server Pages (JSP) Yi Lin

Overview of History CGI CGI Servlet JSP Template (ASP, PHP) (in C) (java, C++) Template (ASP, PHP) complexity Speed, Security Servlet JSP

HelloWorld import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<body>"); out.println("<head>"); out.println("<title>Hello CS764!</title>"); out.println("</head>"); out.println("<h1>Hello CS764!</h1>"); out.println("</body>"); out.println("</html>"); out.close(); }

<title>Hello CS764!</title></head> <body> <html><head></head> <body> <a href="../servlet/HelloWorld"> <h1>Execute HelloWorld Servlet</h1> </a> </body> </html> <html> <head> <title>Hello CS764!</title></head> <body> <h1>Hello CS764!</h1> </body> </html>

Client - Server - DB Web server (Apache, JWS) Client (browser) Trigger Servlet, JSP (Request) Web server (Apache, JWS) Client (browser) Through internet Return html file (Response) Request data JDBC, intranet Return data Database server (DB2)

Life Cycle of Servlet servlet HttpServlet GenericServlet init(ServletConfig); doGet(HttpServletRequest, HttpServletResponse); service(ServletRequest, ServletResponse); doPost(HttpServletRequest, HttpServletResponse); ……. destroy();

Interaction with Client HttpServletRequest String getParameter(String) Enumeration getParameters(String[]) HttpServletResponse Writer getWriter() ServletOutputStream getOutputStream() Handling GET and POST Requests

Assignment 2: Get Stock Price Ass2.html <html><head></head> <body> <form action="../servlet/Ass2Servlet" method=POST> <h2>Stock Symbol name: <input type=text name="stockSymbol"></h2><br> <input type="submit" value = "get price"> </form> </body></html> Client Side

Ass2Servlet public class Ass2Servlet extends HttpServlet { import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class Ass2Servlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse res) throws IOException, ServletException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); String stockSymb = request.getParameter("stockSymbol"); StockGrabber sg = new StockGrabber(); sg.setStockSymbol(stockSymb); // Set the stock symbol as “input” String stockPrice = sg.getPrice();// Get the price of stock System.out.println("After StockGrabber.getPrice --"+stockPrice);// Debug out.println("<html><head></head><body><br><br>"); out.println(stockSymb + " -- " + stockPrice); out.println("<hr>"); out.println("<form action=\"../servlet/Ass2Servlet\" method=POST>"); out.println("<h3>Stock Symbol name: <input type=text name=\"stockSymbol\"></h3>"); out.println("<input type=submit value=\"get price\">"); out.println("</form>"); out.println("</body></html>"); }

Java Server Pages (JSP) Client’s Computer Server 1.Browser requests HTML 7. Server sends HTML back to browser servlet class 5.The servlet runs and generates HTML Java Engine 6. Java Engine sends HTML to server 2. Server sends requests to Java Engine 3. If needed, the Java Engine reads the .jsp file 4. The JSP is turned into a servlet, compiled, and loaded Bean

A First JSP Calculator.html <html> <head></head> <body> <p>Enter two numbers and click the ‘calculate’ button.</p> <form action=“calculator.jsp” method=“get”> <input type=text name=value1><br> <input type=text name=value2 ><br> <input type=submit name=calculate value=calculate> </form> </body> </html> Calculator.html

Calculator.jsp <html> <head><title>A simple calculator: results</title></head> <body> <%-- A simpler example 1+1=2 --%> 1+1 = <%= 1+1 %> <%-- A simple calculator --%> <h2>The sum of your two numbers is:</h2> <%= Integer.parseInt(request.getParameter("value1")) + Integer.parseInt(request.getParameter("value2")) %> </body> </html> Calculator.jsp

JSP Tags Comments <%-- …...text…... --%> Declaration <%! int i; %> <%! int numOfStudents(arg1,..) {} %> Expression <%= 1+1 %> Scriptlets <% … java code … %> include file <%@ include file=“*.jsp” %> …...

Using Java Bean Declaration Setting property Getting property 1. <jsp:useBean id=“bean1” class=“Bean1”/> 2. <jsp:useBean id=“bean1” class=“Bean1” name=“serBean” type=“SerBean1”/> Setting property 1. <jsp:setProperty name=“bean1” property=“color” value=“red”/> 2. <jsp:setProperty name=“bean1” property=“color”/> 3. <jsp:setProperty name=“bean1” property=“color” param=“bgColor”/> 4. <jsp:setProperty name=“bean1” property=“*”/> Getting property 1. <jsp:getProperty name=“bean1” property=“color”/> 2. <%=bean1.getColor() %>

Assg2 example Client side Ass2.html <html> <head></head> <body> <center> <table border = 0> <form action=ass2.jsp method = POST> <tr><td><font color=blue>choose a stock market:</font></td> <td><select name="stockMarket"> <option value="Waterhouse">Waterhouse</option> <option value="Yahoo">Yahoo</option> <option value="ChicagoStockex">Chicago Stockex</option> <option value="Reuters">Reuters</option> </select></td> </tr> <tr><td><font color = blue>input a stock symbol:</font></td> <td><input type="edit" name="stockSymbol" size=15></td> <tr><td></td><td><input type="submit" value = "get price"></td></tr> </table> </form></center> </body></html> Client side Ass2.html

<jsp:setProperty name=“ass2” property=“stockSymbol”/> ass2.jsp <html><head> <jsp:useBean id="ass2" scope="session" class="ass2.StockGrabber" /> <jsp:setProperty name="ass2" property="*" /> </head> <body><h2><% ass2.processInput(); ass2.getPrice(); %> <center><table border=5> <tr><td># of data</td> <td>Stock Market</td> <td>Stock Symbol</td> <td>Stock Price </td> </tr><% String[] stockMarkets = ass2.getStockMarkets(); String[] symbols = ass2.getSymbols(); String[] prices = ass2.getPrices(); for(int i=0; i<prices.length; i++){ <tr><td> <%= i+1 %> </td> <td> <%= stockMarkets[i] %> </td> <td> <%= symbols[i] %> </td> <td><font color=red><%= prices[i] %></font></td> } </table> </center> </h2> <hr><%@include file="ass2.html" %></html> <jsp:setProperty name=“ass2” property=“stockSymbol”/> <jsp:setProperty name=“ass2” property=“stockMarket”/> Server side

Without using JDBC Public class StockGrabber { ... public void processInput(){ if(stockMarket.compareTo("Waterhouse")==0){ setPrePriceString("<!--Last-->"); setPostPriceString("</FONT>"); setUrlPrefix("http://research.tdwaterhouse.com/ waterhouse/quote.asp?ticker="); } else if(stockMarket.compareTo("Yahoo")==0){ setPrePriceString("<td nowrap><b>"); setPostPriceString("</b></td>"); setUrlPrefix("http://finance.yahoo.com/q?s="); else if(...){} else{...}

Using JDBC --> Database import java.sql.*; Public class StockGrabber { ... public void processInput(){ try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String sourceURL="jdbc:odbc:stockInfo"; Connection databaseConnection=DriverManager.getConnection(sourceURL); Statement statement=databaseConnection.createStatement(); ResultSet info =statement.executeQuery( "select tPrePriceStr, tPostPriceStr, tUrlPrefix from stockMarketData where tStockMarket = stockMarket”); while(inf.next()) { prePriceString = info.getString(”tPrePriceStr"); postPriceString = info.getString(“tPostPriceStr”); urlPrefix = info.getString(“tUrlPrefix”); } catch(SQLException e){ ... }