Presentation is loading. Please wait.

Presentation is loading. Please wait.

HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,

Similar presentations


Presentation on theme: "HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,"— Presentation transcript:

1 HTTP protocol Java Servlets

2 HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS, DELETE HTTP protocol is stateless Server does not know whether this is your first request, or not, and whether this request is yours at all This is bad for e-commerce HTTP protocol, Java Servlets2

3 HTTP request structure Method URL Version cr lf Header field name: value cr lf. Header field name: value cr lf cr lf Data HTTP protocol, Java Servlets3

4 HTTP request example GET /~donatas/index.html HTTP/1.1¶ Host: www.mif.vu.lt¶ User-agent: Mozilla/4.0¶ Accept-language: en¶ ¶ HTTP protocol, Java Servlets4

5 HTTP response structure Version status code phrase cr lf Header field name: value cr lf. Header field name: value cr lf cr lf Duomenys HTTP protocol, Java Servlets5

6 HTTP response example HTTP/1.1 200 OK Date: Wed, 03 Mar 2004 10:00:15 GMT Server: Apache/1.3.27 (Unix) Last-Modified: Mon, 01 Mar 2004 12:15:34 GMT Content-Length: 6821 Content-Type: text/html ¶... HTTP protocol, Java Servlets6

7 Java Servlet Technology Java Servlet – component framework, that: allows to process incoming HTTP requests: to retrieve request parameters as Java data structures, to convert the response to HTTP protocol format provides means to identify end-users and associate state with them (so called session) Cookies URL rewriting Hidden form fields components in this framework are called servlets Java Servlets do not provide functionality for sending HTTP requests HTTP protocol, Java Servlets7

8 Java Servlet example public class SimpleServlet extends HttpServlet { public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String title = "Hello World !"; String selectedProductID = request.getParameter(“selectedProduct”); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(" "); out.println(title); out.println(" "); out.println(" " + title + " "); out.println(" This is output from SimpleServlet."); out.println(" "); out.close(); } HTTP protocol, Java Servlets8

9 Servlets are Components Servlet container: creates instances of servlets (programmers don’t use operator new) controls life-cycle of servlets – invokes following methods for each servlet: init() – once upon initialization of a servlet as request are coming from clients: doGet(), doPost(), and others destroy() – once before servlet container shutdown Servlets have packaging format – WAR archives HTTP protocol, Java Servlets9

10 Web application packaging All files of a web application must be organized in the following way: In root directory (and its subdirectories) static content is placed (e.g.: images, CSS & JavaScript files, static HTML, etc.) Root directory must contain subdirectory named “WEB-INF”; its content will not be accessible to clients: /WEB-INF/web.xml – configuration file (deployment descriptor); /WEB-INF/classes/ – contains compiled Java classes; /WEB-INF/lib/ – contains libraries used by web application; Root directory and its content are put to Java archive with extension “.war” This WAR file can be deployed to any Servlet container HTTP protocol, Java Servlets10

11 HTTP protocol, Java Servlets11 Web application packaging

12 Mapping requests to servlets Request format: http[s]://host[:port] [/ ][/ ][/ ] Context path binds the request with specific web application. Binding can be configured in file: application.xml In NetBeans by default context path is the same as projects name Servlet path binds the request with specific web system‘s servlet. Binding is specified in file /WEB-INF/web.xml Parameters do not participate in binding – they just pass some parameters to some specific servlet HTTP protocol, Java Servlets12

13 Example of file web.xml catalog com.mycorp.CatalogServlet catalog /catalog/* 30 HTTP protocol, Java Servlets13 Servlet path


Download ppt "HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,"

Similar presentations


Ads by Google