Pages

Monday 28 November 2011

JSP/Servlet-Display Server Memory Details

Memory.jsp

<%@ page import="java.lang.Runtime" %>
<%@ page import="java.util.Date" %>
<%
Runtime rt = Runtime.getRuntime();
rt.gc();
Float Free;
Float Total;
Float Used;
long iFree;
long iTotal;
 
String strTimeStamp; /* The current date/time as a string: YYYY-MM-DD HH:MM */
String strTsYear; /* The current year as a string : YYYY */
String strTsHour; /* The current hour as a string : HH */
String strTsMon; /* The current monthh as a string : MM */
String strTsDay; /* The current day as a string : DD */
String strTsMin; /* The current minute as a string : MI */
String strTsSec; /* The current second as a string : SS */
Date ts;
ts = new java.util.Date();
strTsYear = (new Integer(ts.getYear() + 1900)).toString();
strTsMon = (new Integer(ts.getMonth() + 1)).toString();
strTsDay = (new Integer(ts.getDate())).toString();
strTsHour = (new Integer(ts.getHours())).toString();
strTsMin = (new Integer(ts.getMinutes())).toString();
strTsSec = (new Integer(ts.getSeconds())).toString();
if (strTsMin.length() == 1) {strTsMin = "0" + strTsMin; }
if (strTsMon.length() == 1) { strTsMon = "0" + strTsMon; }
if (strTsDay.length() == 1) {strTsDay = "0" + strTsDay; }
if (strTsHour.length() == 1) {strTsHour = "0" + strTsHour; }
if (strTsSec.length() == 1) {strTsSec = "0" + strTsSec; }
strTimeStamp = strTsYear + "-" + strTsMon + "-" + strTsDay + " " + strTsHour + ":"
+ strTsMin + ":" + strTsSec;
iFree = rt.freeMemory();
iTotal = rt.totalMemory();
Free = new Float(iFree/10000);
Free = new Float(Free.floatValue() / 100.);
Total = new Float(iTotal/10000);
Total = new Float(Total.floatValue() / 100.);
Used = new Float((iTotal - iFree)/10000);
Used = new Float(Used.floatValue() / 100.);
ts = new Date();
out.println("Date :: " + strTimeStamp);
out.println("Server Name ::" + request.getServerName());
out.println("Port ::" + request.getServerPort());
out.println("Used Memory :: " + Used.floatValue()+ " (MB)");
out.println("Free Memory :: " + Free.floatValue()+ " (MB)");
out.println("Total Memory :: " + Total.floatValue() + " (MB)");
%>


Date :: 2011-11-28 05:50:59
Server Name :: brigitz.com
Port :: 443
Used Memory :: 19.74 (MB)
Free Memory :: 46.97 (MB)
Total Memory :: 66.71 (MB)
OUTPUT:

Thursday 17 November 2011

LINUX-How can I start/stop tomcat server in Linux machine?

1) First download the Linux tomcat version.
2) Unzip it.
3) Go to the tomcat bin folder.
4) Run the startup.sh for start the tomcat server
5) Shutdown.sh for using to stop the tomcat

START TOMCAT :

$/opt/user/>export JAVA_HOME=/usr/local/j2sdk1.6
$/opt/user/>export CATALINA_OPTS="-Djava.awt.headless=true"
$/opt/user/>export JDK_HOME=${JAVA_HOME}
$/opt/user/>export CATALINA_HOME=/usr/local/tomcat6
$/opt/user/>${CATALINA_HOME}/bin/startup.sh

STOP TOMCAT :

$/opt/user/>export JAVA_HOME=/usr/local/j2sdk1.6
$/opt/user/>export CATALINA_OPTS="-Djava.awt.headless=true"
$/opt/user/>export JDK_HOME=${JAVA_HOME}
$/opt/user/>export CATALINA_HOME=/usr/local/tomcat6
$/opt/user/>${CATALINA_HOME}/bin/shutdown.sh

Wednesday 16 November 2011

JAVA-Create Java project with eclipse

1) Click the eclipse.exe, after opening the eclipse; Right click the mouse button in the left side window.
2) Select New->Project.
3) Click Project
4) Enter the project name “TestJava” ->Click Next->Click Finish.

5) Select the Project name; Right click the mouse button->select New->click Class.



6) Enter the class name “HelloExample” ->click Finish.


7) Developing your java code.


8) Right click the mouse button->select Run-as->select Java Application.


9) The output is showing in the console window.

Friday 11 November 2011

JAVA-Dynamic Password Generation

public class DynamicPassword {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DynamicPassword pass=new DynamicPassword();
System.out.println("New Pass="+pass.randomGenPassword());
}
private String randomGenPassword(){
StringBuffer s=new StringBuffer();
for(int i=0;i<= 8; i++)
{
int nextChar=(int)(Math.random()*62);
if(nextChar<10) //0-9
s.append(nextChar);
else if (nextChar<36) //a-z
s.append((char)(nextChar-10+'a'));
else
s.append((char)(nextChar-36+'A'));
}

return(s.toString().toLowerCase());

}

}

Monday 7 November 2011

JAVA-java validation

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ValidateUtility implements Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;
public static final String ERROR_MSG = "Invalid Input";
/**
* This method is to check for special character from the user entered
* String
*
* @param String
* p_SearchText
* @return boolean
*
*/

public static boolean isInvalidSpecialChar(String p_SearchText) {
String l_MatchString = "[\\\"\\<\\>\\`\\~\\!\\@\\$\\^\\&\\=\\|\\{\\}\\[\\]\\:\\'\\,\\?\\\\]";

Pattern l_Pattern = Pattern.compile(l_MatchString);
Matcher l_PatternMatch = l_Pattern.matcher(p_SearchText);
boolean l_Valid = false;
while (l_PatternMatch.find()) {
l_Valid = true; // set true if wrong pattern match
break;
}
return l_Valid;
}
public static boolean isInvalidSpecialChar1(String p_SearchText) {

String l_MatchString = "[\\\"\\<\\>\\`\\~\\!\\@\\$\\^\\=\\|\\{\\}\\[\\]\\:\\?\\\\]";

Pattern l_Pattern = Pattern.compile(l_MatchString);
Matcher l_PatternMatch = l_Pattern.matcher(p_SearchText);
boolean l_Valid = false;
while (l_PatternMatch.find()) {
l_Valid = true; // set true if wrong pattern match
break;
}
return l_Valid;
}
/**
* This method is to check for valid number from the user entered String
*
* @param String
* p_SearchText
* @return boolean
*
*/

public static boolean isInvalidNumber(String p_SearchText) {
boolean l_Valid = true;
try {
Integer.parseInt(p_SearchText);
l_Valid = false; // set false if not numeric value
} catch (Exception p_exc) {
System.out.println("Exception in isInvalidNumber() : "
+ p_exc.getMessage());
}
return l_Valid;
}

/**
* This method is to check for valid Decimalnumber from the user entered
* String
*
* @param String
* p_SearchText
* @return boolean
*
*/

public static boolean isInvalidDecimalNumber(String p_SearchText) {
boolean l_Valid = true;
try {
Double.parseDouble(p_SearchText);
l_Valid = false; // set false if not decimal value
} catch (Exception p_exc) {
System.out.println("Exception in isInvalidDecimalNumber() : "
+ p_exc.getMessage());
}
return l_Valid;
}

/**
* This method is to check for valid Lnumber from the user entered String
*
* @param String
* p_SearchText
* @return boolean
*
*/

public static boolean isInvalidLNumber(String p_SearchText) {
boolean l_Valid = false;
if (p_SearchText.length() < 7 || p_SearchText.length() > 7) {
l_Valid = true; // set true if < 7 } else if (!(p_SearchText.substring(0, 1).equalsIgnoreCase("l")) || !(p_SearchText.substring(0, 1).equalsIgnoreCase("L"))) { l_Valid = true; // set true if not "l" or "L" } else { try { Integer.parseInt(p_SearchText.substring(1, 7)); } catch (Exception p_exe) { l_Valid = true; } } return l_Valid; } /** * This method is to check for all special character from the user entered * String * * @param String * p_SearchText * @return boolean * */ public static boolean isInvalidString(String p_SearchText) { String l_MatchString = "[\\\"\\<\\>\\`\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)\\-\\_\\+\\=\\|\\{\\}\\[\\]\\:\\;\\'\\.\\,\\?\\/\\\\]";
Pattern l_Pattern = Pattern.compile(l_MatchString);
Matcher l_PatternMatch = l_Pattern.matcher(p_SearchText);
boolean l_Valid = false;
while (l_PatternMatch.find()) {
l_Valid = true; // set true if wrong pattern match
break;
}
return l_Valid;
}

/**
* This method is to check for flag input
*
* @param String p_SearchText
* @return boolean
*
*/
public static boolean isInvalidFlag(String p_SearchText) {
boolean l_Valid = true;
if(p_SearchText.equalsIgnoreCase("Y") || p_SearchText.equalsIgnoreCase("N")){
l_Valid = false;
}
return l_Valid;
}

/**
* This method trims the value
*
* @param p_val
* @return
*/
public static String trimValue(String p_val)
{
return p_val !=null ? p_val.trim() : p_val;
}


/**
* This method is to check for date format (mm/dd/yyyy)
*
* @param String p_SearchText
* @return boolean
*
*/
public static boolean isInvalidDate(String p_SearchText){
SimpleDateFormat l_format = new SimpleDateFormat("MM/dd/yyyy");
boolean l_Valid = true;
Date l_parsedate = null;
try{
l_parsedate = l_format.parse(p_SearchText);
}catch (ParseException e){
return true;
}
if(l_format.format(l_parsedate).equals(p_SearchText)){
l_Valid = false;
}
return l_Valid;
}

}

Friday 4 November 2011

JAVA-Sending mail through java program

package gmail;

import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SendExternalMail {

public SendExternalMail() {

try{

String toAddr = "toaddress@gmail.com";
String fromAddr = "fromaddress@gmail.com";
//set system properties
Properties prop = System.getProperties();
prop.setProperty("mail.smtp.host", "localhost");
prop.setProperty("mail.user", "yourgmailusername@gmail.com"); //Enter your Gmail User name(eg:wilbertengg@gmail.com)
prop.setProperty("mail.password", "***********"); //Enter your Gmail password(eg:test123)

Session sessi = Session.getDefaultInstance(prop);
MimeMessage mimMsg = new MimeMessage(sessi);
mimMsg.setFrom(new InternetAddress(fromAddr));
mimMsg.addRecipient(Message.RecipientType.TO,new InternetAddress(toAddr));
mimMsg.setSubject("Test Subject Message");
BodyPart msgBody = new MimeBodyPart();
msgBody.setText("Test message content");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(msgBody);
mimMsg.setContent(multipart );
Transport.send(mimMsg);
System.out.println("Mail Sent message successfully....");
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}


}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SendExternalMail extMail = new SendExternalMail();
}

}