JavaExample.net
  • About
  • Articles
  • Cookie Policy and Privacy

Building keystore from existing private keys and certificates

By admin On 2 November 2012 · Add Comment
Prerequisites: – a private key in PEM format and her passphrase; – a certificate in PEM format firmed by authority; – any authority certificate (the chain) in PEM format. We are describing how create a java keystore starting from our prerequisites PEM files. Let’s assume you have a private key (key.pem) and a certificate (cert.pem), [...]
[Continue Reading...]

Serialize a java bean into string with ReflectionToStringBuilder

By admin On 21 January 2012 · Add Comment
If we need a simple utility to log bean values transforming them into a string rapresentation we can use ReflectionToStringBuilder To use this class we need common.lang jar library. Example: import org.apache.commons.lang.builder.ReflectionToStringBuilder; public class MyBean{  private String firstValue; private int secondValue; //... setter and getter public static main(String[] args){ MyBean myBean = new MyBean(); myBean.setFirstValue("Hello"); myBean.setSecondValue(1); System.out.println(ReflectionToStringBuilder.toString(this));}  } When [...]
[Continue Reading...]

Login interceptor for struts 2 web application

By admin On 31 December 2011 · Add Comment
If we want execute a business logic only when a user has been authenticated we need to introduce an interceptor as best practice. On struts.xml we define <interceptor name="login"class="com.AuthInterceptor" ></interceptor> <interceptor&minus;stack name="defaultLoginStack"><interceptor&minus;ref name="servletConfig" ></interceptor><interceptor&minus;ref name="params" ></interceptor><interceptor&minus;ref name="login" ></interceptor><interceptor&minus;ref name="pageConfig" ></interceptor><interceptor&minus;ref name="prepare" ></interceptor><interceptor&minus;ref name="chain" ></interceptor><interceptor&minus;ref name="modelDriven" ></interceptor><interceptor&minus;ref name="fileUpload" ></interceptor><interceptor&minus;ref name="staticParams" ></interceptor><interceptor&minus;ref name="params" ></interceptor><interceptor&minus;ref name="conversionError" ></interceptor><interceptor&minus;ref name="validation" ></interceptor><interceptor&minus;ref [...]
[Continue Reading...]

Default struts 2 action

By admin On 31 December 2011 · Add Comment
We can define through struts.xml an action that response when we get an unavailable resource For example if we get http://localhost:8080/helloworld  but helloworldAction doesn’t exist generally we obtain an 404 error Using following configuration  <default-action-ref name="home" /> <global-results> <result name="success">/jsp/main.jsp</result> <result name="login" type="redirect">/login.html</result> <result name="error" type="redirect">/404.html</result> </global-results>  <global-exception-mappings> <exception-mapping exception="java.lang.Throwable" result="error" /> </global-exception-mappings> In this way [...]
[Continue Reading...]

Struts 2 web application white project with annotation

By admin On 30 December 2011 · Add Comment
Well, we are configuring a new struts 2 web application with annotation support. Before all, we download last full GA release of Struts 2 framework and Apache Tomcat 7 After we can start from an Eclipse “Dynamic web project”. On /lib of our struts 2 zip archive we find and copy following jar files to WEB-INFlib : [...]
[Continue Reading...]

Bypass self-signed certificate on httpclient

By admin On 23 December 2011 · Add Comment
When we execute an https connection with a server http that utilize a self-signed certificate we can obtain following exeption: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at sun.security.ssl.Alerts.getSSLException(Alerts.java:192) at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1697) at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:258) at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:252) at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1165) at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:154) at sun.security.ssl.Handshaker.processLoop(Handshaker.java:610) at sun.security.ssl.Handshaker.process_record(Handshaker.java:546) at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:945) at [...]
[Continue Reading...]

Xml String to Document

By admin On 24 November 2011 · Add Comment
For Document we refer org.w3c.dom.Document. Well, in this example we want to transform a xml string into a Document object public static Document parseXmlFile(String xmlRequest) { // get the factory DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // if we want manage namespace dbf.setNamespaceAware(true); try {  // Using factory get an instance of document builder DocumentBuilder db = [...]
[Continue Reading...]

Converting Date to XMLGregorianCalendar

By admin On 24 November 2011 · Add Comment
A simple method to converting a java Date to a XMLGregorianCalendar (a representation of W3C XML Schema 1.0 date/time): public static XMLGregorianCalendar dateToXmlGregorianCalendar(Date date) { GregorianCalendar c = new GregorianCalendar(); c.setTime(date); try { return DatatypeFactory.newInstance().newXMLGregorianCalendar(c); } catch (DatatypeConfigurationException e) { return null; } }
[Continue Reading...]

Web application initialization at startup on tomcat or jboss AP

By admin On 24 November 2011 · Add Comment
1) listener based: we define a listener in the web.xml of our web application   my.package.mylistener  we create a new java class that implements ServletContextListener and we can use following method to hook the startup public void contextDestroyed(ServletContextEvent event){ myDestroyLogic();} 2) servlet based we define a new servlet in web.xml like following tag   myServletName [...]
[Continue Reading...]

Detect shutdown on tomcat or Jboss AS

By admin On 24 November 2011 · Add Comment
there are many way to detect when a tomcat or jboss application is going to shutdown. 1) servlet method: If you are using a web application you can use a destroy method on a servlet public void destroy(){// things to do at shutdown} 2) listener method (useful when our web application is packed into a [...]
[Continue Reading...]
  • ← Previous Entries
  • Categories

    • apache tomcat
    • Eclipse
    • java standard edition
    • java web application
    • JBoss
    • JMS
    • Other
    • struts2
  • Archives

    • November 2012
    • January 2012
    • December 2011
    • November 2011
  • Calendar

    January 2021
    M T W T F S S
    « Nov    
     123
    45678910
    11121314151617
    18192021222324
    25262728293031
  • Meta

    • Log in
    • Entries (RSS)
  • About
  • Articles
  • Cookie Policy and Privacy
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.OkRead more