Java Struts – Handle HTTP 404 Error

The “HTTP Status 404” error code shows that the system can not find the page or resource you requested. In practice, you should display your custom 404 error page. However, it is handle in web.xml , not in Struts framework. For example,

<web-app>
...
<error-page>
<error-code>404</error-code>
<location>/pages/error404.jsp</location>
</error-page>

</web-app>

When system hit the 404 error, it will forward to your custom 404 error page “/pages/error404.jsp“.

struts-config.xml

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Maven Struts Examples</display-name>

<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>
/WEB-INF/struts-config.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<error-page>
<error-code>404</error-code>
<location>/pages/error404.jsp</location>
</error-page>

</web-app>

Java iText – Add Anchor / Link to PDF

import com.itextpdf.text.Anchor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class AnchorDemo {

	public static void main(String[] args) {
		Document document = new Document();
		try {
			PdfWriter.getInstance(document,
					new FileOutputStream("AnchorDemo.pdf"));
			document.open();

			String content = "You can learn Java programming on the " + "following website: ";
			Paragraph paragraph = new Paragraph(content);

			//
			// Creates a new anchor that link to external website
			// and add this anchor to the paragraph.
			//
			Anchor anchor = new Anchor("javac - Java Examples Source Code");
			anchor.setReference("http://javac.in");
			paragraph.add(anchor);

			document.add(paragraph);
		} catch (DocumentException | FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			document.close();
		}
	}

}

Java – Generate Random alpha-numeric String

The code below show you how to use the apache commons RandomStringUtils class to generate some random string data.

import org.apache.commons.lang.RandomStringUtils;

public class RandomStringUtilsDemo {

	public static void main(String[] args) {
		//
		// Creates a 64 chars length random string of number.
		//
		String result = RandomStringUtils.random(64, false, true);
		System.out.println("random = " + result);

		//
		// Creates a 64 chars length of random alphabetic string.
		//
		result = RandomStringUtils.randomAlphabetic(64);
		System.out.println("random = " + result);

		//
		// Creates a 32 chars length of random ascii string.
		//
		result = RandomStringUtils.randomAscii(32);
		System.out.println("random = " + result);

		//
		// Creates a 32 chars length of string from the defined array of
		// characters including numeric and alphabetic characters.
		//
		result = RandomStringUtils.random(32, 0, 20, true, true, "qw32rfHIJk9iQ8Ud7h0X".toCharArray());
		System.out.println("random = " + result);
	}

}

Output:

random = 2377316738997278218735578855577798247976319451877477254850564896
random = wwqbLxlRynIdptAoxuSIfbABRoOFHLyKaFEscrUoBQjAHPOkcIqfHuMnhXVzaLCf
random = 6nQ0PJf<#VgIi=’&d`*`Ru#i_exMoHXb
random = ZGbelXYEiLrlLBXKroCIDJPrevZPIGkD

Page 1 of 3123