Содержание

Слайд 2

Lesson goals Client-Server Static vs dynamic content Java Servlet Tomcat

Lesson goals

Client-Server
Static vs dynamic content
Java Servlet
Tomcat

Слайд 3

Client-Server architecture A person enters a URL string into the browser.

Client-Server architecture

A person enters a URL string into the browser.
The browser

generates HTTP-request.
The server receives a request, processes, and sends an HTTP response.
The browser receives a response and displays it.
Слайд 4

Client-Server architecture • Web-server for static content (popular): Apache, nginx, GWS,

Client-Server architecture

• Web-server for static content (popular): Apache, nginx, GWS, IIS,

...
• Software for dynamic content: CGI programs & modules, PHP AS,
MS IIS AS, Java EE, ...
Слайд 5

CGI - Common Gateway Interface Standard used to communicate of the

CGI - Common Gateway Interface

Standard used to communicate of the web

server with an external program for generating dynamic web content.
CGI-script - a program that works on the CGI-interface. Web-server is configured to redirect certain URL requests for CGI scripts.
Слайд 6

CGI - Common Gateway Interface Shortcomings of CGI technology: ● Start

CGI - Common Gateway Interface

Shortcomings of CGI technology:
● Start an OS

process for each request
● A lot of low-level coding
● No sharing resources across requests
● No session state
Слайд 7

Http Servlet service(HttpServletRequest, HttpServletResponse) doGet(HttpServletRequest, HttpServletResponse) doPost(HttpServletRequest, HttpServletResponse)

Http Servlet

service(HttpServletRequest, HttpServletResponse)
doGet(HttpServletRequest, HttpServletResponse)
doPost(HttpServletRequest, HttpServletResponse)

Слайд 8

Java Servlet @WebServlet(name = "helloServlet", urlPatterns = {"/hello"}) public class HelloServlet

Java Servlet

@WebServlet(name = "helloServlet", urlPatterns = {"/hello"}) public class HelloServlet extends HttpServlet

{ public void doGet( HttpServletRequest request, HttpServletResponse response ) throws IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("

Hello World

"); } }
Слайд 9

Java Servlet 4.0 HTTP /2 Request/Response multiplexing Stream Prioritization Server Push Upgrade from HTTP 1.1

Java Servlet 4.0

HTTP /2
Request/Response multiplexing
Stream Prioritization
Server Push
Upgrade from HTTP 1.1 

Слайд 10

Servlet Lifecycle Loads the servlet class Creates an instance of the

Servlet Lifecycle

Loads the servlet class
Creates an instance of the servlet class
Initializes

the servlet instance by calling the init() method
Does all work
When the container needs to remove the servlet, it calls the servlet’s destroy() method
Слайд 11

HttpServletRequest Container creates HttpServletRequest object and passes it as argument to

HttpServletRequest

Container creates HttpServletRequest object and passes it as argument to Servlet's

service methods (doGet, doPost, etc.)
HttpServletRequest provides client request info to Servlet.
Passed to the service(...) method, doGet (...), doPost (...), do ... ().
Слайд 12

HttpServletRequest Methods: - getHeader (String) - getParameter (String) - getInputStream ()

HttpServletRequest

Methods:
  - getHeader (String)
  - getParameter (String)
  - getInputStream ()
  -

getSession ()
  - getServletContext ()
- etc
Слайд 13

HttpServletResponse Container creates HttpServletResponse object and passes it to Servlet's service

HttpServletResponse

Container creates HttpServletResponse object and passes it to Servlet's service methods

(doGet, doPost, etc.)
HttpServletResponse assists a servlet in sending a response to the client.
As well as HttpServletRequest is passed in service(...) and do...(...)
Слайд 14

HttpServletResponse Methods: - addHeader (String, String) - setStatus (int) - sendError

HttpServletResponse

Methods:
  - addHeader (String, String)
  - setStatus (int)
  - sendError (int)
 

- sendRedirect (String)
  - setHeader (String, String)
  - getWriter ()
  - etc
Слайд 15

HttpSession HttpSession session = request.getSession(); Main methods: - invalidate () -

HttpSession

HttpSession session = request.getSession();
Main methods:
  - invalidate ()
  - getAttributeNames ()
 

- getAttribute (String)
  - removeAttribute (String)
  - setAttribute (String, Object)
- etc
Слайд 16

ServletContext Servlet uses ServletContext to communicate with its servlet container, access

ServletContext

Servlet uses ServletContext to communicate with its servlet container, access the

servlet container.
ServletContext sc = request.getServletContext();
Main methods:
  - getAttributeNames ()
  - getAttribute (String)
  - removeAttribute (String)
  - setAttribute (String, Object)
- etc
Слайд 17

Filter @WebFilter(urlPatterns = {"/*"}) public class HelloFilter implements Filter { public

Filter

@WebFilter(urlPatterns = {"/*"}) public class HelloFilter implements Filter { public void init(FilterConfig

cfg) { } public void doFilter(
ServletRequest req, ServletResponse resp, FilterChain chain ) throws ServletException, IOException { chain.doFilter(req, resp); } public void destroy() { } }
Слайд 18

Error Handling @WebServlet("/error") public class ErrorHandler extends HttpServlet { private static

Error Handling

@WebServlet("/error") public class ErrorHandler extends HttpServlet { private static final long

serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { resp.setContentType("text/html; charset=utf-8"); try (PrintWriter writer = resp.getWriter()) { writer.write("Error description"); writer.write("

Error description

"); writer.write("
    "); Arrays.asList( ERROR_STATUS_CODE, ERROR_EXCEPTION_TYPE, ERROR_MESSAGE) .forEach(e -> writer.write("
  • " + e + ":" + req.getAttribute(e) + "
  • ") ); writer.write("
"); writer.write(""); } } }
Слайд 19

Error Handling Web Project java.lang.RuntimeException /error

Error Handling

Web Project

java.lang.RuntimeException /error
Слайд 20

Web (servlet) container A Web server - component that interacts with

Web (servlet) container

A Web server - component that interacts with Java

Servlets.
Primary responsibilities are:
  - Management of servlets and their life cycle
- Mapping the URL to a specific servlet.
- Check access rights
Слайд 21

Java EE & Servlet (Web) Container Most popular Servlet (Web) Containers

Java EE & Servlet (Web) Container

Most popular Servlet (Web) Containers
• Apache

Tomcat
• Jetty
• JBoss Application Server (WildFly)
• …
Слайд 22

Apache Tomcat

Apache Tomcat

Слайд 23

WAR Web application ARchive - is a JAR file used to

WAR

Web application ARchive - is a JAR file used to distribute

a collection of JavaServer Pages, Java Servlets, Java classes, XML files, libraries, static web pages (HTML, JS, CSS, etc) and other resources…
Слайд 24

WAR

WAR

Слайд 25

Literature Building Java Web Applications Servlets (by Baeldung) Servlets (by Tutorialspoint)

Literature

Building Java Web Applications
Servlets (by Baeldung)
Servlets (by Tutorialspoint)
How to Install Apache

Tomcat
What is servlet container
Servlets (by Jetbrains)
Слайд 26

Homework Task 1 Implement a servlet with interface: `/session?action=[add/update/remove/invalidate]&name=...&value=...` 1. The

Homework Task 1

Implement a servlet with interface:
`/session?action=[add/update/remove/invalidate]&name=...&value=...`
1. The servlet generates

a form with input fields "action", "name", "value" and button "Submit". Below print is a list of all attributes of the user session.
action - depending on the value, the servlet adds, updates, or removes an attribute in/from a session;
name - the name of the attribute;
value - is the value of the attribute.
2. If any error happens – log and redirect user to custom error page
3. Add request blocking filter, when user browser is ‘Microsoft Edge’ – block request and show error page. Use
4. Add separate request logging filter. Log endpoints path and total execution time. Should measure all actions (even when request blocked)
5. Add separate request blocking filter. When today is weekend – deny all operations. Microsoft Edge users should never come here and be stopped by user-agent filter.
6. Attributes lists should not be shared between two browsers