Jax-rs form parameters jersey example
TestWS.java
import javax.ws.rs.FormParam; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.core.Response; @Path("/student") public class TestWS { @POST @Path("/add") public Response addStudent( @FormParam("name") String name, @FormParam("rollNo") String rollNo) { String output = "Student Name: " + name + ", Roll No.: " + rollNo; return Response.status(200).entity(output).build(); } } |
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <servlet> <display-name>JAX-RS REST Servlet</display-name> <servlet-name>JAX-RS REST Servlet</servlet-name> <servlet-class> com.sun.jersey.spi.container.servlet.ServletContainer </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>JAX-RS REST Servlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> |
index.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <body> <h1>JAX-RS Form Parameter Example</h1> <form action="services/student/add" method="post"> Name : <input type="text" name="name" /> <br/><br/> Roll No : <input type="text" name="rollNo" /> <br/><br/> <input type="submit" value="Add Student" /> </form> </body> </html> |
Output:
Enter data.
Click on “Add Student” button.
Download this example.