java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to read request parameter from servlet?

When creating an application with Java servlet most of the time we will work with the request and response object. From the request object we can read the parameter submitted by the user's browser either through an HTTP GET or POST method.

Basically what you need to know is when you try to get the passed parameter from inside the servlet you can call the request.getParameter(paramname) where the paramname is the name of paramater that you want to read from the servlet request object.

In this example I'll show you how to read the parameter to process user action in a very simple login servlet. In this example we'll create a login form, a jsp page that accept user input for username and password.

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>Login Page</title>
</head>
<body>
	<form id="loginForm" action="/java-web-examples/login" method="post">
		<label>Username</label>
		<input type="text" name="username" />
		<label>Password</label>
		<input type="password" name="password" />
		<input type="submit" value="Login" />
	</form>
</body>
</html>

In this form you'll have to input box for username and password. You also have a submit button for executing the login procces. Now we have the form, let's create the login servlet.

package com.javacoderanch.example.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet implements Servlet {
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		doLogin(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		doLogin(request, response);
	}
	
	protected void doLogin(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
                
                //
                // Here we read the parameters from servlet request
                //
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		PrintWriter pw = response.getWriter();
		if (username != null && username.equals("administrator") 
				&& password != null && password.equals("secret")) {
			// authentication accepted!
			pw.println("Success!");
		} else {
			// authentication denied!
			pw.println("Denied!");
		}
		pw.close();
	}
}	

For our servlet to work you must register the servlet in the web.xml file under the WEB-INF folder. You can find the configuration below.

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>java-web-examples</display-name>
	<servlet>
		<description>
		</description>
		<display-name>LoginServlet</display-name>
		<servlet-name>LoginServlet</servlet-name>
		<servlet-class>com.javacoderanch.example.servlet.LoginServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>LoginServlet</servlet-name>
		<url-pattern>/login</url-pattern>
	</servlet-mapping>
</web-app>

Now you have everything, you can deploy the application on your servlet container, for example Apache Tomcat. Access you login page in the following address:

http://localhost:8080/java-web-examples/login.jsp

You can also access the servlet directly from the following url:

http://localhost:8080/java-web-examples/login

To pass the username and password information you can append the parameter like:

http://localhost:8080/java-web-examples/login?username=administrator&password=secret 

This will call the servlet and validate your login information.