71
1 HTML Sites for self-study Tag reference (also Hebrew, Russian?) http://www.werbach.com/barebones/ download.html Tutorials (English) http://www.cwru.edu/6532680/help/ introHTML/toc.html http://www.utoronto.ca/webdocs/ HTMLdocs/NewHTML/htmlindex.html http://www.davesite.com/webstation/ html/index.shtml

1 HTML Sites for self-study –Tag reference (also Hebrew, Russian?) . html –Tutorials (English)

  • View
    239

  • Download
    2

Embed Size (px)

Citation preview

Page 1: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

1

HTML

Sites for self-study– Tag reference (also Hebrew, Russian?)

http://www.werbach.com/barebones/download.html

– Tutorials (English) http://www.cwru.edu/6532680/help/introHTML/

toc.html http://www.utoronto.ca/webdocs/HTMLdocs/

NewHTML/htmlindex.html http://www.davesite.com/webstation/html/

index.shtml

Page 2: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

2

HTML versions

Current version HTML 4.0 (W3C)– Primarily for compatibility– Tags that aren’t XML conformant deprecated

XHTML is recommended version– Concurrent with HTML 4.0– Conforms to XML rules– Use it.

Page 3: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

3

Points to remember

Case insensitive– XHTML: requires lower case

XHTML is “elements”– Containers with start/end tags– Some HTML tags don’t need close

<p>, <li>, etc.

– XHTML: always close tags XHTML attributes always in quotes

Page 4: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

4

HTML Doc Structure

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">

<HTML><HEAD><Title>Let's Play Collaboration</Title><META NAME="AUTHOR" CONTENT="Israel Gottlieb"/></HEAD>

<BODY>

<!--body goes here (does all the work of the page) --><!--these are comments in XHTML -->

</BODY>

</HTML>

Page 5: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

5

Lists

<UL> <LI>Monday <LI>Tuesday <LI>Wednesday <LI>Thursday <LI>Friday

</UL>

• Monday

• Tuesday

• Wednesday

• Thursday

• Friday

Page 6: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

6

Lists (cont’d)

<UL> <LI>Monday <LI>Tuesday <LI>Wednesday <UL>

<LI>6am - 9am <LI>9am - 12n <LI>12n - 3pm <LI>3pm - 6pm

</UL><LI>Thursday <LI>Friday

</UL>

• Monday

• Tuesday

• Wednesday o 6am - 9am

o 9am - 12n

o 12n - 3pm

o 3pm - 6pm

• Thursday

• Friday

Page 7: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

7

More Lists

<OL> <LI>Monday <LI>Tuesday <LI>Wednesday <LI>Thursday <LI>Friday

</OL>

1. Monday

2. Tuesday

3. Wednesday

4. Thursday

5. Friday

Page 8: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

8

More Lists

<DL> <DT>File: <DD>Operations on the open document <DT>Edit: <DD>Changes to the document text<DT>View: <DD>Options to display the document <DT>Insert: <DD>Adding external content

</DL>

Page 9: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

9

File:

Operations on the open document

Edit:

Changes to the document text

View:

Options to display the document

Insert:

Adding external content

More Lists

Page 10: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

10

Anchors & Images<A HREF="tcopy.html"> <IMG SRC="pix/warning.gif"> Unauthorized duplication is prohibited! </A>

Unauthorized duplication is prohibited!

Page 11: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

Tables

<TABLE border>

<TR> <TH>Course</TH> <TH>Like</TH> <TH>Don’t Like</TH> </TR>

<TR> <TD>Robotics</TD> <TD>82%</TD> <TD>18%</TD> </TR>

<TR> <TD>SW Engineering</TD> <TD>24%</TD> <TD>76%</TD> </TR>

<TR> <TD>Linear Systems</TD> <TD>34%</TD> <TD>66%</TD> </TR>

</TABLE>

Course Like Don’t Like

Robotics 82% 18%

SW Engineering 24% 76%

Linear Systems 34% 66%

Page 12: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

12

TablesTABLE border>

<TR> <TH>Course</TH> <TH>Like</TH> <TH>Don’t Like</TH>

</TR>

<TR> <TD>Robotics</TD> <TD>82%</TD> <TD>18%</TD>

</TR>

<TR> <TD>SW Engineering</TD> <TD>24%</TD> <TD>76%</TD>

</TR>

<TR> <TD>Linear Systems</TD> <TD>34%</TD> <TD>66%</TD>

</TR>

</TABLE>

Page 13: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

13

Tables – Global Attributes

<TABLE width="75%“ <!--percent of browser width--> border="8" cellpadding="10" cellspacing="10“

>

<Table align="right"> <!--Entire table to right margin-->

<TR align="right" valign="top"> <!--Default for cells in row-->

<TD align="right" valign="top"> <!--Override row default-->

Tables – Alignment

<TD width="150"> <!–-in pixels--> <TH width="50%"> <!–-in percent of table width-->

Tables – Cell Width

Page 14: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

14

Tables -- Spanning

<TH colspan="2"> <-- grows to right-->

<TD colspan="2">

<TD rowspan="2"> <!-- grows down-->

<TH colspan="2">

<TD rowspan="2">

Page 15: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

15

Example Web Page

OPEN BROWSER

Page 16: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

Example File

<BODY> <FONT FACE="Arial">

<p align="center"><H1>Let's Play Collaboration</H1></p>

<TABLE WIDTH="90%" cellspacing="0">

<caption align=bottom align=left><p align="left"><br/><br/><br/>Here's our first attempt at a Distributed, Web-based Collaboration support system. This is the Home page, and it includes a real-time list of current Collaborations. Enjoy! </p></caption>

<TR><th bgcolor="#FFC266"><i>Let's Get Going!</i></th><th align="center" valign="bottom"><font color="#C66D40">Click to Enter a Collaboration</font><HR/></th></TR>

Page 17: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

Example file (cont’d)<TR>

<td height="50" bgcolor="#85D9FF"><font size="3"><A HREF=search.parms.html>Search Collaborations</A></font></td>

<td align="center" rowspan="4"><font color="#FFC266" size="7">Collaboration Entries will List Here</font></td>

</TR>

<TR><td height="50" bgcolor="#85D9FF"><font size="3"><A HREF=new.parms.html>Create New Collaboration</A></font></td></TR>

Page 18: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

Example File (cont’d)

<TR><td height="50" bgcolor="#85D9FF"><font size="3">Test Link 1</font></td></TR>

<TR><td height="50" bgcolor="#85D9FF"><font size="3">Test Link 1</font></td></TR>

</TABLE>

</FONT></BODY>

Page 19: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

19

<HTML>

<HEAD>

<TITLE>Tsirkin Evgeny</TITLE>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-8">

</HEAD>

<FRAMESET COLS="150,*">

<FRAME SRC="menu.html" >

<FRAME SRC="local.html" name="window">

</FRAMESET>

</HTML>

Page 20: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

20

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>Evgeny Tsurkin</title>

</head>

<body bgcolor="#ffcc00" link="#0000ee" vlink="#551a8b" alink="#33cc00">

<center><font size="+1">

<u><i><b><font face="Helvetica, Arial, sans-serif"color="#505050">

Hi ,this is Evgeny's home page.

</center>

</font></b>

</i></u> To see my <a href="cv_r.htm">CV</a> please click here.

</body>

</html>

Page 21: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

21

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head> <title>Evgeny's Page</title>

</head>

<body text="#000000" bgcolor="#ffffff" link="#3333ff" vlink="#cc6600"

alink="#009900">

<i><font color="#009900"><cite><font face="Times New Roman, Times, serif" size="-1"><b>

<ul> <li><a href="passwd.htm" target="window">JCT Work</a> <li><a href="ps/PostScript/postscript.html" target="window">Postsrcipt</a>

<li><a href="emacs.html" target="window">Emacs Settings</a>

<li><a href="courses/index.htm" target="window">Courses</a>

</ul>

</b></font></strike></cite></font> </i><hr><br>

<center><font face="Times New Roman, Times, serif" size="-1"><b>

<a href="mailto:tsurkin(at)mail.jct.ac.il" align="center"> mail to Evgeny</a>

</b></font></center> <br>

</body>

</html>

Page 22: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

22

Page 23: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

23

Java OO

• Interfaces ,abstract classes and packages

• Exceptions

• Essential classes

Page 24: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

24

Interfaces and abstract classes

Page 25: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

25

•public

By default, a class can be used only by other classes in the same package. The public modifier declares that the class can be used by any class regardless of its package

•abstract

Declares that the class cannot be instantiated

•final

Declares that the class cannot be subclassed

•extends Super

The extends clause identifies Super as the superclass of the class, thereby inserting the class within the class hierarchy

•implements Interfaces

To declare that your class implements one or more interfaces, use the keyword implements followed by a comma-separated list of the names of the interfaces implemented by the class

Page 26: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

26

Abstract class

• abstract class className { . . . }

• No instantiation is allowed

AbstractTest.java:6: class AbstractTest is an abstract class.

It can't be instantiated.

new AbstractTest(); ^

1 error

• An abstract class may contain abstract methods

Page 27: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

27

abstract class GraphicObject {

int x, y; . . . void moveTo(int newX, int newY) { . . . }

abstract void draw();

}

class Circle extends GraphicObject {

void draw() { . . . }

}

class Rectangle extends GraphicObject {

void draw() { . . . }

}

Page 28: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

28

Final Class/Method• Class that cannot be subclassed

• Why?

Security Design

• Declaration:

final class myClass { . . . }

Chess.java:6: Can't subclass final classes: class myClass

class myClass extends myClass{ ^

1 error

• Final Method

Method that can’t be overridden by subclasses

class ChessAlgorithm { . . .

final void myMethod();

}

Page 29: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

29

Interface

• Definition: An interface is a named collection of method definitions (without implementations). An interface can also declare constants

• Interface versus Abstract class

1. An interface cannot implement any methods, whereas an abstract class can.

2. A class can implement many interfaces but can have only one superclass.

3. An interface is not part of the class hierarchy. Unrelated classes can implement the same interface

Page 30: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

30

•The interface body contains method declarations for all the methods included in the interface. All methods declared in an interface are implicitly public and abstract

• An interface can contain constant declarations in addition to method declarations. All constant values defined in an interface are implicitly public, static, and final.

Page 31: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

31

• A class that implements an interface adheres to the protocol defined by that interface. To declare a class that implements an interface, include an implements clause in the class declaration.

public class myClass extends Applet implements Interface

• When you define a new interface, you are defining a new reference data type.

public void method(Inteface object)

Page 32: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

32

Exception

• Definition: 

An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.

• Why?

Separating Error Handling Code from "Regular" Code

Propagating Errors Up the Call Stack

Grouping Error Types and Error Differentiation

Page 33: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

33

errorCodeType readFile { initialize errorCode = 0; open the file; if (theFileIsOpen) {

determine the length of the file;

if (gotTheFileLength) {

allocate that much memory;

if(gotEnoughMemory) {

read the file into memory;

if (readFailed) {

errorCode = -1;

}

} else {

errorCode = -2;

} } …

return errorCode;}

Page 34: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

34

readFile {

try {

open the file;

determine its size;

all ocate that much memory;

read the file into memory;

close the file;

} catch (fileOpenFailed) {

doSomething;

} catch (sizeDeterminationFailed) {

}

Page 35: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

35

method1 { errorCodeType error; error = call method2; if (error) doErrorProcessing; else proceed; }

errorCodeType method2 {

errorCodeType error; error = call method3; if (error) return error; else proceed;

}

errorCodeType method3 { errorCodeType error; error = call readFile; if (error) return error; else proceed;

}

Page 36: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

36

method1 {

try {

call method2;

} catch (exception) {

doErrorProcessing;

}

}

method2 throws exception {

call method3;

}

method3 throws exception {

call readFile;

}

Page 37: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

37

catch(InvalidIndexException e) { . . . }

catch (ArrayException e) { . . . }

catch (Exception e) { . . . }

Page 38: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

38

Throwing Exception

public Object pop() throws emptyStackException

{

Object obj;

if (size == 0) throw new EmptyStackException();

obj = objectAt(size - 1);

setObjectAt(size - 1, null);

size--;

return obj;

}

Page 39: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

39

Page 40: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

40

Page 41: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

41

Essential Classesimport java.io.*;

public class Copy {

public static void main(String[] args) throws IOException {

File inputFile = new File("farrago.txt");

File outputFile = new File("outagain.txt");

FileReader in = new FileReader(inputFile);

FileWriter out = new FileWriter(outputFile);

int c;

while ((c = in.read()) != -1) out.write(c);

in.close(); out.close();

}

}

Page 42: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

42

BufferedReader in = new BufferedReader(source);

...

PrintWriter out = new PrintWriter(target);

Buffered I/O

Page 43: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

43

Strings

• The String class provides for strings whose value will not change

If you are not going to modify the string – use it it is faster.

• The StringBuffer class provides for strings that will be modified .

You use string buffers when you know that the value of the character data will change.

Page 44: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

44

String palindrome = "Dot saw I was Tod"; StringBuffer sb = new StringBuffer("Drink Java!");

char[] helloArray = { 'h', 'e', 'l', 'l', 'o' };

String helloString = new String(helloArray);

System.out.println(helloString);

String palindrome = "Dot saw I was Tod";

int len = palindrome.length();

StringBuffer dest = new StringBuffer(len);

Page 45: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

45

class ReverseString {

public static String reverseIt(String source) {

int i, len = source.length();

StringBuffer dest = new StringBuffer(len);

for (i = (len - 1); i >= 0; i--) {

dest.append(source.charAt(i));

}

return dest.toString();

}

}

Modifying StringBuffers

Page 46: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

46

Forms and Servlets

<FORM method="post" action="/servlets/program1">

<INPUT type="text" name="ID" size="10" maxlength="8"> <!–- above input field has no visible label! -->

<P> Which of these courses have you taken?<BR> <INPUT type="checkbox" name="courses_taken" value="1">EE101<BR> <INPUT type="checkbox" name="courses_taken" value="2">CS201<BR> <INPUT type="checkbox" name="courses_taken" value="3">OP210<BR> </P>

</FORM> Which of these courses have you taken?

EE101

CS201

OP210

Page 47: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

47

Forms

<INPUT type="hidden" <!-- not visible in browser -->name="mailTarget" Value="[email protected]">

<SELECT name="access"> <!–- drop-down list --> <OPTION value="0"> modem </OPTION><OPTION value="1"> direct </OPTION></SELECT>

<INPUT type="submit"> <!–- send form data --> <INPUT type="reset"> <!–- reset form -->

Page 48: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

48

Servlet API Intro.

• Servlet intro.

• User Input

• Session

• Servlet context

Java Servlet Technology Documentationhttp://java.sun.com/products/servlet/docs.html

API Specificationshttp://java.sun.com/products/servlet/reference/api/index.html

Page 49: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

49

• A servlet is a Java technology based web component, managed by a container, that generates dynamic content.

• The servlet container is a part of a web server or application server that provides the network services over which requests and responses are sent, decodes MIME based requests, and formats MIME based responses. A servlet container also contains and manages servlets through their lifecycle.

Page 50: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

50

1. A client (e.g., a web browser) accesses a web server and makes an HTTP request.

2. The request is received by the web server and handed off to the servlet container.

The servlet container can be running in the same process as the host web server, in a different process on the same host, or on a different host from theweb server for which it processes requests.

3. The servlet container determines which servlet to invoke based on the configuration of its servlets, and calls it with objects representing the request and response.

4. The servlet uses the request object to find out who the remote user is, what HTTP POST parameters may have been sent as part of this request, and other relevant data. The servlet performs whatever logic it was programmed with, and generates data to send back to the client. It sends this data back to the client via the response object.

5. Once the servlet has finished processing the request, the servlet container ensures that the response is properly flushed, and returns control back to the host web server.

Page 51: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

51

The Servlet Interface

• The Servlet interface is the central abstraction of the servlet API.

• The two classes in the servlet API that implement the Servlet interface are GenericServlet and HttpServlet. For most purposes, developers will extend HttpServlet to implement their servlets.

• The basic Servlet interface defines a service method for handling client requests. This method is called for each request that the servlet container routes to an instance of a servlet.

• The handling of concurrent requests to a web application generally requires the web developer design servlets that can deal with multiple threads executing within the service method at a particular time.

Page 52: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

52

HTTP Specific Request Handling Methods

• doGet for handling HTTP GET requests

• doPost for handling HTTP POST requests

• doPut for handling HTTP PUT requests

• doDelete for handling HTTP DELETE requests

• doHead for handling HTTP HEAD requests

• doOptions for handling HTTP OPTIONS requests

• doTrace for handling HTTP TRACE requests

Page 53: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

53

Servlet Life Cycle• The life cycle is expressed in the API by the init, service, and destroy methods of the javax.servlet.Servlet interface

• The loading and instantiation can occur when the container is started, or delayed until the container determines the servlet is needed to service a request.

• The container initializes the servlet instance by calling the init method of the Servlet interface with a unique (per servlet declaration) object implementing the ServletConfig interface.

• When the servlet container determines that a servlet should be removed from service, it calls the destroy method of the Servlet interface to allow the servlet to release any resources it is using and save any persistent state.

Page 54: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

54

import java.io.*;import javax.servlet.*;import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {

public void doGet(HttpServletRequest request,

HttpServletResponse response)

throws IOException, ServletException {

response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<body>"); out.println("<head>"); out.println("<title>Hello World!</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Hello World!</h1>"); out.println("</body>"); out.println("</html>"); }}

Page 55: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

55

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

HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html");

GregorianCalendar cal=new GregorianCalendar(); PrintWriter out = response.getWriter(); out.println("<html>");

out.println("<body bgcolor=\"white\">");

out.println("This page was last refreshed on "+

cal.get(Calendar.DAY_OF_MONTH)+"/"+

cal.get(Calendar.MONTH)+"/"+

cal.get(Calendar.YEAR));

out.println("</body>"); }}

Page 56: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

56

public class CatalogServlet extends HttpServlet {    private BookDB bookDB;    public void init() throws ServletException {      bookDB = (BookDB)getServletContext().        getAttribute("bookDB");      if (bookDB == null) throw new        UnavailableException("Couldn't get database.");    } }

public void destroy() {    bookDB = null; }

Page 57: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

57

User Input<Form

method=“GET” action=“HTTP://www.mysite.co.il/filename.asp”

><ul><li><Input name=“first“ type=“text”/> <li><Input name=“last“ type=“text”/><li><Input name=“age“ type=“text”/></ul>

<SELECT name=“choice"> <OPTION>Yoav </OPTION><OPTION>Arye </OPTION<OPTION>Dani </OPTION>

</SELECT>

<Input type=“hidden” name=“status” value=“New”/><input type="submit"/></Form>

Page 58: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

58

Query String

HTTP://www.mysite.co.il/filename.asp?first=Mark&last=Markovich

&age=30&choice=Yoav&choice=Arye&choice=Dani&status=New

Page 59: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

59

public class Time extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html");

GregorianCalendar cal=new GregorianCalendar();

PrintWriter out = response.getWriter();

int hour = cal.get(Calendar.HOUR_OF_DAY);

out.println("<html>");

out.println("<body bgcolor=\"white\">");

if(hour < 12){

out.println("Good Morning! ");

}else{

out.println("Hello!");

}

out.println("</body>");

}

}

Page 60: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

60

public class Time extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("<html>");

out.println("<body bgcolor=\"white\">");

out.println("Hello"+request.getParameter("first")+

request.getParameter("last")+

"You are"+

request.getParameter("age")+

"years old!");

if(request.getParameter("status").equals(new String("New"))){

out.println("This is your first visit to our site!");

}

out.println("</body>"); }}

Page 61: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

61

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

HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html");

PrintWriter out = response.getWriter(); out.println("<html>");

out.println("<body bgcolor=\"white\">");

out.println("Hello. You made the following choices:");

String[] val = request.getParameterValues("choice");

for(int i=0;i < val.length;i++){

out.println(val[i]+"<br>");

}

out.println("</body>");

out.println("</html>"); }}

Page 62: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

62

User Input

“Post” method – use “doPost” instead of “doGet”

Post method uploads only from Form– Transmitted in Content section of HTTP Request

Get method query data is part of the URL– Can be sent to server without a form

Just type in URL to browser– Long query data may be truncated

(use form/Post) for long data

Page 63: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

63

Session Tracking Mechanisms

• Cookies . The container sends a cookie to the client. The client will then return the cookie on each subsequent request to the server, unambiguously associating the request with a session. The name of the session tracking cookie must be JSESSIONID.

• SSL Sessions. Secure Sockets Layer, the encryption technology used in the HTTPS protocol, has a mechanism built into it allowing multiple requests from a client to be unambiguously identified as being part of a session.

• URL Rewriting . URL rewriting involves adding data, a session id, to the URL path that is interpreted by the container to associate the request with a session.The session id must be encoded as a path parameter in the URL string. The name of the parameter must be jsessionid. Here is an example of a URL containing encoded path information:

http://www.myserver.com/catalog/index.html;jsessionid=1234

Page 64: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

64

• You can associate object-valued attributes with a session by name. Such attributes are accessible by any Web component that belongs to the same Web context and is handling a request that is part of the same session.

public class CashierServlet extends HttpServlet {   

public void doGet (HttpServletRequest request,

HttpServletResponse response)     throws ServletException, IOException {     

// Get the user's session and shopping cart     

HttpSession session = request.getSession();   

ShoppingCart cart =  (ShoppingCart)session.

         getAttribute("cart");     

...     

// Determine the total price of the user's books     

double total = cart.getTotal();

Page 65: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

65

To ensure that an active session is not timed out, you should periodically access the session via service methods because this resets the session's time-to-live counter.

When a particular client interaction is finished, you use the session's invalidate method to invalidate a session on the server side and remove any session data.

public class ReceiptServlet extends HttpServlet {   

public void doPost(HttpServletRequest request,           

HttpServletResponse response)           throws ServletException, IOException {     

// Get the user's session and shopping cart     

HttpSession session = request.getSession();     

// Payment received -- invalidate the session     

session.invalidate();    

 ...

Page 66: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

66

Session Tracking

out.println("<a href=\"" +

response.encodeURL(request.getContextPath() );

If cookies are turned off, the session is encoded in the Check Out URL as follows:

http://localhost:8080/bookstore1/cashier;jsessionid=c0o7fszeb1

If cookies are turned on, the URL is simply :

http://localhost:8080/bookstore1/cashier

Page 67: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

67

Hands on

• cd

• mkdir –m 755 javaserver

• cd javaserver

• mkdir –m 755 WEB-INF

• mkdir src

• mkdir classes

• edit wed.xml

• program/compile/put into the classes directory

Page 68: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

68

Web.xml <?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"><web-app> <display-name>Calendar example</display-name>

<description> Calendar Example ,text database in use.

</description>

<welcome-file-list>

<welcome-file>

index.html

</welcome-file>

</welcome-file-list>

<context-param> <param-name>reminderGeneratorUrl</param-name> <param-value>/calendar/reminderGenerator</param-value> </context-param>

Page 69: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

69

Web.xml (cont)

<servlet> <servlet-name>calendarGenerator</servlet-name> <servlet-class>calendarGenerator</servlet-class> </servlet>

<servlet-mapping>

<servlet-name>invoker</servlet-name>

<url-pattern>/servlet/*</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>calendarGenerator</servlet-name>

<url-pattern>/calendar</url-pattern>

</servlet-mapping>

</web-app>

Page 70: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

70

import java.io.*;import javax.servlet.*;import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {

public void doGet(HttpServletRequest request,

HttpServletResponse response)

throws IOException, ServletException {

response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<body>"); out.println("<head>"); out.println("<title>Hello World!</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Hello World!</h1>"); out.println("</body>"); out.println("</html>"); }}

Page 71: 1 HTML  Sites for self-study –Tag reference (also Hebrew, Russian?)  . html –Tutorials (English)

71

First assignment

Write a calculator application.

Number 1Number 2

+ -