0% found this document useful (0 votes)
34 views17 pages

Servlets

Servlets allow Java code to run on the server-side of a web application. They provide dynamic content to web pages and allow for easy coordination between server-side components. Servlets are contained within a servlet container, which provides supporting infrastructure like sessions, persistence, security, and more. This allows servlets to be more efficient than CGI programs by loading each servlet only once rather than creating a new process for each request. The document then provides details on how to write, compile, deploy and map servlets within a web application.

Uploaded by

abhijitddas19
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views17 pages

Servlets

Servlets allow Java code to run on the server-side of a web application. They provide dynamic content to web pages and allow for easy coordination between server-side components. Servlets are contained within a servlet container, which provides supporting infrastructure like sessions, persistence, security, and more. This allows servlets to be more efficient than CGI programs by loading each servlet only once rather than creating a new process for each request. The document then provides details on how to write, compile, deploy and map servlets within a web application.

Uploaded by

abhijitddas19
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

Servlets

Stoney Jackson
[email protected]
http://wwwcsif.cs.ucdavis.edu/~jacksoni
Java on the Web: J2EE
• Thin clients (minimize download)
• Java all “server side”

Servlets

Client
Server

THIS IS WHAT YOU’LL BE DOING!!


Outline
Who made Servlets?Sun
What are Servlets?
Why are Servlets?
Where are Servlets?
When are Servlets?
How are Servlets?
What are Servlets?
• Units of Java code that run server-side.
• Run in containers (provide context)
• Helps with client-server communications
– Not necessarily over HTTP
– But usually over HTTP (we’ll focus here)
Why are Servlets?

• Web pages with dynamic content


• Easy coordination between Servlets to make
Web applications
• Containers support many features
– Sessions, persistence, resource management
(e.g., database connections), security, etc.
Where are Servlets?

Static File system

HTTP Web
Server
Servlet
Dynamic Server

Tomcat = Web Server + Servlet Server


When are Servlets?
Receive Request for
Servlet S
Loaded when first used,
or after modified
Is S loaded? no
yes

Is S current? no (re)Load S

yes
Servlets die when
Forward Request Servlet Server dies
to S
Why Not CGIs?
• CGI = Common Gateway Interface
• Defines interface between Web servers and
programs
– Environment variables and standard-in as input
from client
– Standard-out for output to client
Why Not CGIs (cont.)
• Only defines interface
 no supporting infrastructure
(security, sessions, persistence, etc.)

• Inefficient: new process for each request!!!

Recall: Servlets loaded only “once”!


import java.io.*;
import javax.servlet.*; How are
import javax.servlet.http.*;
Servlets?
public class Hellox 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>");
} // doGet
} // Hellox
Compiling
javac –classpath
$LIB/servlet-api.jar
Hellox.java
Directory Structure
Create your
web applications
here

Create a directory
D for your
web application

Create “WEB-INF”
under D

Create “classes”
under “WEB-INF”
Directory Structure
(cont.)

Static content in D

Dynamic content
in WEB-INF

web.xml in WEB-INF

Servlets in classes
<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

<description>Examples</description>
<display-name>Examples</display-name>

<servlet> Declares servlet


<servlet-name>Hellox</servlet-name> abbreviation
<servlet-class>Hellox</servlet-class>
</servlet> fully qualified (e.g., java.lang.String)

<servlet-mapping> Maps servlet to URL (rooted at D)


<servlet-name>Hellox</servlet-name>
<url-pattern>/Hellox</url-pattern>
</servlet-mapping> </web-app>
public class Helloy 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, Tell me your name!</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello, Tell me your name!</h1> <br>");
out.print("<form action=\"");
out.println("NamedHello\" method=POST>");
out.println("<input type=text length=20 name=yourname><br>");
out.println("<input type=submit></form>");

out.println("</body>");
out.println("</html>");
}}
public class NamedHello extends HttpServlet {
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name = request.getParameter("yourname");
out.println("<html>");
out.println("<body>");
out.println("<head>");
out.println("<title>Hello, Tell me your name again!</title>");
out.println("</head>");
out.println("<body>");
out.println("<h2>Hello, " + name + "</h2> <br>");
out.print("<form action=\"");
out.println("NamedHello\" method=POST>");
out.println("<input type=text length=20 name=yourname><br>");
out.println("<input type=submit></form>");
out.println("</body>");
out.println("</html>");
}}
public class NamedSessionHello1 extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html"); PrintWriter out = response.getWriter();
HttpSession hs = request.getSession(true);
String sn = (String) hs.getAttribute("yourname");
out.println("<html>"); out.println("<body>"); out.println("<head>");
out.println("<title>Hello, Tell me your name again!</title>");
out.println("</head>"); out.println("<body>");
if(sn != null && ! sn.equals ("")) {
out.println("<h1><blink> OH, NamedSessionHello1” +
“already know your name: " + sn + "</blink></h1>");
} else {
String sn2 = request.getParameter("yourname");
if (sn2 == null || sn2.equals("")) {
out.println("<h2>Hello,noname " + "</h2> <br>");
} else {
out.println("<h2>Hello, " + sn2 + "</h2> <br>");
hs.setAttribute("yourname", sn2);
}}
out.print("<form action=\""); out.println("NamedSessionHello2\" method=GET>");
... }

You might also like