It is common in web applications to pass data or parameters from one to another page. To retrieve the value of various elements in a HTML form or from another page, we can use the following method which is available in HttpServletRequest object:
- String getParameters(String name)
- Enumeration getParameterName()
- String[] getParamterValue(String name)
- Client sends a HTTP request from web browsers containing a URL with servlet.
- Web server receives the request and forwards the request to application servers.
- Using information available in xml (deployment descriptor) the servlet container loads the object appropriate servlet classes.
- If required, the servlet retrieves data from database, processes it and sends the response back to web servers.
- Web server forward the response back to the client who sent the requests.
Following example demonstrate retrieving data from a login page to a servlet:
login.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Code4Example</title> </head> <body> <body> <form action="auth.html" method="get"> Username: <input type="text" name="txtusers"><br> Password: <input type="password" name="txtpasss"><br> <input type="submit" value="Submit"> <input type="reset" value="Clear"> </form> </body> </body> </html> |
web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?xml version="1.0" encoding="UTF-8"?> <web-app> <servlet> <servlet-name>Authenticates</servlet-name> <servlet-class>Authenticates</servlet-class> </servlet> <servlet-mapping> <servlet-name>Authenticates</servlet-name> <url-pattern>/auth.html</url-pattern> </servlet-mapping> </web-app> |
Authenticate.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class Authenticate */ public class Authenticate extends HttpServlet { private static final long serialVersionUID = 1 L; /** * @see HttpServlet#HttpServlet() */ public Authenticate() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("txtuser"); String password = request.getParameter("txtpass"); if (username.equals("vishnu") && password.equals("college")) { response.getWriter().print("Valid user!" "); } else { response.getWriter().print("Bad username or password"); } } } |
In the above example we are retrieving the data from login page using the getParameter() method.
Take your time to comment on this article.