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
4.Create form-param.jsp under WebContent folder
form-param.jsp
<%@ 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>
5. Create package called com.javatutorialscorner.jaxrs.formparam under RESTful-WebService
6. Create Java class FormParamService under com.javatutorialscorner.jaxrs.formparam package
FormParamService.java
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;
@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();
}
}
6.Configure Jersey Servlet Dispatcher
you need to configure REST as servlet in web.xml.
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">
<display-name>JAX-RS-Path</display-name>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.javatutorialscorner.jaxrs.formparam</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>
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
http://localhost:8080/RESTful-WebService/form-param.jsp
output
http://localhost:8080/RESTful-WebService/rest/tutorial/jtc
http://localhost:8080/RESTful-WebService/form-param.jsp
submit form to service
http://localhost:8080/RESTful-WebService/rest/tutorial/jtc
0 comments:
Post a Comment