Servlets and Java Server Pages

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 Servlets Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun.
Java Server Pages (JSP)
Introduction to Servlets Based on: Hall, Brown, Core Servlets and JavaServer Pages.
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.
Servlets Stoney Jackson
WEB1P servintro1 Introduction to servlets and JSP Dr Jim Briggs.
An introduction to Java Servlet Programming
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
JSP Java Server Pages Reference:
Introduction to Servlet & JSP
Servlet Session Tracking II Session API All material and examples are from
Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET.
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.
A Servlet’s Job Read explicit data sent by client (form data) Read implicit data sent by client (request headers) Generate the results Send the explicit.
Java Servlets and JSP.
Introduction to Java web programming Dr Jim Briggs JWP intro1.
Chapter 10 Servlets and Java Server Pages. A servlet is a Java class designed to be run in the context of a special servlet container An instance of the.
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.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
Servlets, Sessions, and Cookies Lecture 8 cs193i – Internet Technologies Summer 2004 Kelly Shaw, Stanford University.
Java Servlet Technology. Introduction Servlets are Java programs that run on a Web server, handle HTTP requests and build Web pages Servlet specification.
Servlets. - Java technology for Common Gateway Interface (CGI) programming. - It is a Java class that dynamically extends the function of a web server.
Introduction to JSP Based on: Marty Hall, Larry Brown, Core Servlets and JavaServer Pages.
Java support for WWW Babak Esfandiari (sources: Qusay Mahmoud, Roger Impey, textbook)
Chapter 5 Java Servlets. Objectives Explain the nature of a servlet and its operation Use the appropriate servlet methods in a web application Code the.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
111 Java Servlets Dynamic Web Pages (Program Files) Servlets versus Java Server Pages Implementing Servlets Example: F15 Warranty Registration Tomcat Configuration.
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.
JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA James Faeldon CS 119 Enterprise Systems Programming.
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.
Session tracking There are a number of problems that arise from the fact that HTTP is a "stateless" protocol. In particular, when you are doing on- line.
Java Servlets & Java Server Pages Lecture July 2013.
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.
JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►
Java Servlets and Java Server Pages Norman White Stern School of Business.
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.
©SoftMoore ConsultingSlide 1 Overview of Servlets and JavaServer Pages (JSP)
Java Servlets and Java Server Pages
INVOKING JAVA CODE WITH JSP SCRIPTING ELEMENTS. Creating Template Text A large percentage of your JSP document consists of static text (usually HTML),
Introduction To HTML Dr. Magdi AMER. HTML elements.
How CGI and Java Servlets are Run By David Stein 14 November 2006.
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.
Speaker Name Speaker Title Speaker Title Oracle Corporation Olivier Le Diouris Principal Product Manager Oracle Corporation Building Servlet and JSP Applications.
Java Servlets References: Karen Anewalt, Mary Washington College.
Distributed Web Systems Java Servlets Lecturer Department University.
Introduction to Servlets
JSP Java Server Pages Reference:
Java Servlets By: Tejashri Udavant..
Net-centric Computing
Session Tracking in Servlets
Sessions.
Servlet.
Java Server Pages.
Servlets and JSP 20-Nov-18 servletsJSP.ppt.
Servlets CEN /28/2018 Copyright 2001 Ege Consulting, Inc.
Web Search Interfaces.
Web Search Interfaces by Ray Mooney
Servlets.
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Basic servlet structure
Servlets: Servlet / Web Browser Communication I
Presentation transcript:

Servlets and Java Server Pages Prem Melville The Machine Learning Group Department of Computer Sciences The University of Texas at Austin UT-CS 11/18/2018

What’s a Servlet? Java’s answer to CGI programming Program runs on Web server and builds pages on the fly When would you use servlets? Page is based on user-submitted data e.g search engines Data changes frequently e.g. weather-reports Page uses information from a databases e.g. on-line stores UT-CS 11/18/2018

Tomcat Freely available web server with servlet support http://jakarta.apache.org/tomcat/ Currently installed in (tomcat_dir) /u/ml/tomcat/jakarta-tomcat-3.2.2 Starting and stopping the server tomcat_dir/bin/startup.sh and shutdown.sh Currently running on titan on port 8080 http://titan.cs.utexas.edu:8080/ UT-CS 11/18/2018

Setting Up Your Environment Add to your Classpath tomcat_dir/lib/servlet.jar and jsp.jar Place your servlet classes in tomcat_dir/classes/ Place your HTML and JSP files in tomcat_dir/webapps/ROOT/ UT-CS 11/18/2018

Basic Servlet Structure import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class SomeServlet extends HttpServlet { // Handle get request public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // request – access incoming HTTP headers and HTML form data // response - specify the HTTP response line and headers // (e.g. specifying the content type, setting cookies). PrintWriter out = response.getWriter(); //out - send content to browser } UT-CS 11/18/2018

A Simple Servlet public class HelloWorld extends HttpServlet { import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println("Hello World"); } UT-CS 11/18/2018

Running the Servlet Place your classes in tomcat_dir/classes Run servlet using http://host/servlet/ServletName e.g. http://titan.cs.utexas.edu:8080/servlet/HelloWorld …/servlet/package_name.class_name Restart the server if you recompile UT-CS 11/18/2018

Generating HTML public class HelloWWW extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<HTML>\n" + "<HEAD><TITLE>HelloWWW</TITLE></HEAD>\n" + "<BODY>\n" + "<H1>Hello WWW</H1>\n" + "</BODY></HTML>"); } http://titan.cs.utexas.edu:8080/servlet/HelloWWW UT-CS 11/18/2018

HTML Post Form <FORM ACTION=“/servlet/hall.ThreeParams” METHOD=“POST”> First Parameter: <INPUT TYPE="TEXT" NAME="param1"><BR> Second Parameter: <INPUT TYPE="TEXT" NAME="param2"><BR> Third Parameter: <INPUT TYPE="TEXT" NAME="param3"><BR> <CENTER> <INPUT TYPE="SUBMIT"> </CENTER> </FORM> UT-CS 11/18/2018

Reading Parameters response.setContentType("text/html"); public class ThreeParams extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(… +"<UL>\n" + "<LI>param1: " + request.getParameter("param1") + "\n" + "<LI>param2: " + request.getParameter("param2") + "\n" + "<LI>param3: " + request.getParameter("param3") + "\n" + "</UL>\n" + …); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); UT-CS 11/18/2018

Form Example UT-CS 11/18/2018

Servlet Output UT-CS 11/18/2018

Reading All Params parameter names in unspecified order Enumeration paramNames = request.getParameterNames(); parameter names in unspecified order String[] paramVals = request.getParameterValues(paramName); Array of param values associated with paramName UT-CS 11/18/2018

Session Tracking Typical scenario – shopping cart in online store Necessary because HTTP is a "stateless" protocol Common solutions: Cookies and URL-rewriting Session Tracking API allows you to look up session object associated with current request create a new session object when necessary look up information associated with a session store information in a session discard completed or abandoned sessions UT-CS 11/18/2018

Session Tracking API - I Looking up a session object HttpSession session = request.getSession(true); Pass true to create a new session if one does not exist Associating information with session session.setAttribute(“user”,request.getParameter(“name”)) Session attributes can be of any type Looking up session information String name = (String) session.getAttribute(“user”) UT-CS 11/18/2018

Session Tracking API - II getId the unique identifier generated for the session isNew true if the client (browser) has never seen the session getCreationTime time in milliseconds since session was made getLastAccessedTime time in milliseconds since the session was last sent from client getMaxInactiveInterval # of seconds session should go without access before being invalidated negative value indicates that session should never timeout UT-CS 11/18/2018

Java Server Pages Mixes dynamic content with static HTML Write the regular HTML Enclose dynamic parts in special tags JSPs are equivalent to Servlets Convenient if a lot of HTML is involved Must be located in same directory as html UT-CS 11/18/2018

JSP Syntax - I Expression - <%= expression %> Current time: <%= new java.util.Date() %> Scriptlet- <% code %> <% String queryData = request.getQueryString(); out.println("Attached GET data: " + queryData); %> Declaration - <%! code %> <%! private int accessCount = 0; %> Number of accesses to page: <%= ++accessCount %> UT-CS 11/18/2018

JSP Syntax - II Page directive - <%@ page att="val" %> <%@ page import="java.util.*" %> <%@ page extends="package.class" %> <%@ page errorPage="url" %> Include directive - <%@ include file="url" %> <%@ include file="/navbar.html" %> Predefined variables request, response, out, session UT-CS 11/18/2018

JSP Example <%@ page import="libra.*" %> <html> <%@ page import="libra.*" %> <%@ include file="/libra/Navbar.html" %> <%= (request.getParameter("username")==null ? "" : (request.getParameter("username")+", ")) %>Welcome to Libra! <a href="/servlet/libra.Recommend">Produce recommendations.</a> For more information view <a href="/libra/help.html">help file</a>. </html> UT-CS 11/18/2018

References Core Servlets and JavaServer Pages - Marty Hall Sample code http://www.coreservlets.com/ Online tutorial http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/ UT-CS 11/18/2018