×
☰ See All Chapters

FormParam Example

@FormParam allows the rest service consumer to pass the input in HTML, JSP forms. Values should be passed from input html tags and should be inside form tag. In this tutorial you will learn to pass the input to rest service and extract the input parameters using @FormParam. Follow the below steps to create the example.

Step 1: Create a dynamic web project.

 formparam-0
 

Step 2: Add Jersey implementation Jars by downloading from the following location.

(Google search key word “jax rs jersey jar download”)

https://jersey.github.io/download.html

Or click here to download the jars that we have used.

Now we will get jaxrs-ri-2.28.zip, extract the zip file. Copy all the jars present in lib, ext and api directories into “WebContent/WEB-INF/ lib” directory of project.

                  formparam-1
 

Step 3: Now create a Restful services using QueryParam annotations.

RestServiceFormParam.java

package com.java4coding;

 

import javax.ws.rs.FormParam;

import javax.ws.rs.POST;

import javax.ws.rs.Path;

import javax.ws.rs.Produces;

import javax.ws.rs.core.MediaType;

 

@Path("/hello")

public class RestServiceFormParam {

        @POST

        @Produces(MediaType.TEXT_PLAIN)

        public String sayHello(@FormParam("firstName") String firstName, @FormParam("lastName") String lastName) {

                return "Hello " + firstName+ " "+ lastName;

        }

}

Step 5: In web.xml, configure service as below.

web.xml

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

<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns="https://java.sun.com/xml/ns/j2ee" xmlns:web="https://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="https://java.sun.com/xml/ns/j2ee https://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4">

  <servlet>

    <servlet-name>jersey-serlvet</servlet-name>

    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

    <init-param>

      <param-name>jersey.config.server.provider.packages</param-name>

      <param-value>com.java4coding</param-value>

    </init-param>

    <load-on-startup>1</load-on-startup>

  </servlet>

  <servlet-mapping>

    <servlet-name>jersey-serlvet</servlet-name>

    <url-pattern>/rest/*</url-pattern>

  </servlet-mapping>

</web-app>

Step 6: Create client html form.

client.html

<html>

<body>

    <form action="https://localhost:8090/JAX_RS_FormParamAnnotationExample/rest/hello" method="post">

        First Name : <input type="text" name="firstName" /><br></br>

        Last Name :<input type="text" name="lastName" /><br></br>

                <input type="submit" value="Submit" />            

    </form>

</body>

</html>

Final project directory structure is

formparam-2
 

Step 5:  Deploy the project to any server (Lets consider Tomcat), Just right click on the project >Run As > Run on Server >Select Tomcat server.

Open client.html in web browser

formparam-3
 

Enter First Name and Last Name and click Submit.

formparam-4
 

After clicking on Submit

formparam-5
 

All Chapters
Author