chapter 4 servlets concept of servlets (what, why, and how) servlet api third-party tools to run...

27
Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML <form> tag with GET request HTML <form> tag with POST request Server-side Includes Servlet Chaining Example of Using Servlet with JDBC-ODBC Session Tracking Lab Exercises

Upload: tobias-underwood

Post on 22-Dec-2015

252 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET

Chapter 4 Servlets• Concept of Servlets (What, Why, and How)• Servlet API• Third-party tools to run servlets • Examples of Using Servlets• HTML <form> tag with GET request• HTML <form> tag with POST request• Server-side Includes• Servlet Chaining• Example of Using Servlet with JDBC-ODBC• Session Tracking• Lab Exercises

Page 2: Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET

Concept of Servlets • What is a servlet?

- a server-side software component, written in Java, that dynamically extends the functionality of a server- servlets execute on a java-enabled server- servlet API from Sun

• Why servlets?- client/server computing- dynamic Web page updating- Multithreading- Easy use in HTML and browsers

• How servlet works - Servlet lifecycle, see: servlet lifecycle and API.doc

Page 3: Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET

Servlet API• javax.servlet package

(in zipped file: servlet lifecycle and API.doc)• javax.servlet.http package

(in zipped file: HttpServlet chart and API.doc)• Download servlet API

- http://java.sun.com/products/servlet/index.html• Configuration of servlet API

- unzip the API package to the following directory:JDK_HOME/jre/lib/ext

(JDK_HOME is the directory in which the JDK is in the execution path in your computer)

Page 4: Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET

Servlet API (continue)• Set up the classpath

- method 1: (temporary)

at DOS prompt, enter:

set classpath=JDK_HOME\jre\lib\ext\

- method 2: (permanent)

add:

classpath=JDK_HOME\jre\lib\ext\

into autoexec.bat file and save the file(JDK_HOME is the directory in which the JDK is in the execution

path in your computer)

Page 5: Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET

Third-party tools to run servlets • Servlets must be executed by a server, usually a

Web server• There many third-party tools providing Web

servers for free to use• JRun is used throughout all examples in the text • The CD-ROM with the textbook has free copy of

JRun 3.0• Tomcat server is getting popularity and it’s free to

download: http://jakarta.apache.org/builds/jakarta-tomcat-4.0/release

• Configuration of Tomcat: http://www.cecs.csulb.edu/~artg/internet/tomcat.txt

Page 6: Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET

Third-party tools to run servlets (continue)

• Execute JRun default server and admin server

- click on start, programs, JRun 3.0, and select the server to run

• Login to the admin server

- username: admin

- password: admin

• More functionalities in JRun will be discussed later

Page 7: Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET

Examples of Using Servlets• A simple example using GenericServlet

- (textbook) p. 162 – Welcome.java• Compile the program to create a class file

- javac Welcome.java• Execute the servlet - method 1: run the class file by a client from a browser

a). Copy the class file into the following directory in JRun:

JRUN_HOME/servers/default/default-app/web-inf/classesb). Enter the following statement as URL in a browser:

http://IP_address:8100/servlet/file_name(JRUN_HOME is the directory in which the JRun is installed in your computer; IP_address is the computer where the class file is copied to, enter localhost if the same machine is used for testing)

Page 8: Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET

Examples of Using Servlets (continue)

• Execute the servlet (continue)

- method 2: run the class file by a client from a HTML using hyperlink (p. 163)

a). Copy and paste the class file into the following directory in JRun:

JRUN_HOME/servers/default/default-app/web-inf/classes

b). Create a HTML file using hyperlink as:

<a herf=http://IP_address:8100/servlet/file_name>Try a servlet</a>

c). Open the HTML file from a browser

Page 9: Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET

Examples of Using Servlets (continue)

• Execute the servlet (continue)

- method 3: run a HTML file stored in JRun to execute the servlet from a client computer

a). Copy the HTML to the following directory:

JRUN_HOME/servers/default/default-app

b). Copy the servlet class file into the following directory:

JRUN_HOME/servers/default/default-app/web-inf/classes

c). Enter the following URL in a client browser:

http://IP_address:8100/file_name.html

Page 10: Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET

HTML <form> tag with GET request

• <form> tag allows the client to submit the request or/and data to the server

• The submission then can be handled by doGet() method in HttpServlet

• Example of <form> tag with GET:

. <form action=“http://IP_address:8100/servlet/file_name” method=GET>

.

</form>

.

Page 11: Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET

HTML <form> tag with GET request (continue)

• Example of doGet() method of HttpServlet:

.

public void doGet(HttpServletRequest req, HttpServletRequest resp) throws ServletException, IOException

{ .

String message = req.getParameter(“Order”);

PrintWriter out=resp.setContentType(“text/html”);

out.println(“<strong>display ”+message+“</strong>”);

.

}

.

• Complete example (textbook) p. 165-166

Page 12: Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET

HTML <form> tag with POST request

• We may use either GET request or POST request to submit request or/and data to a server

• GET request is handled by doGet() of HttpServlet• GET request allows browser to include the data in

the URL window• POST request is handled by doPost() of HttpServlet• POST does not allow browser to include the data in

the URL window • POST is more secure than GET and can handle

more data

Page 13: Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET

HTML <form> tag with POST request(continue)

• Example of <form> tag with POST:

. <form action=“http://IP_address:8100/servlet/file_name” method=POST>

.

</form>

.

Page 14: Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET

HTML <form> tag with POST request(continue)

• Example of doGet() method of HttpServlet:public void doPost(HttpServletRequest req, HttpServletRequest resp) throws ServletException, IOException

{ . String message = req.getParameter(“Order”); PrintWriter out=resp.setContentType(“text/html”); out.println(“<strong>display ”+message+“</strong>”); .}

• Complete example (textbook) p. 168-170• More reading in HTTPServlet, see Servlet Chart

and Methods.doc

Page 15: Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET

Server-side Includes

• What is server-side includes?

- SSIs allow servlet calls to be embedded in a HTML file using a special <servlet> tag

- by convention, the file uses the .shtml file extension

- JRun uses the .shtml file extension

• Why server-side includes?

- Embed the response to the client’s Web page

- focus on the content by the servlet

Page 16: Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET

Server-side Includes (continue)

• Example of using <servlet> tag

.

<servlet code= classFile_name codebase=“http://IP_address:8100/servlet”>

<param name=data value=9>

.

• The response from the servlet will be embedded into the HTML file and displayed on the Web page

• Complete example (textbook) p. 172-174

Page 17: Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET

Servlet Chaining

• What is servlet chaining?

- is the process of passing the output of one servlet to the input of another

- Chart of servlet chaining (in zipped file: servlet chaining.doc)

• Why servlet chaining?

- to filter/alter a servlet’s output

- pipelining process

Page 18: Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET

Servlet Chaining (continue)

• Two different methods to implement servlet chaining:

- chain the servlets in the URL

- use MIME filter (Multipurpose Internet Mail Extensions)

• Example of chaining the servlets in the URL (textbook) p. 175

http://IP_address:8100/servlet/LuckyWelcome, LuckTag

Page 19: Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET

Servlet Chaining (continue)• Example of using MIME filter in JRun

- Configuration steps:a). Make sure JRun admin server is runningb). If login is needed, use admin as both username and passwordc). Click on JRun Default Serverd). Click on Web Applicationse). Click on Default User Applicationf). Click on MIME Chainingg). Click on Edit (use of textbook example as follows)h). Enter custom/lucky in MIME Typei). Enter LuckyTag in Servlet Invokedj). Stop and start the Default Server

• Complete example (textbook) p. 175-178• Servlet filtering using Tomcat:

http://www.cecs.csulb.edu/~artg/internet/ch4changes.txt

Page 20: Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET

Example of Using Servlet with JDBC-ODBC

• Concept of three-tied architecture using servlet

• Complete example (textbook) p. 180-182

request

response

Client Server

JDBC-ODBC

Database

Page 21: Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET

Session Tracking• What is a session?

- is a persistent network connection b/w a client and a server that facilitates the exchange of data

What is a HTTP session?- Since HTTP is connectionless and stateless protocol, a HTTP session is a virtual session- a virtual session is a series of associate requests in which the client can be uniquely identified by a session ID/data returned to the server by the client

• Why session tracking?- to keep track of clients from one connection to another

Page 22: Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET

Session Tracking (continue)

• Implementations of session tracking- method 1: using cookie (Set-Cookie in HTTP Header from a client and method setHeader() in HttpServletResponse from the server), will be discussed later

- method 2: using HttpSession object• Methods in HttpSession Interface see: HttpSeesion API.doc• Complete example (textbook) p. 183-186• Author’s updates: http://www.cecs.csulb.edu/~artg

/internet/advanced.html

Page 23: Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET

Concept of JSP• What is JSP?

- stands for JavaServer Pages

- is a simple Java-based specification for building dynamic Web sites

- is built on top of Java servlets. All JSP pages are automatically compiled into a servlet by the JSP container

• Why JSP?

- Simplify the use of servlets

- Client/server applications

- Integration ability

Page 24: Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET

Using JSP Tags in HTML• We use comment tag

<!-- comment statements -->

to write the comments

• We use JSP page directive tag

<%@ page import=“package_name”%>

to import a Java package in HTML

• We can use JSP expression tag

<% = any Java expression %>

to write any Java expression in HTML

• We use JSP statement tag

<% any Java statement(s) %>

to write Java statements in HTML

• So, we can handle all kind of requests from a client in HTML

Page 25: Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET

Simple Example of JSP with HTML GET method

• (p. 188) Example 4.10 GetOrder.jsp- Comparing JSP version with servlet version (p. 188) GetOrder.jsp and (p.166) GetOrder.java

- Comparing HTML files in these two versions (p. 188) GetJspOrder.html and (p. 165) GetOrder.html

• Steps to execute the application:1. Copy GetJspOrder.html and GetOrder.jsp into the following JRun directory:JRUN_HOME\servers\default\default-app2. Browser the URL:http://IP_address:8100/GetJspOrder.html

Page 26: Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET

Using JSP Handling HTML POST Method

• Example handling POST method in HTML– (p. 190) Example 4.11 – PostOrder.jsp– (p. 190) PostJspOrder.html

• Steps to execute the application:

1. Copy PostJspOrder.html and PostOrder.jsp into the following JRun directory:

JRUN_HOME\servers\default\default-app

2. Browser the URL:

http://IP_address:8100/PostJspOrder.html

Page 27: Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET

In-class Exercises

- Do TRY-IT-YOURSELF 3, 7, 9, 11,13, and 15

- textbook (p. 195) 3, 5, 7, and 9