Online Courses
Free Tutorials  Go to Your University  Placement Preparation 
0 like 0 dislike
876 views
in RTU B.Tech (CSE-V Sem) Advance Java Lab by Goeduhub's Expert (5.8k points)
edited by

Introduction to Java Servlet and server side programming with servlet

To know step by step guide to install and setup Apache Tomcat server in Eclipse Click here

Goeduhub's Top Online Courses @Udemy

For Indian Students- INR 360/- || For International Students- $9.99/-

S.No.

Course Name

 Coupon

1.

Tensorflow 2 & Keras:Deep Learning & Artificial Intelligence

Apply Coupon

2.

Natural Language Processing-NLP with Deep Learning in Python Apply Coupon

3.

Computer Vision OpenCV Python | YOLO| Deep Learning in Colab Apply Coupon
    More Courses

1 Answer

0 like 0 dislike
by Goeduhub's Expert (5.8k points)
edited by
 
Best answer

Servlet

  • 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 requests.
  • Servlet is a web component that is deployed on the server to create a dynamic web page.
  • Before Servlet, CGI (Common Gateway Interface) scripting language was common as a server-side programming language.

Common Gateway Interface (CGI)

CGI technology enables the web server to call an external program and pass HTTP request information to the external program to process the request. For each request, it starts a new process.

Disadvantages of CGI:-

  • If the number of clients increases, it takes more time for sending the response.
  • For each request, it starts a process, and the web server is limited to start processes.
  • It uses platform dependent language e.g. C, C++, perl.

Advantages of Servlet:-

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

Servlet vs CGI

Servlet vs CGI

Servlet API

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

Servlet Interface

Servlet interface provides common behavior to all the servlets. Servlet interface defines methods that all servlets must implement.

Servlet interface needs to be implemented for creating any servlet (either directly or indirectly). It provides 3 life cycle methods that are used to initialize the servlet, to service the requests, and to destroy the servlet and 2 non-life cycle methods.

Methods of Servlet Interface

Method Description
public void init(ServletConfig config) initializes the servlet. It is the life cycle method of servlet and invoked by the web container only once.
public void service(ServletRequest request,ServletResponse response) provides response for the incoming request. It is invoked at each request by the web container.
public void destroy() is invoked only once and indicates that servlet is being destroyed.
public ServletConfig getServletConfig() returns the object of ServletConfig.
public String getServletInfo() returns information about servlet such as writer, copyright, version etc.

Program Code Implementing Servlet Interface

First.java

import java.io.*;

import javax.servlet.*;

public class First implements Servlet{

ServletConfig config=null;

public void init(ServletConfig config){

this.config=config;

System.out.println("Servlet is initialized");

}

public void service(ServletRequest req,ServletResponse res)

throws IOException,ServletException{

res.setContentType("text/html");

PrintWriter out=res.getWriter();

out.print("<html><body>");

out.print("<b>Hello this is an example of simple servlet</b>");

out.print("</body></html>");

}

public void destroy(){System.out.println("Servlet is destroyed");}

public ServletConfig getServletConfig(){return config;}

public String getServletInfo(){return "copyright 2007-1010";}

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app >

<servlet>

 <servlet-name>s1</servlet-name>

 <servlet-class>First</servlet-class>

</servlet>

<servlet-mapping>

 <servlet-name>s1</servlet-name>

 <url-pattern>/hello</url-pattern>

</servlet-mapping>

  <display-name>Simple</display-name>

  <welcome-file-list>

    <welcome-file>index.html</welcome-file>

    <welcome-file>index.htm</welcome-file>

    <welcome-file>index.jsp</welcome-file>

    <welcome-file>default.html</welcome-file>

    <welcome-file>default.htm</welcome-file>

    <welcome-file>default.jsp</welcome-file>

  </welcome-file-list>

</web-app>

index.html
<a href="hello">Invoke Servlet</a>

Output

Output 1

Output 2

Output 3


For more RTU V Sem Advance Java Lab Experiments Click here


3.3k questions

7.1k answers

394 comments

4.6k users

Related questions

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 
...