4th lecture: jsp and such
DESCRIPTION
An overview of what comes.TRANSCRIPT
Web Technologies
4th Lecture: JSP and such
Δευτέρα, 10 Απριλίου 2023
URL (and many other UR*s)
• Uniform Resource Locators
Ports
• A 16-bit number that identifies a particular program on the server hardware.
• 0-1023 reserved• sys-admins control them
The apache file system
Missed last week
Static vs Dynamic content
Static vs. dynamic content
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.
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.
example
CGI (non-Java)
approach
Perl, C, Python, PHP, …
What’s wrong with CGI?
• Logical– Java is suppose to be the language of the internet
• Performance– Light-weight – Easy thread manipulation
• Productivity– J2EE
Servlet: prepare
• Build a directory tree
• Write the two files
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
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
Servlet: deploy (2)
Save your files in your file system
Servlet: deploy (3) and use
• Compile• Transfer to Tomcat’s file system• Start Tomcat• Click path in your browser
But …
• Stuff HTML as a string into java (println)?
• Why not stuff Java into HTML (or XML)?
Java Server Pages JSP
Apps developer
Web page designer
What is a Container?
What a Container offers?
• Help you to concentrate on your business logic than programming details– Communication– Lifecycle Management– Multithreading Support– Declarative Security– JSP Support
How the Container handles requests?
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>”); }}
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
Three different names for a servlet
Why?
• Flexibility• Security
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>
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.
Prepare Yourself
• http://tomcat.apache.org/tomcat-7.0-doc/appdev/
• Netbeans• Stackoverflow and friends