1 of 23

Introduction to Java Servlets and The Need for Dynamic Content

Faculty name: Ravula Kartheek M.Tech, (Ph. D)

Ravula Kartheek

https://ravulakartheek.blogspot.com/

2 of 23

Index

  1. Introduction to Java Servlets
  2. Java Servlet technology
  3. Why Servlets?
  4. What can Servlets do?
  5. Java Servlet API and Life cycle
  6. The Servlet skeleton
  7. The Servlet Life cycle
  8. A simple welcome Servlet
  9. Working with Servlets

Faculty Name: Ravula Kartheek M.Tech, (Ph. D)

Assistant Professor in CSE of Data Science

Ravula Kartheek

https://ravulakartheek.blogspot.com/

3 of 23

1. Introduction to Java Servlets

Topic: The Need for Dynamic Content

  • There are many advantages of Servlet over CGI. The web container creates threads for handling the multiple requests to the Servlet.
  • Threads have many benefits over the processes such as they share a common memory area, lightweight, cost of communication between the threads are low.

Faculty Name: Ravula Kartheek M.Tech, (Ph. D)

Assistant Professor in CSE of Data Science

Ravula Kartheek

https://ravulakartheek.blogspot.com/

4 of 23

Continue…

  • The advantages of Servlet are as follows:

Fig: Advantages of Servlet

Faculty Name: Ravula Kartheek M.Tech, (Ph. D)

Assistant Professor in CSE of Data Science

Ravula Kartheek

https://ravulakartheek.blogspot.com/

5 of 23

Continue…

  • 1. Better Performance: because it creates a thread for each request, not process.
  • 2. Portability: because it uses Java language.
  • 3. Robust: JVM manages Servlets, so we don’t need to worry about the memory leak, garbage collection, etc.
  • 4. Secure: because it uses java language.

Faculty Name: Ravula Kartheek M.Tech, (Ph. D)

Assistant Professor in CSE of Data Science

Ravula Kartheek

https://ravulakartheek.blogspot.com/

6 of 23

2. Java Servlet Technology

- Servlet can be described in many ways, depending on the context.

  • Servlet is a technology which is used to create a web application.
  • Servlet is an API that provides many interfaces and classes including documentation.
  • Servlet is an interface that must be implemented for creating any Servlet.
  • Servlet is a class that extends the capabilities of the servers and responds to the incoming requests. It can respond to any request.
  • Servlet is a web component that is deployed on the server to create a dynamic web page.

Faculty Name: Ravula Kartheek M.Tech, (Ph. D)

Assistant Professor in CSE of Data Science

Ravula Kartheek

https://ravulakartheek.blogspot.com/

7 of 23

3. Why Servlets?

  • Servlet technology is used to create a web application (resides at server side and generates a dynamic web page).
  • Servlet technology is robust and scalable because of java language. Before Servlet, CGI (Common Gateway Interface) scripting language was common as a server-side programming language. However, there were many disadvantages to this technology.

Faculty Name: Ravula Kartheek M.Tech, (Ph. D)

Assistant Professor in CSE of Data Science

Ravula Kartheek

https://ravulakartheek.blogspot.com/

8 of 23

4. What can servlets do?

  • A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model.
  • Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers. For such applications, Java Servlet technology defines HTTP-specific servlet classes.

Faculty Name: Ravula Kartheek M.Tech, (Ph. D)

Assistant Professor in CSE of Data Science

Ravula Kartheek

https://ravulakartheek.blogspot.com/

9 of 23

5. Java Servlet API and Lifecycle

Java Servlet API

  • The jakarta.servlet and jakarta.servlet.http packages represent interfaces and classes for servlet api.
  • The jakarta.servlet package contains many interfaces and classes that are used by the servlet or web container. These are not specific to any protocol.
  • The jakarta.servlet.http package contains interfaces and classes that are responsible for http requests only.
  • Let’s see what are the interfaces of jakarta.servlet package.

Interface – What to do?

Class – How to do it?

Faculty Name: Ravula Kartheek M.Tech, (Ph. D)

Assistant Professor in CSE of Data Science

Ravula Kartheek

https://ravulakartheek.blogspot.com/

10 of 23

Interfaces in jakarta.servlet package

  • There are many interfaces in jakarta.servlet package. They are as follows:

  1. Servlet

2. ServletRequest

3. ServletResponse

4. RequestDispatcher

5. ServletConfig

6. ServletContext

7. SingleThreadModel (deprecated now)

8. Filter

9. FilterConfig

10. FilterChain

11. ServletRequestListener

12. ServletRequestAttributeListener

13. ServletContextListener

14. ServletContextAttributeListener

Faculty Name: Ravula Kartheek M.Tech, (Ph. D)

Assistant Professor in CSE of Data Science

Ravula Kartheek

https://ravulakartheek.blogspot.com/

11 of 23

Classes in jakarta.servlet package

  • There are many classes in jakarta.servlet package. They are as follows:

1. GenericServlet

2. ServletInputStream

3. ServletOutputStream

4. ServletRequestWrapper

5. ServletResponseWrapper

6. ServletRequestEvent

7. ServletContextEvent

8.ServletRequestAttributeEvent

9. ServletContextAttributeEvent

10. ServletException

11. UnavailableException

Faculty Name: Ravula Kartheek M.Tech, (Ph. D)

Assistant Professor in CSE of Data Science

Ravula Kartheek

https://ravulakartheek.blogspot.com/

12 of 23

Interfaces in jakarta.servlet.http package

  • There are many interfaces in jakarta.servlet.http package. They are as follows:

  1. HttpServletRequest

2. HttpServletResponse

3. HttpSession

4. HttpSessionListener

5. HttpSessionAttributeListener

6. HttpSessionBindingListener

7. HttpSessionActivationListener

8. HttpSessionContext (deprecated now)

Faculty Name: Ravula Kartheek M.Tech, (Ph. D)

Assistant Professor in CSE of Data Science

Ravula Kartheek

https://ravulakartheek.blogspot.com/

13 of 23

Classes in jakarta.servlet.http package

  • There are many classes in jakarta.servlet.http package. They are as follows:
  1. HttpServlet

2. Cookie

3. HttpServletRequestWrapper

4. HttpServletResponseWrapper

5. HttpSessionEvent

6. HttpSessionBindingEvent

7. HttpUtils (deprecated now)

Faculty Name: Ravula Kartheek M.Tech, (Ph. D)

Assistant Professor in CSE of Data Science

Ravula Kartheek

https://ravulakartheek.blogspot.com/

14 of 23

The Servlet Skeleton

  • The Sun Microsystem defines a unique standard to be followed by all the server vendors. Let’s see the skeleton structure that must be followed to create the servlet.

Fig: Skeleton Structure

Faculty Name: Ravula Kartheek M.Tech, (Ph. D)

Assistant Professor in CSE of Data Science

Ravula Kartheek

https://ravulakartheek.blogspot.com/

15 of 23

The Servlet Life Cycle

Fig: Servlet Life Cycle

Faculty Name: Ravula Kartheek M.Tech, (Ph. D)

Assistant Professor in CSE of Data Science

Ravula Kartheek

https://ravulakartheek.blogspot.com/

16 of 23

Continue…

The init() Method

Initializes the servlet. It is the life cycle method of servlet and invoked by the web container only once.

Syntax:

public void init() throws ServletException

{

//Initialization code…

}

Faculty Name: Ravula Kartheek M.Tech, (Ph. D)

Assistant Professor in CSE of Data Science

Ravula Kartheek

https://ravulakartheek.blogspot.com/

17 of 23

Continue…

The service() Method

Provides response for the incoming request. It is invoked at each request by the web container.

Syntax:

public void service(ServletRequest request, ServletResponse response)

throws ServletException, IOException

{

}

Faculty Name: Ravula Kartheek M.Tech, (Ph. D)

Assistant Professor in CSE of Data Science

Ravula Kartheek

https://ravulakartheek.blogspot.com/

18 of 23

Continue…

The doGet() Method

This method process normal request to URL form as shown below:

Syntax:

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

{

//Servlet code

}

Faculty Name: Ravula Kartheek M.Tech, (Ph. D)

Assistant Professor in CSE of Data Science

Ravula Kartheek

https://ravulakartheek.blogspot.com/

19 of 23

Continue…

The doPost() Method

A post request results from an HTML form that specifically lists POST as the METHOD and it should be handled by doPost() method.

Syntax:

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

{

//Servlet code

}

Faculty Name: Ravula Kartheek M.Tech, (Ph. D)

Assistant Professor in CSE of Data Science

Ravula Kartheek

https://ravulakartheek.blogspot.com/

20 of 23

Continue…

The destroy() Method

It is invoked only once and indicates that servlet is being destroyed. After the destroy() method is called, the servlet object is marked for garbage collection.

Syntax:

public void destroy()

{

//Finalization code…

}

Faculty Name: Ravula Kartheek M.Tech, (Ph. D)

Assistant Professor in CSE of Data Science

Ravula Kartheek

https://ravulakartheek.blogspot.com/

21 of 23

A Simple Welcome Servlet

import jakarta.servlet.ServletException;

import jakarta.servlet.http.*;

import java.io.*;

public class Demo extends HttpServlet {

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

res.setContentType("text/html");

PrintWriter pw = res.getWriter();

pw.println("<html><body>");

pw.println("<h1>Welcome to the Servlet</h1>");

pw.println("</body></html>");

pw.close();

}

}

*DemoServlet project in Eclipse

Faculty Name: Ravula Kartheek M.Tech, (Ph. D)

Assistant Professor in CSE of Data Science

Ravula Kartheek

https://ravulakartheek.blogspot.com/

22 of 23

OUTPUT

Faculty Name: Ravula Kartheek M.Tech, (Ph. D)

Assistant Professor in CSE of Data Science

Ravula Kartheek

https://ravulakartheek.blogspot.com/

23 of 23

Working With Servlets

Faculty Name: Ravula Kartheek M.Tech, (Ph. D)

Assistant Professor in CSE of Data Science

Ravula Kartheek

https://ravulakartheek.blogspot.com/