1. Netbeans 6.9
2. Create a project Helloworld
3. Deploy and test the service
4. Right Click on project properties set relative url as
resources/helloworld
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package helloworld;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.PathParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.xml.bind.JAXBElement;
import javax.ws.rs.FormParam;
import javax.ws.rs.QueryParam;
import java.util.ArrayList;
import java.util.List;
/**
* REST Web Service
*
* @author localadmin
*/
@Path("helloworld")
public class HelloWorld {
@Context
private UriInfo context;
/** Creates a new instance of HelloWorld */
public HelloWorld() {
}
/**
* Retrieves representation of an instance of helloworld.HelloWorld
* @return an instance of java.lang.String
*/
@POST
// @Produces("text/html")
@Produces("application/xml")
// @Consumes("application/x-www-form-urlencoded")
// public List<userData> getHtml(@QueryParam("name") String user_id) {
public List<userData> getHtml(@FormParam("test") String user_id) {
userData toReturn = new userData();
toReturn.name = user_id;
List<userData> retUser = new ArrayList<userData>();
retUser.add(toReturn);
return retUser;
}
/**
* PUT method for updating or creating an instance of HelloWorld
* @param content representation for the resource
* @return an HTTP respons with content of the updated or created
resource.
*/
@PUT
@Consumes("text/html")
public void putHtml(String content) {
}
}
2nd Class
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package helloworld;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class userData {
public String name;
}
And the post html
Reply
Forward
Reply
|
Harshal Shah
show details 8:11 PM (1 hour ago)
<html>
<head>
<script src=
"
http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"
type="text/javascript">
</script>
<script type="text/javascript">
function register(){
//
window.open("/welcome.html",'welcome','width=300,height=200,menubar=yes,status=yes,location=yes,toolbar=yes,scrollbars=yes');
alert("hey");
$.ajax({
type: "GET",
url: "
http://localhost:8080/HelloWorld/resources/helloworld",
data: "user_id=" + document.getElementById("user_id").value ,
success: function(msg){
window.alert(msg);
},
error: function(xmlHttpRequest, status, err) {
alert('Status ' + status + ', Error ' + err);
}
});
}
</script>
</head>
<body>
<form action="
http://localhost:8080/HelloWorld/resources/helloworld"
method="post">
<textarea name="test" rows="8" cols="40" value="tangeo"></textarea>
<input type="text" id="name" name="name" value="harshal"/>
<input type="submit" value="submit"/>
<div id="response">
hello
</div>
</form>
</body>
</html>
--
View this message in context: http://jersey.576304.n2.nabble.com/Need-help-on-Jersey-RestFul-WebService-tp5481613p5633536.html
Sent from the Jersey mailing list archive at Nabble.com.