Home » Enterprise Java » servlet » Get Request Parameter in Servlet

About Ilias Tsagklis

Ilias Tsagklis
Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

Get Request Parameter in Servlet

With this tutorial we shall show you how to get request parameter in a Java Servlet. This the most basic step you have to consider when developing a Servelt application because HTTP is based mostly on parameters exchange. You will find this example particularly useful when you want to get the value of known parameter.

Basically in order to get Request Parameter in Servlet, one should take the following steps:

  • Create a handleRequest method so you can use it both in doGet and doPost methods.
  • Use HttpServletRequest.getParameterValues(paramName) to get the parameters values.
 
Let’s see the simple code snippets that follow:

package com.javacodegeeks.snippets.enterprise;

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

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

public class GetRequestParameterInServlet extends HttpServlet {

	private static final long serialVersionUID = -2128122335811219481L;

	public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
		handleRequest(req, res);
	}

	public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
		handleRequest(req, res);
	}

	public void handleRequest(HttpServletRequest req, HttpServletResponse res) throws IOException {

		PrintWriter out = res.getWriter();
		res.setContentType("text/plain");

		String paramName = "myparam";
		String paramValue = req.getParameter(paramName);

		out.write(paramName);
		out.write(" = ");
		out.write(paramValue);
		out.write("n");

		paramName = "UNKNOWN";
		paramValue = req.getParameter(paramName);

		if (paramValue==null) {
			out.write("Parameter " + paramName + " not found");
		}

		out.close();

	}

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  version="2.5">

	<display-name>JCG Snippets Web Project</display-name>

	<servlet>
		<servlet-name>JCG Snippets Application</servlet-name>
		<servlet-class>com.javacodegeeks.snippets.enterprise.GetRequestParameterInServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>JCG Snippets Application</servlet-name>
		<url-pattern>/jcgservlet</url-pattern>
	</servlet-mapping>

</web-app>

URL:

http://myhost:8080/jcgsnippets/jcgservlet?myparam=paramvalue

Output:

myparam = paramvalue
Parameter UNKNOWN not found

 
This was an example on how to get Request Parameter in Servlet.

(No Ratings Yet)
1 Comment Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
alnebras Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
alnebras
Guest
alnebras

Hi
when I was type parameter in URL
this is an error occurred
The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.