In this example we are going to use @POST and @FORMPARAM Annotations. For this example we are going to have the following files:
- WEB.xml
- POSTExample.java
- index.html
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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>com.tushar.jersey.Project</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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.tushar.jersey.Project</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>
|
PostExample.java
package com.tushar.jersey.Project;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
@Path("/product")
public
class PostExample{
@POST
@Path("/add")
public Response addUser(
@FormParam("id") int id,
@FormParam("name") String name,
@FormParam("price") float price) {
return Response.status(200)
.entity(" Product added
successfuly!<br> Id: "+id+"<br> Name: " +
name+"<br> Price: "+price)
.build();
}
}
|
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Form Details</title>
</head>
<body>
<form action="rest/information/add"
method="post">
Enter Id: <input type="text"
name="id" /><br />
<br /> Enter Name: <input type="text"
name="name" />
<br /> Enter Price:<input type="text"
name="price"/><br/><br/>
<input type="submit"
value="Add Product"/>
</form>
</body>
</html>
|
Run the Server:
Start the server and paste the following URL in the browser
URI: localhost:8080/com.tushar.jersey.Project/index.html
URI: localhost:8080/com.tushar.jersey.Project/index.html


No comments:
Post a Comment