Saturday, May 10, 2014

Polymer and AngularJS two-way Data Binding

Lately I came across Polymer, and I hope and think that it will become the future of web development.
While trying to play with this framework (until it will become a web standard), I tried to integrate it with AngularJS because of Angular's data-binding features.

Here is a link to  's blog on how to make this two frame works live with each other and the problem with the two-way data binding. Shortly I will tell that it is easy to bind data from outside of custom Polymer elements to Polymer attributes but it is impossible to make Angular to listen to changes within the Polymer elements (which were passed to it via attributes) without help of some other library.

Seth Ladd in his blogs shows how to overcome this problem with angular with this angular_node_bind project. I searched some more and I found angu-poly project created by Matjaž Lipuš, for me it was easier to use.

I want to show you 2 examples of how to use the angu-poly library. Check the bellow links, the first one is one that I found while searching for examples, and the second one was created by me for making a more complex testing of this project:
1. first link.
2. second link.

I hope that in the near future the Angular team will make some improvements in order to make this two-way data binding built in with Angular without any need of third party library project.

Saturday, February 15, 2014

Making image available via url on tomcat

So I needed to make my tomcat server to give access from the url, something like: http://your_server_name/image_folder/image.png.

So what you need to do is to add path to your tomcat sever.xml file. If you using eclipse you should have a server project in your workspace, here is how it should be looked (yours would be probably called just Servers):
If you are not using eclipse find this file in your system.

Open this file (marked in blue on the image) and add this line of code in under your <host> tag:

 <Context docBase="full_path_to_image_folder" path="/image" />  

Note: full_path_to_image_folder  is something like C:/images.

After this just reset your tomcat server and try to access some image via your browser, here is an example:


You may need to restart your machine in order for the changes will take their place.

Wednesday, February 5, 2014

How to upload files from java client (Android) to java server

I needed to find a way to upload a file to my server from my Android application, so after searching the net I found the MultyPartEntityBuilder. I will show you both client and server.


 import org.apache.http.HttpEntity;  
 import org.apache.http.HttpResponse;  
 import org.apache.http.client.ClientProtocolException;  
 import org.apache.http.client.HttpClient;  
 import org.apache.http.client.methods.HttpPost;  
 import org.apache.http.entity.ContentType;  
 import org.apache.http.entity.mime.HttpMultipartMode;  
 import org.apache.http.entity.mime.MultipartEntityBuilder;  
 import org.apache.http.impl.client.DefaultHttpClient;  
1:       /**  
2:        * Executes request with file passing, different then other executions.  
3:        * @param url only server and servlet path  
4:        * @param keyOfString the key of the JSON data  
5:        * @param request the JSON request (example- {name: "some name", age: 27}
6:        * @param keyForFile the key for the file  
7:        * @param file the file to upload  
8:        * @return String if executed correctly, else returned null.  
9:        */  
10:       public String multiPartExecute(String url, String keyOfString, String request, String keyForFile, File file)  
11:       {  
12:            HttpClient client = new DefaultHttpClient();  
13:            HttpPost post = new HttpPost(url);  
14:            MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();  
15:            multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);  
16:            if(file != null)  
17:            {  
18:                 multipartEntity.addBinaryBody(keyForFile, file, ContentType.DEFAULT_BINARY, "name");  
19:            }  
20:            multipartEntity.addTextBody(keyOfString, request, ContentType.APPLICATION_JSON);  
21:            post.setEntity(;  
22:            HttpResponse response = null;  
23:            String line = "", output = "";  
24:            try  
25:            {  
26:                 response = client.execute(post);  
27:                 BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));  
28:                 while ((line = br.readLine()) != null)  
29:                 {  
30:                      output += line;  
31:                 }  
32:            }   
33:            catch (ClientProtocolException e)  
34:            {  
35:                 e.printStackTrace();  
36:                 return null;  
37:            }   
38:            catch (IOException e)  
39:            {  
40:                 e.printStackTrace();  
41:                 return null;  
42:            }  
43:            if (response.getStatusLine().getStatusCode() != 200)   
44:            {  
45:                 response.getStatusLine().getStatusCode();  
46:                 return null;  
47:            }  
48:            HttpEntity entity = response.getEntity();  
49:            try  
50:            {  
51:                 entity.consumeContent();  
52:            }   
53:            catch (IOException e)  
54:            {  
55:                 e.printStackTrace();  
56:                 return null;  
57:            }  
58:            client.getConnectionManager().shutdown();  
59:            return output;  
60:       }  


 import javax.servlet.annotation.MultipartConfig;  
 import javax.servlet.annotation.WebServlet;  
 import javax.servlet.http.HttpServletRequest;  
 import javax.servlet.http.HttpServletResponse;  
 import javax.servlet.http.Part;  
1:  @WebServlet("/MyServlet")  
2:  @MultipartConfig  
3:  public class MyServlet extends HttpServlet   
4:  {  
5:       private static final long serialVersionUID = 1L;  
6:       protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException   
7:       {  
8:            init(request,response);       
9:            String req = request.getParameter("requestKey");  
10:            Part filePart = request.getPart("fileKey");  
11:      }  
12:  }