Validate e-mail address with regular expression
/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
All regular expressions start and end with forward slashes to differentiate them from ordinary string expressions. Most regular expressions start matches at the first character ^ and end at the last $.
Now we try to match the mailbox name which can include periods and dashes \w+ states one or more alphanumeric must be at the start of the name. ([\.-]?\w+)* allows periods or dashes to be included in the mailbox name with the trailing \w+ ensuring that those characters can not finish the name. The @ is the mandatory separator.
The domain name can have several .xx or .xyz suffixes such as .com.uk. Once again \w+ ensures that domain starts with an alphanumeric and ([\.-]?\w+)* allows for the dashes and periods. Finally (\.\w{2,3})+ ensures that there is at least one suffix of between 2 and 3 characters preceded by a period.
Note: This is not a completely foolproof validation as it does not account for new domain names of 4 or more characters. Also not all two and three letter combinations are legitimate domains!
Displaying a wait page for a struts action
I was searching google to find a way to show a wait page when a struts form is submitted and the response is loaded. Sometimes it is required when the page has to process a lot of information or get huge amount of data from the database.
I found a really clean way to do so at http://wiki.apache.org/struts/StrutsPleaseWait .
Variable name for getters
It’s NOT the name of the member variable that counts, it’s the name of the get method.
You use the part after “get” as the property name.
Normally you would convert the first letter after “get” to lowercase, but if the second letter is uppercase also, then you don’t.
getXYAxis – the variable name should be “XYAxis”
getName – the variable name should be “name”
Legal Identifiers in Java
- Identifiers must start with a letter, a currency character($), or an underscore(_).
- Identifiers cannot start with a number.
- After the first character, identifiers can contain any combination of letters, currency characters, underscores, or numbers.
- Identifiers are case-sensitive
- “enum” cannot be used as identifier because it is added as a Java keyword in 5.0
Heap size and Eclipse
To increase heap size of eclipse editor, modify eclipse.ini file to the values you need
-vmargs
-Xms128M
-Xmx512M
-XX:PermSize=64M
-XX:MaxPermSize=128M
Read The Init-Params Defined In The web.xml
[eg. of web.xml
<web-app>
<servlet>
<servlet-name>ReadInitParams</servlet-name>
<servlet-class>com.company.MyServlet</servlet-class>
<display-name>Example Servlet</display-name>
<init-param>
<param-name>emailHost</param-name>
<param-value>192.168.1.1</param-value>
</init-param>
</servlet>
</webapp>
In Servlet Code:
public void init(ServletConfig config) throws ServletException {
String emailHost = config.getInitParameter("emailHost");
}
or
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String host = getServletConfig().getInitParameter("emailHost");
}
In JSP Code:
<html>
<head><title>Read Init params from a JSP</title></head>
<body>
<%
String emailHost = null;
public void jspInit() {
ServletConfig config = getServletConfig();
emailHost = config.getInitParameter("emailHost");
}
%>
<table border="1">
<tr><td>email server</td><td><%=emailHost%></td></tr>
</table>
<% // It can also be read directly from the implicit object - config %>
<%=config.getInitParameter("sys")%><br><br>
</body>
</html>
Difference between request.getParameter() and request.getAttribute()
The difference between getAttribute() and getParameter() is that getParameter() will return the value of a parameter that was submitted by an HTML form or that was included in a query string.
getAttribute() returns an object that you have set in the request, the only way you can use this is in conjunction with a RequestDispatcher. You use a RequestDispatcher to forward a request to another resource (JSP / Servlet). So before you forward the request you can set an attribute which will be available to the next resource.
Runtime.getRuntime().exec()
To copy a file, you can use the following command:
copy source.txt destination.txt
To run this in Java,
String[] cmd = {“copy”,”source.txt”,”destination.txt”}
Runtime.getRuntime().exec(cmd);
This may generate error=2. To fix this, use the complete path of copy.exe
eg. C:\Windows\System32\copy.exe
Hibernate gives “No CurrentSessionContext configured” error
If you use Hibernate in a managed environment (with JBoss or some other AS that Hibernate supoorts). Add this line to session-factory section:
<property name=”hibernate.current_session_context_class” >jta </property>
If you use Hibernate in a non-managed environment (standalone application with JDBC). Add this line:
<property name=”hibernate.current_session_context_class” >thread </property>
