The ServletRequest interface have many methods to handle client requests to access a servlet. It is used for access client requests in servlet. HttpServletRequest extends to ServletRequest.
Below example will help you to batter understand :
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>ServletDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/servlet</url-pattern>
</servlet-mapping>
</web-app>
index.html
<form action="servlet" method="get">
<input type="text" name="name"><br>
<input type="submit" value="login">
</form>
ServletDemo.java
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class ServletDemo extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
String name=req.getParameter("name");
out.println("Welcome "+name);
out.close();
}}
0 Comment(s)