Archives:

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,
[code lang=”xml”]<web-app>

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

</web-app>[/code]

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

struts-config.xml
[code lang=”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>[/code]


Java iText – Add Anchor / Link to PDF

[code lang=”java”]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();
}
}

}[/code]


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.

Output:

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