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 Example in Java:-                                                                                               Next Chapter



Servlet is the special class in java which provide a set of web method to write server-side java program.

Servlet class is used to implement dynamic functionality under the JAVA Web application.

Servlet is also called Server Side Java Class.

Java provides a HttpServlet class to implement Servlet Functionality.

Servlet class will run under a web server or application server.

means we should install Apache Web Server or Glassfish Web Server to execute Servlet Class

class Classname extends HtttpServlet
{
     public void doPost(HttpServletRequest request,HttpServletResponse response)
     {

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


      }
   


}


How to create Servlet Class Practically:-

In Eclipse:-

File   ---->   New Project---> Dynamic Web Project  ---->  Web Application-->Right-click on  Source Package ----> Servlet------>

Code under Servlet class

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.write("<h1>Welcome in Java</h1>");
}




Create a Servlet program to perform the addition of two numbers?

1)  Create a new Servlet class on right-click on Package


2)  write code on doGet() 


3)  This is the code that will be written on the Servlet method.



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




Complete code of Servlet class in java:-

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;

@WebServlet("/Addition")
public class Addition extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
int a =100;
int b=200;
int c=a+b;
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