JAX-RS @FormParam Web service example

In this tutorial we are going to see how to get HTML form parameters in java method using RESTful web service via URL using JAX-RS @FormParam. @FormParam used to bind HTML form parameters to java method


Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

Create maven web application project and add Jersey dependency in pom.xml. Find Jersey 2.x maven dependency configuration below. If you are new to maven refer our maven tutorials

Jersey 2.x dependency in pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.ehowtonow</groupId>
  <artifactId>RestTutorials</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>RestTutorials Maven Webapp</name>
  <url>http://maven.apache.org</url>
   <properties>
        <jersey2.version>2.19</jersey2.version>
        <jaxrs.version>2.0.1</jaxrs.version>
    </properties>
  <dependencies>
      <!-- JAX-RS -->
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>${jaxrs.version}</version>
        </dependency>
        <!-- Jersey 2.19 -->
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>${jersey2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>${jersey2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-client</artifactId>
            <version>${jersey2.version}</version>
        </dependency>
  </dependencies>
  <build>
    <finalName>RestTutorials</finalName>
  </build>
</project>

Create form-param.jsp under webapp folder

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="rest/tutorial/jtc" method="post">
Site : <input type="text" name="site"/>
<br/>
Tutorials :<input type="text" name="tutorial"/>
<br/>
Rank :<input type="text" name="rank"/>
<br/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>

FormParamService.java

package com.ehowtonow.webservice;

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

@Path("/tutorial")
public class FormParamService {

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

 }
}

Configure Jersey 2.x Servlet dispatcher

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
	<display-name>Restful Webservice Example</display-name>
	<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.ehowtonow.webservice</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>

Now you can run the jsp  and access the service by submit the form

run jsp

http://localhost:8080/RestTutorials/form-param.jsp

call service by submit form using post method

http://localhost:8080/RestTutorials/rest/tutorial/jtc


Ask your questions in eHowToNow Forum

To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

Posts You may Like

  1. How to Read File using BufferedReader
  2. JAX-RS to Produce JSON using Jackson – Jersey 1.x
  3. Create Pretty Print JSON using GSON
  4. How to find min and max value from Collection
  5. Java Best Practice Reuse Objects instead of creating new ones if possible
  6. Java 8 New Features Overview
  7. Java 8 Stream Collectors groupingBy with sort Example
  8. How to validate IPv4 Address using Java Regular Expression
  9. How to search file from directory using Java
  10. How to send Email with Attachment using Java
  11. How to configure Maven to use Java 8
  12. JAX-RS @PathParm Web service Example
  13. How to delete file using Java
  14. Log4j Example using XML configuration
  15. RESTful Web Services Introduction

Be the first to comment

Leave a Reply

Your email address will not be published.


*