Home » Enterprise Java » rest » JAX-RS @FormParam Example

About Nikos Maravitsas

Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. During his studies he discovered his interests about software development and he has successfully completed numerous assignments in a variety of fields. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.

JAX-RS @FormParam Example

In this example we are going to see how to parse parameters submited by a form in a JAX-RS REST Service using @FormParam annotation.

In this example we are not going to focus on how to create JAX-RS application from top to bottom. So make sure you read carefully Jersey Hello World Example and RESTEasy Hello World Example, and pay attention to the sections concerning the creation of the project with Eclipse IDE as well as the deployment of the project in Tomcat.

The code of this tutorial is going to be based on Jersey Hello World Example. You can download the Eclipse project of this tutorial here : JAXRS-HelloWorld.zip

1. Project structure

Let’s remind ourselves the structure of the project we are working on:


At this point you can also take a look at the web.xml file to see how the project is configured:


<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Restful Web Application</display-name>



As you can see our servlet is mapped to /rest/ URI pattern. So the basic structure of the URIs to reach the REST Service will have the form :


So let’s see how to use @FormParam annotation.

2. Create a new HTML File

This is of course to host a simple HTML form to demonstrate the use of @FormParam annotation. Go to the Package Explorer, Right Click on the project -> New -> HTML File. The new file will be created in the WebContent folder.


So this would be the final project structure:


Now open form.html file and paste the following code:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN" "http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Form Page</title>
<h1>Submit the following form</h1>

	<form action="rest/members/info" method="post">
			First Name : <input type="text" name="fname" />
			Last Name : <input type="text" name="lname" />
		<input type="submit" value="Submit" />

3. REST Servive

Open HelloWorldREST.java and paste the following code:


package com.javacodegeeks.enterprise.rest.jersey;

import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

public class HelloWorldREST {

	public Response responseMsg(@FormParam("fname") String fname,	@FormParam("lname") String lname ) {

		String output = "This all the info about "+fname +" "+lname;
		return Response.status(200).entity(output).build(); 


So as you can see we are going to parse two parameters submited from the above form. Tha parameter naming is obvious.

4. Run the example

After deploying your Application (I used Tomcat for this), open your browser and go to:



You will see the following form. After typing the “First Name” and “Last Name”, hit “Submit” :


After submiting the form:

rest-service responce

5. Using MultivaluedMap<String, String>

You can use MultivaluedMap<String, String> to represent all form data. You will find it particularly useful when you have a form with a lot of input variables. Let’s see how:


package com.javacodegeeks.enterprise.rest.jersey;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;

public class HelloWorldREST {

	public Response responseMsg(MultivaluedMap<String, String> parameter1) {

		String output = " Form parameters :\n";
		for (String key : parameter1.keySet()) {
			output += key + " : " + parameter1.getFirst(key) +"\n";


		return Response.status(200).entity(output).build();

Open your browser and go to:



You will see the following form. After typing the “First Name” and “Last Name”, hit “Submit” :


After submitting the form:


Download Eclipse Project

This was an example on JAX-RS @FormParam annotation. Download the Eclipse Project of this example: JAXRS-HelloWorld.zip

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


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments