The objective of a Servlet is to handle client request. There are two interfaces present in Servlet API, javax.servlet.ServletRequest and javax.servlet.http.HttpServletRequest to encapsulate client request. An object of ServletRequest is used to provide the client request information to a servlet such as content type, content length, parameter names and values, header informations, attributes etc .
Let's see an example to demonstrate the Servlet Request
<html> <body> <form action="MyServlet" method="post"> Name <input type="text" name="uname"> <input type="submit" value="ok"> </form> </body> </html>
MyServlet.java import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class MyServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter pw = response.getWriter(); String s=request.getParameter("uname"); pw.println("Welcome"+s); } }
<servlet> <servlet-name>abc</servlet-name> <servlet-class>com.javarace.MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name> abc</servlet-name> <url-pattern>/MyServlet</url-pattern> </servlet-mapping>
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc