27
Knowledge Sharing

Knowledge Sharing : Java Servlet

Embed Size (px)

DESCRIPTION

Knowledge sharing on Java Servlet material.

Citation preview

Page 1: Knowledge Sharing : Java Servlet

Knowledge Sharing

Page 2: Knowledge Sharing : Java Servlet

Agenda What is Servlet? Servlet hierarchy & lifecycle Servlet program structure Deploying servlets on Tomcat HTTP Servlets and HTTP request

methods Understanding servlet API Responding to requests Accessing form input data Working with header fields URL redirecting

Page 3: Knowledge Sharing : Java Servlet

What is servlet? A Java class which conforms to the

Java Servlet API, a protocol by which a Java class may respond to HTTP requests.

A Servlet is an object that receives a request and generates a response based on that request

Servlets are the Java counterpart to non-Java dynamic Web content technologies such as CGI and ASP.NET(http://en.wikipedia.org/wiki/Java_Servlet)

Page 4: Knowledge Sharing : Java Servlet

Advantages of Servlets Faster than CGI scripts because use a

different process model. Use standard API that is supported by

many Web servers. Have all of the advantages of the

Java languages, including ease of development and platform independence.

Can access the large set of APIs available for the Java platform. (http://java.sun.com)

Page 5: Knowledge Sharing : Java Servlet

Servlet V.S. CGI A servlet stays in memory between

requests. A CGI program needs to be loaded and started for each CGI request.

A servlet doesn’t run in a separate process. This removes the overhead of creating a new process for each request which CGI does.

A servlet allows each request to be handled by a separate Java thread within the web server process (same amount of threads as request but there only be one copy of the servlet class created in memory).

(http://www.novocode.com/doc/servlet-essentials/chapter1.html)

Page 6: Knowledge Sharing : Java Servlet

Servlet Hierarchy

Servlets(javax.servlet.Servlet

interface)

Generic Servlet

(javax.servlet.GenericServlet)

HTTP Servlet(javax.servlet.http.HttpSe

rvlet)

MyServlet

Page 7: Knowledge Sharing : Java Servlet

Servlet Lifecycle

Page 8: Knowledge Sharing : Java Servlet

Servlet Lifecycle (cont.)

Page 9: Knowledge Sharing : Java Servlet

Servlet Program Structure

Page 10: Knowledge Sharing : Java Servlet

[Screencast] Create simple servlet

Page 11: Knowledge Sharing : Java Servlet

[Showcase] Servlet Lifecycle

Page 12: Knowledge Sharing : Java Servlet

[Showcase] Servlet Lifecycle (cont.)

Page 13: Knowledge Sharing : Java Servlet

[Showcase] Servlet Lifecycle (cont.)

Page 14: Knowledge Sharing : Java Servlet

Deploying Servlets on Tomcat Create a servlet Compile the servlet :

No different from compiling java program (javac OurServlet.java)

javax.servlet.* and javax.servlet.http.* must be added in Classpath.

Create web application folder under webapps folder in Tomcat (web container).

Create WEB-INF folder under web application folder.

http://www.roseindia.net/servlets/introductiontoconfigrationservlet.shtml

Page 15: Knowledge Sharing : Java Servlet

Deploying Servlets on Tomcat (cont) Create ‘web.xml’ file and ‘classes’

folder under WEB-INF. Copy the servlet class file into

‘classes’ folder Edit web.xml to include servlet’s

name and url pattern.

http://www.roseindia.net/servlets/introductiontoconfigrationservlet.shtml

Page 16: Knowledge Sharing : Java Servlet

Deploying Servlets on Tomcat (cont) Run Tomcat server and then execute

the Servlet

http://www.roseindia.net/servlets/introductiontoconfigrationservlet.shtml

Page 17: Knowledge Sharing : Java Servlet

HTTP Servlet and HTTP request method

Page 18: Knowledge Sharing : Java Servlet

HTTP Servlet and HTTP request method (cont.) HTTP is a request-response oriented

protocol. An HTTP request consists of :

Request method (GET, HEAD, PUT, POST, DELETE, OPTIONS and TRACE)

URI Header fields Body

An HTTP response contains : Result code Header fields and body

http://www.novocode.com/doc/servlet-essentials/chapter1.html

Page 19: Knowledge Sharing : Java Servlet

Understanding Servlet API Packages in the Java Servlet API 2.1

http://java.sun.com/products/servlet/2.1/servlet-2.1.pdf

Page 20: Knowledge Sharing : Java Servlet

Understanding Servlet API (cont) Packages in the Java Servlet API 2.1

http://java.sun.com/products/servlet/2.1/servlet-2.1.pdf

Page 21: Knowledge Sharing : Java Servlet

Request Header

Accept Accept-charset Accept-

encoding Accept-

language Authorization Connection Content-length Cookie

The most common headers :

From Host If-modified-since Pragma Referer User-agent UA-Pixels, UA-

color, UA-OS, UA-CPU

http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Request-Headers.html

Page 22: Knowledge Sharing : Java Servlet

Request Header (cont.)

Call the getHeader() method of the HttpServletRequest, which returns a String if the header was supplied on this request, null otherwise.

Call the getHeaderNames() to get an Enumeration of all header names received on the particular request.

Reading request header from Servlets :

http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Request-Headers.html

Page 23: Knowledge Sharing : Java Servlet

Accessing form input data

Call getParameter() method of the HttpServletRequest, supplying the parameter name (case sensitive) as an argument.

Call getParameterValues() method to get the parameter that probably have more than one value.

Call getParameterNames() to get full list of all parameters received on the particular request.

http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Form-Data.html

Page 24: Knowledge Sharing : Java Servlet

URL Redirecting

Using sendRedirect() method of HttpServletResponse class.

Using forward() method of RequestDispatcher class.

http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Form-Data.html

Page 25: Knowledge Sharing : Java Servlet

URL Redirecting (cont.)

sendRedirect() It is a new request from the client, and the way to pass data is through the session or with web parameters (url?name=value)

RequestDispatcherThe target servlet/JSP receives the same request/response objects as the original servlet/JSP. Therefore, can pass data between them using request.setAttribute().

http://www.theserverside.com/discussions/thread.tss?thread_id=26425

Page 26: Knowledge Sharing : Java Servlet

URL Redirecting (cont.)

sendRedirect() It will updates the browser history.

RequestDispatcherIf use RequestDispatcher to forward from Servlet-2 to JSP-3, the user's address bar will read http://[host]/Servlet-2. A reload/refresh will execute both Servlet-2 and JSP-3.

Both kinds of redirections are useful, depending on the precise effect you want.

http://www.theserverside.com/discussions/thread.tss?thread_id=26425

Page 27: Knowledge Sharing : Java Servlet

Thank youFahmi Jafar ([email protected])