Posts in java

How to display UTF-8 chars in a JSP

marzo 11th, 2011 Posted by java 0 thoughts on “How to display UTF-8 chars in a JSP”

1. <%@ page contentType=”text/html; charset=UTF-8″ pageEncoding=”UTF-8″ %>
2.
3. File -> Properties -> Resource -> Text File Encoding -> UTF-8
4. If you are using resource bundle you can use this: http://englove.blogspot.com/2011/01/how-to-use-utf-8-encoding-with.htm

StripTags in Java

enero 14th, 2011 Posted by java 0 thoughts on “StripTags in Java”
public class Utils {
   public static String stripTags(String text) {
        return text.replaceAll(“\<.*?\>”,””).trim();

   }
}

Java Class to use Goo.gl API with Gson

enero 13th, 2011 Posted by java 0 thoughts on “Java Class to use Goo.gl API with Gson”
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;

import com.google.gson.Gson;

public class Shortener {
    private static final String URL_GOOGL_SERVICE = “https://www.googleapis.com/urlshortener/v1/url”;
   
    private static final Gson gson = new Gson();
   
    public static String shorten(String longUrl) {
        String result = new String();
        GsonGooGl gsonGooGl = new GsonGooGl(longUrl);
       
        try {
            URL url = new URL(URL_GOOGL_SERVICE);
           
            URLConnection   urlConn = url.openConnection();
            urlConn.setDoInput (true);                                                            // Let the run-time system (RTS) know that we want input.
            urlConn.setDoOutput (true);                                                            // Let the RTS know that we want to do output.
            urlConn.setUseCaches (false);                                                        // No caching, we want the real thing.
            urlConn.setRequestProperty(“Content-Type”, “application/json”);                        // Specify the content type.
           
           
            DataOutputStream printout = new DataOutputStream (urlConn.getOutputStream ());        // Send POST output.
            String content = gson.toJson(gsonGooGl);
            printout.writeBytes (content);
            printout.flush ();
            printout.close ();
           
            DataInputStream input = new DataInputStream (urlConn.getInputStream ());            // Get response data.
           
            Scanner sc = new Scanner(input);
            while(sc.hasNext()) {
                result += sc.next();
            }
           
            GooGlResult gooGlResult = gson.fromJson(result, GooGlResult.class);
           
            return gooGlResult.getId();
        } catch (Exception ex) {
           System.out.println(ex);
           return null;
        }
    }
}

class GsonGooGl {
    public GsonGooGl(){}
    public GsonGooGl(String longUrl){
        this.longUrl = longUrl;
    }
   
    private String longUrl;

    public String getLongUrl() {
        return longUrl;
    }
    public void setLongUrl(String longUrl) {
        this.longUrl = longUrl;
    }
   
   
}

class GooGlResult {
    public GooGlResult(){}
   
    private String kind;
    private String id;
    private String longUrl;
   
    public String getKind() {
        return kind;
    }
    public void setKind(String kind) {
        this.kind = kind;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getLongUrl() {
        return longUrl;
    }
    public void setLongUrl(String longUrl) {
        this.longUrl = longUrl;
    }
   
   
}

To use:

System.out.println(Shortener.shorten(“http://goorkers.com”)); 

How to use UTF-8 encoding with ResourceBundle (Java)

enero 11th, 2011 Posted by java 0 thoughts on “How to use UTF-8 encoding with ResourceBundle (Java)”

By default ResourceBundle uses ISO-8859-1.

But you can use this excelent hack http://www.thoughtsabout.net/blog/archives/000044.html

How to get client ip in a Java Servlet

octubre 29th, 2010 Posted by java, servlet 0 thoughts on “How to get client ip in a Java Servlet”

req.getRemoteAddr();
req.getRemoteHost();

How to capture GET parameters in GWT

octubre 14th, 2010 Posted by gwt, java 0 thoughts on “How to capture GET parameters in GWT”

String param = Window.Location.getParameter(“param”);

NumberFormat in GWT

octubre 6th, 2010 Posted by gwt, java 0 thoughts on “NumberFormat in GWT”

String vat = NumberFormat.getFormat(“0.00”).format(country.getVAT());

I’m trying to learn english, if you find some mistakes in my redaction, please make a comment with my error

How to create a session in Java

octubre 4th, 2010 Posted by java 0 thoughts on “How to create a session in Java”

To create a session attribute:

HttpSession sesion = req.getSession(true);
sesion.setAttribute(“allowed”, “YES”);

To get a session parameter:

HttpSession sesion = req.getSession(true);
String allowed = (String) sesion.getAttribute(“allowed”);

I’m trying to learn english, if you find some mistakes in my redaction, please make a comment with my error

How to generate a random number in java

octubre 3rd, 2010 Posted by java 0 thoughts on “How to generate a random number in java”

int x = ((int) Math.random() * 100);

I’m trying to learn english, if you find some mistakes in my redaction, please make a comment with my error

How to rollback an unsucesful deploy to appengine

septiembre 30th, 2010 Posted by appengine, java, mac 0 thoughts on “How to rollback an unsucesful deploy to appengine”

sh /paht/to/appengine-sdk/bin/appcfg.sh rollback path/to/project/war/

Copyright © 2018 programadorfreelanceargentina.com

Programador Freelance Argentina