Question

How do I get parameters from a HTML form when using servlets?

Answer

When extending javax.servlet.http.HttpServlet, you should override either of the following two methods: -

public void doGet(HttpServletRequest req,
    HttpServletResponse res)
public void doPost(HttpServletRequest req,
    HttpServletResponse res)

Both of these methods accept as a parameter a HttpServletRequest instance. This allows the servlet to obtain information about the browser request, including the parameters passed to the servlet. By using the String getParameter(String) method, you can request any parameter you need. If the parameter is not present, a null value will be returned.


Back