java servlets
TRANSCRIPT
05/03/2023University of Education, Okara
1Java ServletsWeb Engineering
Faraz Hashmi, BSIT-5th Eve, 3011
05/03/2023University of Education, Okara
2
Servlets are protocol- and platform-independent server side components, written in Java, which dynamically extend Java-enabled servers.
They have become more and more popular as they benefit from all the advantages of the Java programming language in particular Architecture and Platform Independence and the ability to build robust and secure applications.
Servlet
05/03/2023University of Education, Okara
3
A Servlet, in simple terms, is a Java program running under a web server taking a 'request' object as an input and responding back by a 'response' object. Typically a web browser will send the request in HTTP format. The Servlet container will convert that into a request object. Similarly the response object - populated by the Servlet is converted into an HTTP response by the Servlet container.
This mechanism makes the browser - web server interaction very easy. Java Servlets are more efficient, easier to use, more powerful, more portable, and cheaper than traditional CGI and many alternative CGI-like technologies.
(More importantly, Servlet developers tend to get paid more than Perl programmers)
Servlet
05/03/2023University of Education, Okara
4 Servlet Mechanism
A servlet is a Java class and thus needs to be executed in a Java Virtual Machine by a service called a servlet engine. The servlet engine loads the servlet before it can be used. The servlet then stays loaded until it is unloaded or the servlet engine is shut down.
05/03/2023University of Education, Okara
5 Servlet Mechanism
Client/Browser Server Servl
et
JVMRequest
Response
Extends the functionality of the server byGenerating HTML pages dynamically
05/03/2023University of Education, Okara
6 Servlet Types
Servlets are based on two main packages javax.servlet javax.servlet.http
GenericServlet For writing protocol independent servlets
HttpServlet Extends from GenericServlet class Adds functionality for writing HTTP specific servlets
Servlet class hierarchy
Object
GenericServlet ServletResponse
HttpServletRequest
ServerRequest
HttpServlet HttpServletResponse
javax.servlet
javax.servlet.http
05/03/2023University of Education, Okara
8 Software Requirements
To use Java servlets, following softwares will be needed
J2SE
Additional J2EE based libraries for servlets such as servlet-api.jar, jsp-api.jar. You can download these APIs separately but they are also available with the web server you’ll be using.
A capable servlet web engine (web server)
05/03/2023University of Education, Okara
9 Environment Setup
Steps
1. Download the Apache Tomcat Server2. Install Tomcat3. Set the JAVA_HOME variable4. Set the CATALINA_HOME variable5. Set the CLASSPATH variable6. Test the Server
05/03/2023University of Education, Okara
10 Servlet Advantages & Disadvantages
The advantage of Servlets is, Portability
o Portable across operating systems and across web servers Power
o Harness the full power of the core Java APIs: networking and URL access, multithreading, image manipulation, data compression, JDBC, object serialization, internationalization Efficiency & Endurance
o Memory resident, so invocation highly efficient—no process to spawn or interpreter to invoke
05/03/2023University of Education, Okara
11 Servlet Advantages & Disadvantages
Safetyo Support safe programming since inherit Java’s strong type
safety, exception-handling mechanism Elegance
o Code is clean, object-oriented, modular, and simple (i.e.. Session tracking, cookie) Integration
o Tightly integrated with the server—translate file paths, perform logging, check authorization, and MIME type mapping
05/03/2023University of Education, Okara
12 Servlet Advantages & Disadvantages
The disadvantage of Servlets is, Web Administrator will need to learn how to install and maintain
Java Servlets Tedious uses of out.println() statements
o Can be remedied by using Java Server Page (JSP)
05/03/2023University of Education, Okara
13 Types of HTTP Requests
Get Post Delete Options Put Trace
How HTTP Sends Request
Client Server
Servlets
Some HTTP request types
• Get -- Attr/Val pairs attached after ? of URL E.g. http://www.gmail.com/register?name=ali
• Post -- Attr/Val pairs attatched with the request body
05/03/2023University of Education, Okara
15 HTTP Request Example
Request parameters etc.
05/03/2023University of Education, Okara
16 Servlet Model
05/03/2023University of Education, Okara
17 Servlet Life Cycle
Initialize Service Destroy
05/03/2023University of Education, Okara
18 HelloWorld Servlet
import java.io.*;import javax.servlet.*;import javax.servlet.http.*;
public class HelloWorldServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter(); out.println(“Hello World!”);}
}
05/03/2023University of Education, Okara
19 Web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
<servlet> <servlet-name>HelloWorldServlet</servlet-name> <servlet-class>HelloWorldServlet</servlet-class> </servlet>
<servlet-mapping> <servlet-name>HelloWorldServlet</servlet-name> <url-pattern>/myfirstservlet</url-pattern> </servlet-mapping>
</web-app>