Results 1 to 1 of 1
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Rep Power

    Default a restless arg in Restful

    I do not know if I am at the right thread subject. All these different Java webpage communication models are still a bit confusing to me! The code example concerning my question is part of the Restful-concept which is close to servlets, right?

    But the button line of all this Java web-activity is "simple":
    1) open one or a series of server-sockets waiting for clients;
    2) "bind" an incoming client to a client-socket and start a thread with each one (better from a thread-pool);
    3) hook-up the client socket with incoming and outgoing streams like readers and writers and start to communicate through some protocol* (here the neck pain starts for me)….
    4) And try to do all this from-out the new Java 7 classes concerning Non Blocking I/O: wow!

    *when Restful that protocol is HTTP, right?

    That is all to it, right?

    My concrete question concerning the next JSP file and the two beans that take care of the server communication is:

    What is the arg "application" (see line 16 first code example) referring to since it is only mentioned once and it is not a field of some kind (I believe)?

    Java Code:
    <jsp:useBean id    = "preds" 
    	     type  = "predictions.Predictions" 
    	     class = "predictions.Predictions"> 
        // Check the HTTP verb: if it's anything but GET, 
        // return 405 (Method Not Allowed).
         String verb = request.getMethod();
         if (!verb.equalsIgnoreCase("GET")) {
                              "GET requests only are allowed.");
         // If it's a GET request, return the predictions.
         else {
           // Object reference application has the value 
           // pageContext.getServletContext()
    Java Code:
    package predictions;
    public class Prediction implements Serializable {
        private String who;   // person
        private String what;  // his/her prediction
        public Prediction() { }
        public void setWho(String who) {
    	this.who = who;
        public String getWho() {
    	return this.who;
        public void setWhat(String what) {
    	this.what = what;
        public String getWhat() {
    	return this.what;

    Java Code:
    package predictions;
    import java.beans.XMLEncoder; // simple and effective
    import javax.servlet.ServletContext;
    public class Predictions {
        private int n = 32;
        private Prediction[ ] predictions;
        private ServletContext sctx;
        public Predictions() { }
        // The ServletContext is required to read the data from
        // a text file packaged inside the WAR file
        public void setServletContext(ServletContext sctx) {
    	this.sctx = sctx;
        public ServletContext getServletContext() { 
    	return this.sctx; 
        // getPredictions returns an XML representation of
        // the Predictions array
        public void setPredictions(String ps) { } // no-op
        public String getPredictions() {
    	// Has the ServletContext been set?
    	if (getServletContext() == null) return null;      
    	// Have the data been read already?
    	if (predictions == null) populate(); 
    	// Convert the Predictions array into an XML document
    	return toXML();
        //** utilities
        private void populate() {
    	String filename = "/WEB-INF/data/predictions.db";
    	InputStream in = sctx.getResourceAsStream(filename);
    	// Read the data into the array of Predictions. 
    	if (in != null) {
    	    try {
    		InputStreamReader isr = new InputStreamReader(in);
    		BufferedReader reader = new BufferedReader(isr);
    		predictions = new Prediction[n];
    		int i = 0;
    		String record = null;
    		while ((record = reader.readLine()) != null) {
    		    String[] parts = record.split("!");
    		    Prediction p = new Prediction();
    		    predictions[i++] = p;
    	    catch (IOException e) { }
        private String toXML() {
    	String xml = null;
    	try {
    	    ByteArrayOutputStream out = new ByteArrayOutputStream();
    	    XMLEncoder encoder = new XMLEncoder(out);
    	    encoder.writeObject(predictions); // serialize to XML
    	    xml = out.toString(); // stringify
    	catch(Exception e) { }
    	return xml;
    Last edited by willemjav; 05-04-2014 at 09:13 AM.

Similar Threads

  1. Java restful api jax -rs with ssl
    By nihar_deb in forum New To Java
    Replies: 0
    Last Post: 05-24-2011, 04:01 AM
  2. RESTful Web Services
    By Akynz in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 04-12-2011, 04:48 PM
  3. RestFul Webservice
    By deepaksurthi in forum XML
    Replies: 0
    Last Post: 10-26-2010, 01:27 PM
  4. RESTful Web Service
    By aakinn in forum New To Java
    Replies: 0
    Last Post: 04-13-2010, 04:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts