SERVLETS IN JAVA:
Introduction:
Servlet is a basically server side programming concept in java, in servlets we don't need to compile our program like core java applications because we run servlets on the server like tomcat,weblogic,jbose server etc. Servlet interface is present in javax.servlet package.In servlet we don't need to install any software manually like JDK in core java because server vandors provide the support in the form of jar files like servlet-api.jar in tomcat server.
By using servlet we can create web applications as well as enterprise application by using servlets,so it's very simple and easy to use in our program.
interface Servlet
{
public void init(ServletConfig config);
public void service(ServletRequest req, ServletResponse res);
public void destroy();
public String getServletInfo();
public servletConfig getServletConfig();
}
We have Three ways to create servlet in java.
1. By implements Servlet interface
2. By extends GenricServlet
3. By extnds HttpServlet
1. By implements Servlet interface :
Inside the Servlet interface there are five abstract methods are available ,we need to provide the implementation for all following five methods weather we are using or not otherwise we can't run our application. Even though we write the logic in service() method only. So that it is not recommended to use , it's increase the length of the code and create performance problems
ex: class MyServlet implements Servlet
{
public void init(Servletconfig config)
{
// to Initialization servlet
}
public void service(ServletRequest req, ServletResponse res)
{
// To write business logic
}
public void destroy()
{
// to destroy the servlet
}
public String getServletInfo(){
}
public servletConfig getServletConfig()
{
}
}
2. By extends GenricServlet:
There is an another way to create servelet in java. In GenricServlet we don't need to provide implementation to all the five methods, we need to provide implementation only service() method where we can write our business logic. It is also known as adapter class in java and it is a abstract class as well because one abstract service method present So we can't create object of GenricServlet class.
GenricServlet is a protocol independent so that if there is a HTTP request or SMTP request same service() method will be executed. So it's not recommanded to use .
abstract class MyServlet extends GenricServlet
{
public void service(ServletRequest req, ServletResponse res)
{
// To write business logic
}
}
3. By extends HttpServlet :
HttpServlet is a best and generally use to create servlet in java. In the HttpServlet class there is no abstract method present but still it is a abstract class because partially implementation of methods present in HttpServlet class we can't create an object of HttpServlet class.
HttpServlet class is protocol dependent , which means we can send only HTTP type of request to the client browser and every time different methods will be executed.
There are following requests we can send to the client browser.
- doGet()
- toPost()
- toDelete()
- doTrace()
- doPut()
- doHead()
- doOption()
package com.kl;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FirstServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public FirstServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("Hello My First Servlet ");
}
}
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FirstServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public FirstServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("Hello My First Servlet ");
}
}
How to set up :
If you want to run your servlet we need a server like tomcat and others. server is a mandatory for servlets.
Download Eclipse IDE:
![]() |
Add caption |
Output:
Hello My First Servlet