{{theTime}}

Search This Blog

Total Pageviews

Showing posts with label Java Servlets. Show all posts
Showing posts with label Java Servlets. Show all posts

Servlet LifeCycle

LifeCycle:  Servlet Creation to Termination:

-- Servlet is intialized by calling the init() method.  Init() method called only once when first created
-- Client requests processed by service() method.  Each request resulted a new thread calling service() method.  Service method checks the request type (GET, POST, PUT, DELETE) and call doGet, doPost, doPut or doDelete() methods.
-- Servlet is termincated by desctroy() method
-- Finally, garbage collected by the JVM garbage collector

ProgramResourceServlet Example Extending HttpServlet

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

 
public class ProgramResourceServlet extends HttpServlet
{  

   public void init() throws HttpServlet {

   }
     
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws Servlet Exception, IOException {
        handleServletRequest(req, res);
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        handleServletRequest(req, res);
    }

    public void handleServletRequest(HttpServletRequest req, HttpServletResponse res) throws IOException {

        PrintWriter out = res.getWriter();
        res.setContentType("text/plain");

        Enumeration<String> parameterNames = req.getParameterNames();

        while (parameterNames.hasMoreElements()) {

            String name = parameterNames.nextElement();
            out.write(name);
            out.write("\n");

            String[] paramValues = req.getParameterValues(name);
            for (int i = 0; i < paramValues.length; i++) {
                String paramValue = paramValues[i];
                out.write("Param Value " + paramValue);
            }
        }
        out.close();
    }
}

Servlet Chaining

What is Servlet Chaining ?
Servlet Chaining is the method which allows to send the output of one servlet sent to other servlet.  The output of the second servlet can be sent to a third servlet, and so on. The last servlet in the chain is responsible for sending the response to the client.

HttpServlet cannot be resolved to a type

How to fix this problem.



servlet api jar is missing.  Configure buildpath in Eclipse to add servlet-api.jar

Optimizing Java Applications for Low-Latency Microservices

Introduction Microservices architecture has become a go-to for building scalable, modular systems, but achieving low latency in Java-based...