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);
}

}


Create Servlet Program using Design and Programming both?

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 SISer
 */
@WebServlet("/SISer")
public class SISer extends HttpServlet {
private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SISer() {
        super();
        // TODO Auto-generated constructor stub
    }

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.write("<html>");
out.write("<head>");
out.write("</head>");
out.write("<body>");
out.write("<h1>SI Program</h1>");
out.write("<form action='' method='post'>");
out.write("<input type='text' name='txtp' placeholder='Enter p' /> <br>");
out.write("<input type='text' name='txtr' placeholder='Enter r' /><br>");
out.write("<input type='text' name='txtt' placeholder='Enter t' /><br>");
out.write("<input type='submit'  value='calculate' />");
out.write("</form>");
out.write("</body>");
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    float  p = Float.parseFloat(request.getParameter("txtp"));
    float  r = Float.parseFloat(request.getParameter("txtr"));
    float  t = Float.parseFloat(request.getParameter("txtt"));
    float si = (p*r*t)/100;
    PrintWriter out = response.getWriter();
    out.write("Result is "+si);
    
}

}

Create Servlet Program and display output In same web page?

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 SISer
 */
@WebServlet("/SISer")
public class SISer extends HttpServlet {
private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SISer() {
        super();
        // TODO Auto-generated constructor stub
    }

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.write("<html>");
out.write("<head>");
out.write("</head>");
out.write("<body>");
out.write("<h1>SI Program</h1>");
out.write("<form action='' method='post'>");
out.write("<input type='text' name='txtp' placeholder='Enter p' /> <br>");
out.write("<input type='text' name='txtr' placeholder='Enter r' /><br>");
out.write("<input type='text' name='txtt' placeholder='Enter t' /><br>");
out.write("<input type='submit'  value='calculate' />");
out.write("</form>");
out.write("</body>");
if(request.getParameter("q")!=null)
{
out.write("Result is "+request.getParameter("q"));
}
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    float  p = Float.parseFloat(request.getParameter("txtp"));
    float  r = Float.parseFloat(request.getParameter("txtr"));
    float  t = Float.parseFloat(request.getParameter("txtt"));
    float si = (p*r*t)/100;
    PrintWriter out = response.getWriter();
   // out.write("Result is "+si);
    response.sendRedirect("SISer?q="+si);
    
}

}


                                                                                                                                          Next Chapter                  

Post a Comment

POST Answer of Questions and ASK to Doubt

Previous Post Next Post