Wednesday, April 24, 2019

Oracle: using DATE type in the WHERE clause condition


If the WHERE clause contains a condition of DATE type, we can use TO_DATE() to convert a date string into DATE type. For example:

SELECT * FROM MyTable WHERE DateField > TO_DATE('2019-04-23 21:30:00', 'YYYY-MM-DD HH24:MI:SS');

The second parameter of TO_DATE() method is the format model. To find out the complete list of datetime format elements, follow this link: https://docs.oracle.com/cd/E11882_01/server.112/e41084/sql_elements004.htm#CDEHIFJA


Tuesday, April 23, 2019

ASP.NET: logout user and invalidate the session


When logging out the user, we need to abandon the session on the server side and remove the session id from the client side. For example, in the Page_Load() method:

if (!IsPostBack) {
  // Invalidate the old session. A new session will be started.
  Session.Abandon();

  // Clear the session ID from the client side. 
  // Otherwise, the old session ID will be recycled by default.
  Response.Cookies["ASP.NET_SessionId"].Value = "";
}

Monday, April 22, 2019

Java: example of Lambda Expressions and Method References


In this post, we showed an example of using anonymous class in the forEach method to loop over a list.

stringList.forEach(new Consumer<String>() {
    @Override
    public void accept(String s) {
        String oneItem = s;
        System.out.println(oneItem);
    }
});


Because the Consumer interface is a functional interface (https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html), the above code can be simplified with Lambda Expressions (https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html)


stringList.forEach((s) -> {
        String oneItem = s;
        System.out.println(oneItem);
});


or even:

stringList.forEach(s -> System.out.println(s));

And because the above Lambda Expresso does nothing but call only an existing method, we can use Method References to write the code this way:

stringList.forEach(System.out::println);


Sunday, April 21, 2019

Google Chrome: print in black and white


Press Ctrl-P in Google Chrome opens the Print dialogue. To print the current page in black and white:
  1. Select the printer for the Destination option.
  2. Select Black and white for the Color option.
However, the Black and white may not always work. It depends on whether the printer supports it.

The other values in the Destination option may also have issues with the Black and white option. When selecting Microsoft Print to PDF, the generated PDF file will be in color. Only when selecting Microsoft XPS Document Writer, the generated XPS file will be in back and white.

Saturday, April 20, 2019

Java: four ways of looping over a List


List<String> stringList = new LinkedList<String>();
stringList.add("item1");
stringList.add("item2");
stringList.add("item3");

System.out.println("loop 1:");
for (int i = 0; i < stringList.size(); i++) {
    String oneItem = stringList.get(i);
    System.out.println(oneItem);
}

System.out.println("loop 2:");
Iterator<String> iterator = stringList.iterator();
while (iterator.hasNext()) {
    String oneItem = iterator.next();
    System.out.println(oneItem);
}

System.out.println("loop 3:");
for (String oneItem : stringList) {
    System.out.println(oneItem);
}

// Supported by Java 8 and plus
System.out.println("loop 4:");
stringList.forEach(new Consumer<String>() {
    @Override
    public void accept(String s) {
        String oneItem = s;
        System.out.println(oneItem);
    }
});


With the use of Lambda Expressions and Method References (explained in this post), the last loop can be replaced with:

stringList.forEach(System.out::println);


The output of the above four loops in the example is:
loop 1:
item1
item2
item3
loop 2:
item1
item2
item3
loop 3:
item1
item2
item3
loop 4:
item1
item2
item3

Friday, April 19, 2019

IIS Web Server: where is the IIS logs of the web site


IIS logs the requests to your web site. To find out where those logs are:
  1. Start the IIS Manager.
  2. On the left side of the IIS Manager, under the Connections section, expand the Sites folder.
  3. Find your web site and click on it.
  4. Find the Logging icon in the center panel. Double click on it.
  5. On the Logging page, find the location of the logging directory in the Directory box.

Thursday, April 18, 2019

Gmail: forward all emails to another email address


1. Click on the Settings (Gear) icon at the top right corner of Gmail. Select Settings.
2. Click on the Forwarding and POP/IMAP tab.
3. In the Forwarding section, click on the Add a forwarding address button.
4. Input the destination email address in the dialogue to add it.
5. An email will be sent to the destination email address to ask for permission.
6. Login to the destination email account to retrieve that email. Click on the link given in that email, or fetch the Confirmation Code.
7. Go back to the forwarding email and enter the Forwarding and POP/IMAP setting again. Click on the radio button of Forward a copy of incoming mail to and select the destination email address. If you choose the Confirmation Code way, input the code to validate the email address.
8. There are some options to let you choose what to do with the original Gmail copy. E.g., you can keep a copy or delete the copy from the original Gmail account.
 

Wednesday, April 17, 2019

ASP.NET: create an error page to display unhandled exceptions


Step 1: In Global.aspx, create the Application_Error() method to trap the error:

<script runat="server">
... ...
  void Application_Error(object sender, EventArgs e)
  {
    // Transfer the server error to the error page.
    Server.Transfer("~/ErrorPage.aspx");
  }
 ... ...
</script>

Step 2: Add the error message to ErrorPage.aspx:

<body>
... ...
<p><asp:Label ID="errorMessage" runat="server" /></p>
... ...
</body>

Step 3: In the code-behind of ErrorPage.aspx, i.e. ErrorPage.aspx.cs, add code in Page_Load:

protected void Page_Load(object sender, EventArgs e)
{
  Exception ex = Server.GetLastError();
  if (ex != null && ex.GetType() == typeof(HttpUnhandledException))
    ex = ex.InnerException;

  if (ex != null)
    errorMessage.Text = ex.Message;

  Server.ClearError();
... ...
}

However, for security reasons, this version of ErrorPage.aspx should be used only in a development environment. In production, you don't want to display the exceptions to the end user because that might leak information of your system.

Windows: find out who has logged on to the system


  1. Open the Event Viewer as an Admin user: right click on the Start button and select Command Prompt (Admin). Run command eventvwr.msc.
  2. On the left side of the Event Viewer dialogue, select Windows Logs|Security from Event Viewer.
  3. Events are listed in the center part of the dialogue.
  4. On the right side of the dialogue, select Filter Current Log... from Actions|Security to open the filter dialogue.
  5. In the Filter Current Log dialogue, look for Event sources near the center. Click on the drop down button and select Microsoft Windows security auditing from the list.
  6. Find Task category below. Click on its drop down menu and multi-select Logon, Special Logon, Other Logon/Logoff Events.
  7. Click on the OK button to close the Filter Current Log dialogue.
  8. Go back to the list of all the logon events and click on them to see the details in the panel below.
The description of Special Logon and Other Logon can be found on Microsoft's web site:
https://docs.microsoft.com/en-us/windows/security/threat-protection/auditing/audit-special-logon
https://docs.microsoft.com/en-us/windows/security/threat-protection/auditing/audit-other-logonlogoff-events

Tuesday, April 16, 2019

Example of using Checkstyle for coding standard


As it claims on its web site, Checkstyle is a development tool to help programmers write Java code that adheres to a coding standard. It can be downloaded from http://checkstyle.sourceforge.net/. The downloadable binary is a JAR file, e.g. checkstyle-8.19-all.jar.

If you want to follow the Google Java Style (https://google.github.io/styleguide/javaguide.html), you can run Checkstyle with the built-in Google style like this:

$ java -jar checkstyle.jar -c /google_checks.xml MyClass.java

-- checkstyle.jar is the downloaded JAR file from the Checkstyle's site.
-- /google_checks.xml tells Checkstyle to use the built-in style.

You may write your own style but a more convenient way is to download one of the default style configuration and modify it to suit your needs.

For example, the built-in google_checks.xml defines the code indentation offset as 2. If we want to set it to 4 for our own use, we can download google_checks.xml from https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/google_checks.xml. Open it with a text editor, look for the Indentation section and change the values from 2 to 4, and 4 to 8:

        <module name="Indentation">
            <property name="basicOffset" value="4"/>
            <property name="braceAdjustment" value="0"/>
            <property name="caseIndent" value="4"/>
            <property name="throwsIndent" value="8"/>
            <property name="lineWrappingIndentation" value="8"/>
            <property name="arrayInitIndent" value="4"/>
        </module>


Then we save the file as my_style_checks.xml.

To use a customized configuration, we run Checkstyle as:

$ java -jar checkstyle.jar -c /path/to/my_style_checks.xml MyClass.java

-- after -c we need to specify the absolute or relative path to the new style file we created.
 
Get This <