Issue
I have a class with extends to HttpServlet
class.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class GreetServer extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().print("hello client");
}
// Method to handle POST method request.
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
My question is, how do I make it working and handling POST and GET requests (What should I write in my public static void main(String[] args)
method)?
I'm using Heroku to deploy the app.
Thank in advance.
Solution
A Servlet cannot stand alone. You need a web container to host your Servlet.
The “-let” in “Servlet” means this is a tiny fraction of a server app. The bulk of the work of a server app such as listening on a port for incoming connections, performing initial analysis of each incoming request, dispatching to a particular Servlet, and sending the Servlet’s response back out over the network, is performed by the Servlet container in which your Servlet is hosted.
In other words, your Servlet is a small component that plugs into a much larger Servlet container server infrastructure application that runs on top of a Java implementation (obtained from a vendor such as Pivotal, Amazon, Oracle, BellSoft, Azul Systems, Red Hat/IBM, Adoptium, Microsoft, etc.) that runs on top of a host operating system (OS) (such as macOS, Linux, BSD, MS Windows, etc.).
Jakarta Servlet server stack |
---|
your Servlet |
Servlet container (Tomcat/Jetty etc.) |
Java |
host operating system |
computer hardware |
As a beginner, I would suggest using either Apache Tomcat or Eclipse Jetty as your Servlet container. These two products are relatively small and simple, implementing only a handful of the few dozen Jakarta specifications.
Later, if your web app grows to utilize other Jakarta specs not supported by Tomcat/Jetty, you can either add Jakarta implementation libraries to Tomcat/Jetty, or you may choose to move to a larger server that comes bundled with those libraries. Examples include TomEE, Payara, WildFly/JBoss, Open Liberty/WebSphere, and more.
I also suggest you do some more studying along with your programming. Start with Wikipedia page.
The usual way to deploy your Servlet to a web container is to package the compiled class(es) to a WAR file. Then simply drop that file into the appropriate folder to your particular container server.
While in development, your IDE (such as IntelliJ, NetBeans, or Eclipse) likely can either run a Servlet container within itself or can connect to an externally-running Servlet container. For example, some Maven archetypes will include actions for running Jetty in an embedded mode within your IDE for quick and convenient execution of your developing Servlet.
In your particular case, you said you want to use Heroku for production. Heroku is a cloud service company that provides the Servlet server stack on which you will deploy your Servlet, your WAR file.
While developing your Servlet you can run with a product like Tomcat or Jetty, later you can deploy to Heroku. The whole point of Jakarta Servlet being an open specification is that any Servlet you write should run successfully on any and all Servlet containers compliant with that spec.
You asked:
What should I write in my
public static void main(String[] args)
method
There is no main
method for a Servlet.
The Servlet container automatically locates, loads, instantiates, and initializes your servlet. As requests for your web app arrive at the server, the web container automatically invokes methods on your Servlet object.
Answered By - Basil Bourque
Answer Checked By - Candace Johnson (JavaFixing Volunteer)