Download - 4th Lecture: JSP and such

Transcript
Page 1: 4th Lecture:  JSP and such

Web Technologies

4th Lecture: JSP and such

Δευτέρα, 10 Απριλίου 2023

Page 2: 4th Lecture:  JSP and such

Last Week

• HTML & CSS• TCP/IP, DNS, URL, URI• HTTP and such

Page 3: 4th Lecture:  JSP and such
Page 4: 4th Lecture:  JSP and such

URL (and many other UR*s)

• Uniform Resource Locators

Page 5: 4th Lecture:  JSP and such

Ports

• A 16-bit number that identifies a particular program on the server hardware.

• 0-1023 reserved• sys-admins control them

Page 6: 4th Lecture:  JSP and such

The apache file system

Page 7: 4th Lecture:  JSP and such

Missedlast

week

Page 8: 4th Lecture:  JSP and such

Missed last week

Page 9: 4th Lecture:  JSP and such

Static vs Dynamic content

Page 10: 4th Lecture:  JSP and such

Static vs. dynamic content

Page 11: 4th Lecture:  JSP and such

Dynamic (deep) Web

• Dynamic pages don’t exist before the request comes in.

• It’s like making an HTML page out of air.– request comes in, – helper app “writes” the HTML, and – web server gets it back to the client.

Page 12: 4th Lecture:  JSP and such

Common Gateway Interface (CGI)

• a standard method for web servers software to delegate the generation of web pages to executable files.

• Such files are known as CGI scripts – they are programs, • often stand-alone applications, • usually written in a scripting language.

Page 13: 4th Lecture:  JSP and such

example

Page 14: 4th Lecture:  JSP and such

CGI (non-Java)

approach

Perl, C, Python, PHP, …

Page 15: 4th Lecture:  JSP and such

What’s wrong with CGI?

• Logical– Java is suppose to be the language of the internet

• Performance– Light-weight – Easy thread manipulation

• Productivity– J2EE

Page 16: 4th Lecture:  JSP and such

Servlet: prepare

• Build a directory tree

• Write the two files

Page 17: 4th Lecture:  JSP and such

Servlet: Writeimport javax.servlet.*;import javax.servlet.http.*;import java.io.*;

public class Ch1Servlet extends HttpServlet { public void doGet(HttpServletRequest request,

HttpServletResponse response) throws IOException {

PrintWriter out = response.getWriter(); java.util.Date today = new java.util.Date(); out.println(“<html> “ + “<body>” + “<h1 align=center>HF\’s Chapter1 Servlet</h1>” + “<br>” + today + “</body>” + “</html>”); }}

Save it to Servlet.java

Page 18: 4th Lecture:  JSP and such

Servlet: deploy (1)<?xml version=”1.0” encoding=”ISO-8851-1” ?><web-app xmlns=”http://java.sun.com/xml/ns/j2ee” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd” version=”2.4”> <servlet> <servlet-name>Servlet</servlet-name> <servlet-class>Ch1Servlet</servlet-class> </servlet>

<servlet-mapping> <servlet-name>Servlet</servlet-name> <url-pattern>/Serv1</url-pattern> </servlet-mapping></web-app>

Save this deployment descriptor

(DD) to web.xml

Page 19: 4th Lecture:  JSP and such

Servlet: deploy (2)

Save your files in your file system

Page 20: 4th Lecture:  JSP and such

Servlet: deploy (3) and use

• Compile• Transfer to Tomcat’s file system• Start Tomcat• Click path in your browser

Page 21: 4th Lecture:  JSP and such

But …

• Stuff HTML as a string into java (println)?

• Why not stuff Java into HTML (or XML)?

Page 22: 4th Lecture:  JSP and such

Java Server Pages JSP

Apps developer

Web page designer

Page 23: 4th Lecture:  JSP and such

What is a Container?

Page 24: 4th Lecture:  JSP and such

What a Container offers?

• Help you to concentrate on your business logic than programming details– Communication– Lifecycle Management– Multithreading Support– Declarative Security– JSP Support

Page 25: 4th Lecture:  JSP and such

How the Container handles requests?

Page 26: 4th Lecture:  JSP and such

Back to the code (servlet.java) import javax.servlet.*;import javax.servlet.http.*;import java.io.*;

public class Ch1Servlet extends HttpServlet { public void doGet(HttpServletRequest request,

HttpServletResponse response) throws IOException {

PrintWriter out = response.getWriter(); java.util.Date today = new java.util.Date(); out.println(“<html> “ + “<body>” + “<h1 align=center>HF\’s Chapter1 Servlet</h1>” + “<br>” + today + “</body>” + “</html>”); }}

Page 27: 4th Lecture:  JSP and such

Questions

• Where service() comes from?• How the container finds the correct servlet?• Does the URL define where exactly the servlet

is?

• Possible answers– The user gives precise URL– The container’s tool does the mapping– We store the mapping to the properties table

Page 28: 4th Lecture:  JSP and such

Three different names for a servlet

Page 29: 4th Lecture:  JSP and such

Why?

• Flexibility• Security

Page 30: 4th Lecture:  JSP and such

Map URLs top Servlet with DD

Deployment Descriptor (DD) has 2 elements

<web-app ...>

<servlet> <servlet-name>Internal name 1</servlet-name> <servlet-class>foo.Servlet1</servlet-class> </servlet> <servlet> <servlet-name>Internal name 2</servlet-name> <servlet-class>foo.Servlet2</servlet-class> </servlet>

<servlet-mapping> <servlet-name>Internal name 1</servlet-name> <url-pattern>/Public1</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Internal name 2</servlet-name> <url-pattern>/Public2</url-pattern> </servlet-mapping>

</web-app>

Page 31: 4th Lecture:  JSP and such

Manipulate without touching with DD

• Minimizes touching source code that has already been tested.

• fine-tune your app’s capabilities, even if you don’t have the source code.

• adapt your application to different resources (like databases), without having to recompile and test any code.

• maintain dynamic security info like access control lists and security roles.

• non-programmers may modify and deploy your web applications.

Page 32: 4th Lecture:  JSP and such

Prepare Yourself

• http://tomcat.apache.org/tomcat-7.0-doc/appdev/

• Netbeans• Stackoverflow and friends