OfferTransform Your Career with Expert-Led IT Training. Flat discounts active!Explore Now
OnlineITGuru Logo
AI & Machine Learning

Servlet Request Interface

Last updated on Jun 15, 2020

Copy Link:
Servlet Request Interface

Servlet Request Interface:

A challenge of ServletRequest is used to give the client request information to a servlet, for instance, content sort, content length, parameter names and qualities, header informations, attributes.

Servlet Request Interface Methods

Example of ServletRequest to display the name: index.html
  1. <form action="welcome"method="get">
  2. Enter your name<input type="text"name="name"><br>
  3. <input type="submit"value="login">
  4. </form>
DemoServ.java
  1. importservlet.http.*;
  2. importservlet.*;
  3. importio.*;
  4. publicclass DemoServ extends HttpServlet{
  5. publicvoid doGet(HttpServletRequest req,HttpServletResponse res)
  6. throwsServletException,IOException
  7. {
  8. setContentType("text/html");
  9. PrintWriter pw=res.getWriter();
  10. String name=req.getParameter("name");//will return value
  11. println("Welcome "+name);
  12. close();
  13. }}

RequestDispatcher:

A challenge of ServletRequest is used to give the client request information to a servlet, for instance, content sort, content length, parameter names and qualities, header informations, attributes.

Methods of RequestDispatcher interface:
  1. public void forward(ServletRequest request, ServletResponse response)throws ServletException, java.io.IOException.
  2. public void include(ServletRequest request, ServletResponse response)throws ServletException, java.io.IOException.
forward( ) method: forward( ) methodInclude( ) method:include ( ) methodSyntax of getRequestDispatcher method:
  1. publicRequestDispatcher getRequestDispatcher(String resource);