1 web applications – the basics. 2 unzipped tomcat folder
Post on 21-Dec-2015
229 views
TRANSCRIPT
![Page 1: 1 Web Applications – The Basics. 2 Unzipped Tomcat Folder](https://reader035.vdocuments.net/reader035/viewer/2022081513/56649d5c5503460f94a3b287/html5/thumbnails/1.jpg)
1
Web Applications – The Basics
![Page 2: 1 Web Applications – The Basics. 2 Unzipped Tomcat Folder](https://reader035.vdocuments.net/reader035/viewer/2022081513/56649d5c5503460f94a3b287/html5/thumbnails/2.jpg)
2
Unzipped Tomcat Folder
![Page 3: 1 Web Applications – The Basics. 2 Unzipped Tomcat Folder](https://reader035.vdocuments.net/reader035/viewer/2022081513/56649d5c5503460f94a3b287/html5/thumbnails/3.jpg)
3
Environment variables
• CATALINA_HOME=d:\tools\tomcat
• PATH– Add d:\tools\tomcat\bin
• startup.bat • shutdown.bat
![Page 4: 1 Web Applications – The Basics. 2 Unzipped Tomcat Folder](https://reader035.vdocuments.net/reader035/viewer/2022081513/56649d5c5503460f94a3b287/html5/thumbnails/4.jpg)
4
Default Page
![Page 5: 1 Web Applications – The Basics. 2 Unzipped Tomcat Folder](https://reader035.vdocuments.net/reader035/viewer/2022081513/56649d5c5503460f94a3b287/html5/thumbnails/5.jpg)
5
http://localhost:8080/abc/f1.txt
![Page 6: 1 Web Applications – The Basics. 2 Unzipped Tomcat Folder](https://reader035.vdocuments.net/reader035/viewer/2022081513/56649d5c5503460f94a3b287/html5/thumbnails/6.jpg)
6
Text File: webapps/abc/f1.txtHi There
![Page 7: 1 Web Applications – The Basics. 2 Unzipped Tomcat Folder](https://reader035.vdocuments.net/reader035/viewer/2022081513/56649d5c5503460f94a3b287/html5/thumbnails/7.jpg)
7
f2.html
![Page 8: 1 Web Applications – The Basics. 2 Unzipped Tomcat Folder](https://reader035.vdocuments.net/reader035/viewer/2022081513/56649d5c5503460f94a3b287/html5/thumbnails/8.jpg)
8
HTML file: webapps/abc/f2.html<html><body><h2>This is a headline</h2>Some text <b>bold</b>, <i>italics</i>, <u>underline</u>
<p>A new paragraph, with a <a href="f1.txt">link</a> to the first page.</p>
</body></html>
![Page 9: 1 Web Applications – The Basics. 2 Unzipped Tomcat Folder](https://reader035.vdocuments.net/reader035/viewer/2022081513/56649d5c5503460f94a3b287/html5/thumbnails/9.jpg)
9
f3.html
![Page 10: 1 Web Applications – The Basics. 2 Unzipped Tomcat Folder](https://reader035.vdocuments.net/reader035/viewer/2022081513/56649d5c5503460f94a3b287/html5/thumbnails/10.jpg)
10
Javascript: webapps/abc/f3.html<html><body>
<h2 id="xy256">Click me!</h2>some text...
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
</script>
<script type="text/javascript"> $(document).ready(function() { $ ("#xy256").click(function() { alert("Current time is " + new Date()); } ); } );</script>
</body></html>
![Page 11: 1 Web Applications – The Basics. 2 Unzipped Tomcat Folder](https://reader035.vdocuments.net/reader035/viewer/2022081513/56649d5c5503460f94a3b287/html5/thumbnails/11.jpg)
11
f4.html
![Page 12: 1 Web Applications – The Basics. 2 Unzipped Tomcat Folder](https://reader035.vdocuments.net/reader035/viewer/2022081513/56649d5c5503460f94a3b287/html5/thumbnails/12.jpg)
12
More Javascript: f4.html<html><body>
<p>N:<input id="input" type="text"/></p><p><button id="compute" type="button">Compute!</button></p><p id="output"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
</script>
<script type="text/javascript"> $(document).ready(function() { $("#compute").click(function() { var v = $("input").val(); v = parseInt(v)*2; $("#output").html("N*2=<b>" + v + "</b>"); }); });</script>
</body></html>
![Page 13: 1 Web Applications – The Basics. 2 Unzipped Tomcat Folder](https://reader035.vdocuments.net/reader035/viewer/2022081513/56649d5c5503460f94a3b287/html5/thumbnails/13.jpg)
13
Sending Data
![Page 14: 1 Web Applications – The Basics. 2 Unzipped Tomcat Folder](https://reader035.vdocuments.net/reader035/viewer/2022081513/56649d5c5503460f94a3b287/html5/thumbnails/14.jpg)
14
f5.html<html><body>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
</script>
<script type="text/javascript"> $(document).ready(function() { $ ("#compute").click(function() { var v = $ ("input").val(); location.assign(location.protocol + "//"
+ location.host + "/abc/f6.html?input=" + v); } ); } );</script>
<p>What is your name? <input id="input" type="text"/></p><p><button type="button" id="compute">Welcome!</button></p>
</body></html>
![Page 15: 1 Web Applications – The Basics. 2 Unzipped Tomcat Folder](https://reader035.vdocuments.net/reader035/viewer/2022081513/56649d5c5503460f94a3b287/html5/thumbnails/15.jpg)
15
f6.html<html><body>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
</script>
<script type="text/javascript"> $(document).ready(function() { // Cuation: Hack ahead. // Use a standard parameter parsing library instead var s = "input="; var i = location.search.indexOf(s); if(i >= 0) { var input = location.search.substring(i + s.length); $("#output").html(input); } });</script>
<h2>Nice to see you, <span id="output"/></h2></body></html>
![Page 16: 1 Web Applications – The Basics. 2 Unzipped Tomcat Folder](https://reader035.vdocuments.net/reader035/viewer/2022081513/56649d5c5503460f94a3b287/html5/thumbnails/16.jpg)
16
So far, we saw…
• Static (hard coded) pages
• Some HTML elements
• Reactive pages – Thanks to Javascript
• Sending data between pages
![Page 17: 1 Web Applications – The Basics. 2 Unzipped Tomcat Folder](https://reader035.vdocuments.net/reader035/viewer/2022081513/56649d5c5503460f94a3b287/html5/thumbnails/17.jpg)
17
Dynamic Server Content: d1.html
![Page 18: 1 Web Applications – The Basics. 2 Unzipped Tomcat Folder](https://reader035.vdocuments.net/reader035/viewer/2022081513/56649d5c5503460f94a3b287/html5/thumbnails/18.jpg)
18
webapps/abc/WEB-INF/web.xml<?xml version="1.0" encoding="ISO-8859-1"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<servlet> <servlet-name>S1</servlet-name> <servlet-class>p1.S1</servlet-class> </servlet>
<servlet-mapping> <servlet-name>S1</servlet-name> <url-pattern>/d1.html</url-pattern> </servlet-mapping>
</web-app>
![Page 19: 1 Web Applications – The Basics. 2 Unzipped Tomcat Folder](https://reader035.vdocuments.net/reader035/viewer/2022081513/56649d5c5503460f94a3b287/html5/thumbnails/19.jpg)
19
Source code: S1.javapackage p1;
import java.io.IOException;import java.util.Date;
import javax.servlet.http.*;
//// IMPORTANT: Needs servlet-api.jar in order to compile!// Can be found at <tomcat-dir>/lib//
public class S1 extends HttpServlet { private static final long serialVersionUID = -1224125312164793742L;
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { resp.setContentType("text/html"); resp.setCharacterEncoding("UTF-8"); resp.getWriter().println("<html><body>Current time " + new Date() + "</body></html>"); }}
![Page 20: 1 Web Applications – The Basics. 2 Unzipped Tomcat Folder](https://reader035.vdocuments.net/reader035/viewer/2022081513/56649d5c5503460f94a3b287/html5/thumbnails/20.jpg)
20
webapps/abc/WEB-INF/classes
![Page 21: 1 Web Applications – The Basics. 2 Unzipped Tomcat Folder](https://reader035.vdocuments.net/reader035/viewer/2022081513/56649d5c5503460f94a3b287/html5/thumbnails/21.jpg)
21
Sending Data – to the Server
![Page 22: 1 Web Applications – The Basics. 2 Unzipped Tomcat Folder](https://reader035.vdocuments.net/reader035/viewer/2022081513/56649d5c5503460f94a3b287/html5/thumbnails/22.jpg)
22webapps/abc/WEB-INF/web.xml<?xml version="1.0" encoding="ISO-8859-1"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<servlet> <servlet-name>S1</servlet-name> <servlet-class>p1.S1</servlet-class> </servlet>
<servlet-mapping> <servlet-name>S1</servlet-name> <url-pattern>/d1.html</url-pattern> </servlet-mapping>
<servlet> <servlet-name>S2</servlet-name> <servlet-class>p1.S2</servlet-class> </servlet>
<servlet-mapping> <servlet-name>S2</servlet-name> <url-pattern>/d2.html</url-pattern> </servlet-mapping></web-app>
![Page 23: 1 Web Applications – The Basics. 2 Unzipped Tomcat Folder](https://reader035.vdocuments.net/reader035/viewer/2022081513/56649d5c5503460f94a3b287/html5/thumbnails/23.jpg)
23
Source code: S2.javapackage p1;
import java.io.IOException;import javax.servlet.http.*;
public class S2 extends HttpServlet { private static final long serialVersionUID = -1224125312164793742L;
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { resp.setContentType("text/html"); resp.setCharacterEncoding("UTF-8"); resp.getWriter().println("<html><body>Nice to see you, " + req.getParameter("input") + "</body></html>"); }}
![Page 24: 1 Web Applications – The Basics. 2 Unzipped Tomcat Folder](https://reader035.vdocuments.net/reader035/viewer/2022081513/56649d5c5503460f94a3b287/html5/thumbnails/24.jpg)
24
(copying S2.class to classes)
![Page 25: 1 Web Applications – The Basics. 2 Unzipped Tomcat Folder](https://reader035.vdocuments.net/reader035/viewer/2022081513/56649d5c5503460f94a3b287/html5/thumbnails/25.jpg)
25
Comments• Changes in classes, web.xml require a restart of tomcat
• IDE can “talk” to the server– Debug a servlet as it runs– Download the necessary plugin(s)
• Automate the (development) deployment process
• This is the most primitive way to work with Tomcat– Frameworks will ease your life (Spring, Grails, …)
• Extending a servlet makes your life difficult– Testing, debugging, resusing– Delegate to a POJO
• Persistency: Files will not work– Serialization is evil– Files get corrupted– SQL simplifies data manipulation
![Page 26: 1 Web Applications – The Basics. 2 Unzipped Tomcat Folder](https://reader035.vdocuments.net/reader035/viewer/2022081513/56649d5c5503460f94a3b287/html5/thumbnails/26.jpg)
26
Comments (cont.)
• Cross-browser incompatibility– Use a good Javascript library from day one– JQuery, Dojo, Prototype, …
• Distributed programming– Two processes: Server (Java), Client (Javascript)– No shared heap– IDs are used as pointers
• Additional techniques: CSS, Ajax, …
![Page 27: 1 Web Applications – The Basics. 2 Unzipped Tomcat Folder](https://reader035.vdocuments.net/reader035/viewer/2022081513/56649d5c5503460f94a3b287/html5/thumbnails/27.jpg)
27
An excellent starting point
“Developing a Spring Framework MVC application step-by-step”
http://static.springsource.org/docs/Spring-MVC-step-by-step