Java Servlet – Add Cookie

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class WriteCookieExample extends HttpServlet {

protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");

//
// Send a cookie named username to the client. There are some others
// properties that we can set before we send the cookie, such as comments,
// domain name, max age, path, a secure flag, etc.
//
Cookie cookie = new Cookie("username", "jduke");
response.addCookie(cookie);
}

protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
}
}

Java Servlet – Get Application Context Path

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;

public class ContextPathDemo extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

//
// HttpServletRequest.getContextPath() returns the portion of the
// request URI that indicates the context of the request.
//
String contextPath = req.getContextPath();
}
}

Java – Get Currency Symbol

import java.util.Currency;
import java.util.Locale;

public class Main {
  public static void main(String[] args) {
    Currency currency = Currency.getInstance(Locale.JAPAN);
    System.out.println("Currency.getSymbol() = " + currency.getSymbol());

    currency = Currency.getInstance(Locale.UK);
    System.out.println("Currency.getSymbol() = " + currency.getSymbol());

    currency = Currency.getInstance(Locale.US);
    System.out.println("Currency.getSymbol() = " + currency.getSymbol());

    currency = Currency.getInstance(new Locale("in", "ID"));
    System.out.println("Currency.getSymbol() = " + currency.getSymbol());
  }
}

Page 1 of 3123