728x90 AdSpace

Saturday, 30 November 2013

JAX-RS @FormParam

In this tutorial we are going to see how to get HTML form parameters in java method using RESTful web service via URL using @FormParam
@FormParam used to bind HTML form parameters  to java method
1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project.
2. Create the Project called RESTful-WebService
3. Add the following jar into WEF-INF/lib folder
add jar
4.Create form-param.jsp under WebContent folder
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<form action="rest/tutorial/jtc" method="post">
Site : <input type="text" name="site"/>
Tutorials :<input type="text" name="tutorial"/>
Rank :<input type="text" name="rank"/>
<input type="submit" value="Submit"/>

5. Create package called com.javatutorialscorner.jaxrs.formparam under RESTful-WebService
6. Create Java class FormParamService under com.javatutorialscorner.jaxrs.formparam package


package com.javatutorialscorner.jaxrs.formparam;

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

public class FormParamService {

public Response getTutorial(@FormParam("site") String site,
@FormParam("tutorial") String tutorial, @FormParam("rank") String rank) {
return Response
.entity("Web Site : " + site + " Tutorial : " + tutorial
+ " Rank : " + rank).build();


6.Configure Jersey Servlet Dispatcher
you need to configure REST as servlet in 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_3_0.xsd" id="WebApp_ID" version="3.0">


The servlet class available in jersey com.sun.jersey.spi.container.servlet.ServletContainer. The init param com.sun.jersey.config.property.package is used to define in which package jersey will look for the service classes.This package points to your resource class package. URL pattern is the part of base URL
Now you can run the jsp  and access the service by submit the form
run jsp


call service by submit form using post method




form jsp

submit form to service

Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments


Post a Comment

Item Reviewed: JAX-RS @FormParam Rating: 5 Reviewed By: Annamalai Thangaraj