Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Confidential
Web components are in the form of either Servlet or JSP (along with
JavaBean's andcustom tags)
Web components run in a Web container
Confidential
Web Application is a deployable package
Confidential
Web Request Handling
Confidential
Java Web Application Technologies
Confidential
Steps for creating a Web Application
Write (and compile) the Web component code (Servlet or JSP) and
helper classes referenced by the web component code
Create any static resources (for example, images or HTML pages)
Create deployment descriptor (web.xml)
Build the Web application (*.war file or deployment-ready directory)
Deploy the web application into a Web container
Web clients are now ready to access them via URN
Confidential
Write and compile the Web component code
Confidential
Development Structure
Hello
src
webroot
WEB-INF
classes
GreetingServlet.class
ResponseServlet.class
web.xml
duke.waving.gif
build.xml
Confidential
Deploy structure of Web application
Confidential
Create *.WAR file
Confidential
Web Applications Deployment Descriptor (web.xml)
Prolog
Alias Paths
Context and Initialization Parameters
Event Listeners
Filter Mappings
Error Mappings
Reference to Environment Entries, Resource environment entries,
or Resources
Confidential
Prolog (of web.xml)
Confidential
Alias Paths (of web.xml)
– http://<host>:8080/context_root/alias_path
Alias Path can be in the form of either
Confidential
Alias Paths (of web.xml)
<servlet>
<servlet-name>greeting</servlet-name>
<display-name>greeting</display-name>
<description>no description</description>
<servlet-class>GreetingServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>response</servlet-name>
<display-name>response</display-name>
<description>no description</description>
<servlet-class>ResponseServlet</servlet-class>
</servlet>
Confidential
<servlet-mapping>
<servlet-name>greeting</servlet-name>
<url-pattern>/greeting</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>response</servlet-name>
<url-pattern>/response</url-pattern>
</servlet-mapping>
Confidential
Context and Initialization Parameters (of web.xml)
<web-app>
...
<context-param>
<param-name>
javax.servlet.jsp.jstl.fmt.localizationContext
</param-name>
<param-value>messages.BookstoreMessages</param-value>
</context-param>
...
</web-app>
Confidential
Event Listeners (of web.xml)
<listener>
<listener-class>listeners.ContextListener</listener-class>
</listener>
Confidential
Filter Mappings (of web.xml)
<filter>
<filter-name>OrderFilter<filter-name>
<filter-class>filters.OrderFilter<filter-class>
</filter>
<filter-mapping>
<filter-name>OrderFilter</filter-name>
<url-pattern>/receipt</url-pattern>
</filter-mapping>
Confidential
Error Mappings (of web.xml)
<error-page>
<exception-type>exception.OrderException</exception-type>
<location>/errorpage.html</location>
</error-page>
Confidential
References (of web.xml)
<resource-ref>
<res-ref-name>jdbc/BookDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Confidential
Example web.xml of hello2
<display-name>hello</display-name>
<servlet>
<display-name>GreetingServlet</display-name>
<servlet-name>GreetingServlet</servlet-name>
<servlet-class>servlets.GreetingServlet</servlet-class>
</servlet>
<servlet>
<display-name>ResponseServlet</display-name>
<servlet-name>ResponseServlet</servlet-name>
<servlet-class>servlets.ResponseServlet</servlet-class>
</servlet>
Confidential
<servlet-mapping>
<servlet-name>GreetingServlet</servlet-name>
<url-pattern>/greeting</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ResponseServlet</servlet-name>
<url-pattern>/response</url-pattern>
</servlet-mapping>
</web-app>
Confidential
Servlet
Confidential
Session Tracking
Servlet Filters
Servlet life-cycle events
Including, forwarding to, and redirecting to other web resources
Concurrency Issues
Invoker Servlet
Confidential
What is Servlet?
Java™ objects which are based on servlet framework and APIs and
extend the functionality of a HTTP server.
Mapped to URLs and managed by container with a simple
architecture
Available and running on all major web servers and app servers
Platform and server independent
Confidential
CGI versus Servlet
Confidential
Confidential
Advantages of Servlet
No CGI limitations
Abundant third-party tools and Webservers supporting Servlet
Access to entire family of Java APIs
Reliable, better performance and scalability
Platform and server independent
Secure
Most servers allow automatic reloading of Servlet's by administrative
action
Confidential
First Servlet Code
Confidential
Servlet Request and Response Model
Confidential
What does Servlet Do?
Confidential
Requests and Responses
What is a request?
Confidential
HTTP
header
a method
Get: Input form data is passed as part of URL
Post: Input form data is passed within message body
Put
Header
request data
Confidential
HTTP GET and POST
POST requests:
Confidential
First Servlet
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
Public class HelloServlet extends HttpServlet {
Confidential
Servlet Interfaces & Classes
Confidential
Servlet Life-Cycle
Confidential
Servlet Life Cycle Methods
Confidential
Servlet Life Cycle Methods
Invoked by container
javax.servlet.GenericServlet class or
init()
destroy()
service() - this is an abstract method
javax.servlet.http.HttpServlet class
doGet(), doPost(), doXxx()
service() - implementation
Confidential
Servlet Life Cycle Methods
init()
connection
Confidential
Service() Method
Confidential
doGet() and doPost() Methods
Confidential
Things You Do in doGet() & doPost()
Confidential
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
Public class HelloServlet extends HttpServlet { public void
doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<title>First Servlet</title>");
out.println("<big>Hello J2EE Programmers! </big>");
}
}
Confidential
Steps of Populating HTTP Response
Buffer size
Get an output stream object from the response
Write body content to the output stream
Confidential
Example: Simple Response
}
}
Confidential