Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

Servlet or Web Servlet :-

                                                                                                                                              Next Chapter

It is a predefined Java Class that will be inherited by HttpServlet class that exists under javax.http.servlet package. This class will be compiled by Java Compiler and execute by JVM under  Servlet container by Web Server.

A Servlet container is used to manage the servlet code that is written on java programming language of an application, Webserver is only used to manage static files (HTML, JS, CSS, Jquery) and JSP Web page but dynamic content will be managed by Servlet Container itself.







hence It is also called a Web-based Java class.

Servlet Class will be executed under Servlet container to Web Server.

Servlet class Managed By HttpServlet  Class in Java


Syntax of Servlet:-

Class Classname extends HttpServlet
{




}




Servlet Example of Class:-



class A extends HttpServlet
{
   public void init()
  {

   }
    public void doGet(HttpServletRequest request,HttpServletResponse response)   //For HttpGet
   {


   }

  public void doPost(HttpServletRequest request,HttpServletResponse response)   // For HttpPost
  {


  }


public void destroy()
{

}
}


Servlet Life Cycle:-



Servlet Use Four Different State to Perform Operation

1)  Loading:- When we call Servlet class then the servlet object will be loaded under Web Container.


2) Initializing:-  After Successfully loading the servlet object data will be initialized under memory and call init();


3)  Handling Request:-  When we request a servlet from client to server then it will check the service method if it gets then doGet() will be called otherwise doPost() will be called.


4) Destroy:-  This method will be called implicit when the servlet objects will be destroyed.







Live Exmple of Servlet:-


OPEN ECLIPSE---->  FILE ----> NEW ---> Other---> Dynamic Web Project---> Enter project Name--> Modify Server----> Next---> Go into Java Resources---->create package---> Create Servlet class



package scs;

}import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Hello")
public class Hello extends HttpServlet {

public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException
{
PrintWriter out= response.getWriter();
out.print("<h1>Hello World</h1>");
}



Now I am Creating another Servlet Class to Calculate Addition Program:-

package scs;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Hello
 */
@WebServlet("/Hello")
public class Hello extends HttpServlet {
    public Hello() {
        super();
        
    }

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int a=100,b=200,c;
c=a+b;
PrintWriter out = response.getWriter();
out.print("Result is ="+c);
}

}


                                                                                                                                        Next Chapter


Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post