Core servlets chapter 6 Generating server response: http status codes.

Slides:



Advertisements
Similar presentations
Chapter 6 Server-side Programming: Java Servlets
Advertisements

Server Side Programming Common Gateway Interface (CGI): Scripts generate Web pages or other files dynamically by processing form data and returning documents.
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.
HTTP – HyperText Transfer Protocol
CSE 190: Internet E-Commerce Lecture 7. HTML Templates Designed to separate server side logic from HTML presentation Key features –Escapes from HTML into.
CS320 Web and Internet Programming Generating HTTP Responses
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 34 Servlets.
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
How the web works: HTTP and CGI explained
Core Servlets Chapter 3 Link for Core Servlets code: om/archive/ om/archive/
1 CS6320 – Servlet Structure and Lifecycle L. Grewe.
Chapter 5 Sending html information. Aside on class files for servlets you already know you’ll need javax.servlet and javax.servlet.http Here’s a link.
Definition Servlet: Servlet is a java class which extends the functionality of web server by dynamically generating web pages. Web server: It is a server.
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.
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.
Web technologies and programming cse hypermedia and multimedia technology Fanis Tsandilas April 3, 2007.
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
CHAPTER 12 COOKIES AND SESSIONS. INTRO HTTP is a stateless technology Each page rendered by a browser is unrelated to other pages – even if they are from.
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
JavaScript, Fourth Edition Chapter 12 Updating Web Pages with AJAX.
Servlets Pranav Maydeo. What is a Servlet ? Servlets are modules of Java code that run in a server application to answer client requests. Servlets are.
Java Omar Rana University of South Asia. Course Overview JAVA  C/C++ and JAVA Comparison  OOP in JAVA  Exception Handling  Streams  Graphics User.
Java support for WWW Babak Esfandiari (sources: Qusay Mahmoud, Roger Impey, textbook)
CSC 2720 Building Web Applications
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.
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.
COMP 321 Week 7. Overview HTML and HTTP Basics Dynamic Web Content ServletsMVC Tomcat in Eclipse Demonstration Lab 7-1 Introduction.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
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.
Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time.
Slides © Marty Hall, book © Sun Microsystems Press 1 Generating the HTTP Response 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.
16-Oct-15 JSP Implicit Objects. 2 JSP Implicit Objects are the Java objects that the JSP Container makes available to developers in each page and developer.
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Chapter 2 Web app architecture. High-level web app architecture  When a client request coming in and needs servlet to serve dynamic web content, what.
Saving Client State Session Tracking: Maintain state about series of requests from same client over time Using Cookies: Clients hold small amount of their.
1-1 HTTP request message GET /somedir/page.html HTTP/1.1 Host: User-agent: Mozilla/4.0 Connection: close Accept-language:fr request.
S ERVLETS Hits Counter 21-Nov-15. S ERVLETS - H ITS C OUNTER Many times you would be interested in knowing total number of hits on a particular page of.
Java Servlet API CGI / HTTP Concepts Java Servlet API.
Introduction to Server-Side Web Development Introduction to Server-Side Web Development Session II: Introduction to Server-Side Web Development with Servlets.
S ERVLETS Cookies Handling 5-Dec-15. S ERVLETS - C OOKIES H ANDLING Cookies are text files stored on the client computer and they are kept for various.
Operating Systems Lesson 12. HTTP vs HTML HTML: hypertext markup language ◦ Definitions of tags that are added to Web documents to control their appearance.
Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection.
Servlets.
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.
©SoftMoore ConsultingSlide 1 Filters. Filters can be used in a web application to intercept, examine, and possibly transform requests or responses associated.
SKT-SSU IT Training Center Servlet and JSP. Chapter Seven: Generating the Server Response: HTTP Response Headers.
HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,
How CGI and Java Servlets are Run By David Stein 14 November 2006.
April 20023CSG11 Electronic Commerce Java (2) John Wordsworth Department of Computer Science The University of Reading Room 129,
Network Programming: Servers. Agenda l Steps for creating a server Create a ServerSocket object Create a Socket object from ServerSocket Create an input.
Introduction to Servlets
Servlet Fudamentals.
Java Servlets By: Tejashri Udavant..
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.
Generating the Server Response: HTTP Status Codes
Servlet APIs Every servlet must implement javax.servlet.Servlet interface Most servlets implement the interface by extending one of these classes javax.servlet.GenericServlet.
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.
Pre-assessment Questions
Presentation transcript:

Core servlets chapter 6 Generating server response: http status codes

A request GET /…/someName HTTP/1.1 Host:…. Header2:… … HeaderN:…

A response HTTP/ OK Content-type: text/html Header2:… … HeaderN:… (blank line) …

(some) HTTP 1.1 status codes – information to the client to take some further action – request was successful – files have moved LOCATION header may provide new address – client error – server error In servlets, you send the code using response.SC_SOME_MESSCODE

Status codes and responses In response to 100 (Expect request header with value Continue) the server could send 100 SC_Continue – client should go ahead or 417 SC_EXPECTATION_FAILED – it won’t accept the document 200 OK – the default for servlets. 202 Accepted, send SC_ACCEPTED to indicate request is still being processed. 204 No Content – send SC_NO_CONTENT to indicate browser should continue to display previous document. Useful if the client is reloading but you can tell that the doc is up to date. 205 reset is used to clear form fields. Send SC_RESET_CONTENT, new in HTTP Moved Permanently and 302 Found: SC_MOVED_PERMANENTLY typically sends a Location header with the new URL. 302 may indicate a temporary move. Servlet uses SC_MOVED_TEMPORARILY

More codes 303 (See Other)

Redirect vs refresh In 302 the browser automatically loads the URL from the location header. Track user behavior by routing them to a link on your own site and then recording what they select Side effects – you can return both a Set-Cookie response header via response.addCookie and a 302 status code by means of a response.sendRedirect(url). This latter is shorter and easier than using both response.setStatus(response.SC_MOVED_TEMPORAR ILY) and respnse.setHeader(“Location”,url)

Wrongdestination servlet sends you to…

The servlet that presents a form

After submitting query

I modified this to count hits to various engines using a file (see Hunter chapt 3 notes) Yahoo 3 MSN 1 Google 1 AllTheWeb 1 AltaVista 0 HotBot 0 Lycos 1

First create a file – this servlet could be run on startup… or file could be built manually import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class WriteToFile extends GenericServlet { public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println("starting to write"); FileWriter fileWriter = null; PrintWriter printWriter = null; try { fileWriter = new FileWriter("EngineCounter.initial"); printWriter = new PrintWriter(fileWriter); for(int j=0;j<7;j++) printWriter.println("0"); return; } catch (IOException e) { // problem during write // Log the exception. See Chapter 5. out.println("exception"+e.toString()); } finally { // Make sure to close the file if (printWriter != null) { printWriter.close();} out.println("writing complete.."); //and don’t forget to close the file //fout.close();}} public String getServletInfo() { return "A servlet that writes to a file in work/ dir"; }}

logic Next, count “hits” to engine by searching a string array for the name of the engine selected. Read file contents, increment one value Re-write file contents My array: String names[]={"Yahoo","MSN","Google","AllTheWeb","AltaVista","HotBot", "Lycos"}; I didn’t write it, but another servlet could be run at the end of the day to view engine-hit distribution

My update file function in the redirect servlet public void update_file(String name)throws IOException{ int pos=-1; for(int k=0;k<names.length;k++)if(name.equals(names[k])){pos=k; System.out.println("name is "+name+" pos is"+pos);} int ct[]=new int[names.length]; int i=0; FileInputStream fin=new FileInputStream("C:/apache-tomcat /apache-tomcat /bin/enginecounter.initial"); Scanner s=new Scanner(fin); while(s.hasNext()){ ct[i]=s.nextInt(); i++; } fin.close(); if(pos!=-1)ct[pos]++; FileWriter fileWriter = new FileWriter("EngineCounter.initial"); PrintWriter printWriter = new PrintWriter(fileWriter); for(int j=0;j<7;j++) printWriter.println(ct[j]); if (printWriter != null) { printWriter.close(); }//if }//update