Sunday, September 27, 2009

Ant Script for JBOSS Server Start and Stop

<property name="JBOSS_HOME" value="C:\jboss-4.2.2.GA" />

<target name="serverON">
<exec executable="cmd">
<arg value="/c" /> <
arg value="${JBOSS_HOME}/bin/run.bat" />
</exec>
</target>

<target name="serverOFF">
<exec executable="cmd">
<arg value="/c" />
<arg value="${JBOSS_HOME}/bin/shutdown.bat -S" />
</exec>
</target>

Wednesday, June 24, 2009

How to set wait in javascript, JavaScript Delay, JavaScript wait, JavaScript pause



You can use setTimeout() method or false loop to acheive wait functionlity in JavaScript. There is no such wait() or sleep() methods are here in JavaScript.

1. First Example with setTimeout()
Write one Time1.html and paste below code

<html>
CHECK FOR WAIT IN JAVA SCRIPT
<script type="text/javascript" language="javascript1.2">

function waitCheck(){
alert("Binod Kumar Suman");
setTimeout(function(){secondMethod()},2000);
}

function secondMethod(){
alert("Target Corporation");
}

</script>
<br>

<input type="button" value="Check" onClick="waitCheck()"/>
</html>

Just do double click on Time1.html you will get one alert immedialty but only after 2 seconde you will next alert message.

2. Second Example with false loop
Write one Time2.html and paste below code

<html>
CHECK FOR WAIT IN JAVA SCRIPT
<script type="text/javascript" language="javascript1.2">
function waitCheck(){
alert("Binod Kumar Suman");
pauseJS(1500);
secondMethod();
}

function secondMethod(){
alert("Target Corporation");
}

function pauseJS(timeInMilliS) {
var date = new Date();
var curDate = null;
do { curDate = new Date(); }
while(curDate-date < timeInMilliS);
}

</script>
<br>
<input type="button" value="Check" onClick="waitCheck()"/>
</html>

Just do double click on Time2.html you will get one alert immedialty but only after 2 seconde you will next alert message. :)

Monday, May 11, 2009

Download the image from Servlet usign core java code

For download the image from Servlet usign core java code
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

public class Download {

public static void main(String args[]) throws Exception {
String url = "http://localhost:9080/mYWebApp/sendImage/Fashion/shirt.jpg";
URL u = new URL(url);
URLConnection uc = u.openConnection();
String contentType = uc.getContentType();
int contentLength = uc.getContentLength();
if (contentType.startsWith("text/")|| contentLength == -1) {
throw new IOException("This is not a binary file.");
}
InputStream raw = uc.getInputStream();
InputStream in = new BufferedInputStream(raw);
byte[] data = new byte[contentLength];
int bytesRead = 0;
int offset = 0;
while (offset < contentLength) {
bytesRead = in.read(data, offset, data.length - offset);
if (bytesRead == -1)
break;
offset += bytesRead;
}
in.close();

if (offset != contentLength) {
throw new IOException("Only read " + offset + " bytes; Expected " + contentLength + " bytes");
}

System.out.println("DATA :: "+u.getFile());
String filename = u.getFile().substring(u.getFile().lastIndexOf('/') + 1);
System.out.println("FILE NAME :: "+filename);
FileOutputStream out = new FileOutputStream(filename);
out.write(data);
out.flush();
out.close();
}
}

Wednesday, February 11, 2009

How to show Image on JSP page from My Computer Folder

Webpage NEVER allow the to access any local files. Means if you write <img src="c:\ImageFolder\Binod.jpg"/> in the jsp file, it will NOT work. (It can work only in some editor, but it will not work using internet explorer)
first.jsp
<html>
<head>
<title>Binod Show Image</title>
</head>
<body>
<h1> BINOD FROM IMAGE SHOW PROJECT </h1>
<img src="c:\ImageFolder\Binod.jpg" width="100" height="100"/></body>
</html>
It will not work.
Then how to show Image on JSP page from local computer drive.
SOLUTION: You have to use servlet to show image on jsp file
1. Servlet SendImage.java
2. first.jsp
3. Change in web.xml
SendImage.java
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.StringTokenizer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SendImage extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
static final long serialVersionUID = 1L;
String image_name = "";
ResourceBundle props = null;
String filePath = "";
private static final int BUFSIZE = 100;
private ServletContext servletContext;
public SendImage() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("FROM SERVLET");
sendImage(getServletContext(), request, response);
}
public void sendImage(ServletContext servletContext,
HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.servletContext = servletContext;
String reqUrl = request.getRequestURL().toString();
StringTokenizer tokens = new StringTokenizer(reqUrl, "/");
int noOfTokens = tokens.countTokens();
String tokensString[] = new String[noOfTokens];
int count = 0;
while (tokens.hasMoreElements()) {
tokensString[count++] = (String) tokens.nextToken();
}
String folderName = tokensString[noOfTokens - 2];
image_name = tokensString[noOfTokens - 1];
filePath = "/" + folderName + "/" + image_name;
String fullFilePath = "c:/ImageFolder" + filePath;
System.out.println("FULL PATH :: "+fullFilePath);
// doShowImageOnPage(fullFilePath, request, response);
doDownload(fullFilePath, request, response);
}
private void doShowImageOnPage(String fullFilePath,
HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.reset();
response.setHeader("Content-Disposition", "inline");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Expires", "0");
response.setContentType("image/tiff");
byte[] image = getImage(fullFilePath);
OutputStream outputStream = response.getOutputStream();
outputStream.write(image);
outputStream.close();
}
private void doDownload(String filePath, HttpServletRequest request,
HttpServletResponse response) throws IOException {
File fileName = new File(filePath);
int length = 0;
ServletOutputStream outputStream = response.getOutputStream();
// ServletContext context = getServletConfig().getServletContext();
ServletContext context = servletContext;
String mimetype = context.getMimeType(filePath);
response.setContentType((mimetype != null) ? mimetype
: "application/octet-stream");
response.setContentLength((int) fileName.length());
response.setHeader("Content-Disposition", "attachment; filename=\""
+ image_name + "\"");
byte[] bbuf = new byte[BUFSIZE];
DataInputStream in = new DataInputStream(new FileInputStream(fileName));
while ((in != null) && ((length = in.read(bbuf)) != -1)) {
outputStream.write(bbuf, 0, length);
}
in.close();
outputStream.flush();
outputStream.close();
}
private byte[] getImage(String filename) {
byte[] result = null;
String fileLocation = filename;
File f = new File(fileLocation);
result = new byte[(int)f.length()];
try {
FileInputStream in = new FileInputStream(fileLocation);
in.read(result);
}
catch(Exception ex) {
System.out.println("GET IMAGE PROBLEM :: "+ex);
ex.printStackTrace();
}
return result;
}
}
2. first.jsp
<html><head><title>Binod Show Image</title></head><body><h1> BINOD FROM IMAGE SHOW PROJECT </h1>
<img src="http://localhost:8080/ImageShow/SendImage/12/Binod.jpg" width="100" height="100"/>
</body></html>
3. Update in web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd%22&gt;
<display-name> ImageShow</display-name>
<servlet>
<description> </description> <display-name> SendImage</display-name>
<servlet-name>SendImage</servlet-name>
<servlet-class> SendImage</servlet-class>
</servlet> <servlet-mapping> <servlet-name>SendImage</servlet-name>
<url-pattern>/SendImage/*</url-pattern> </servlet-mapping>
</web-app>

use this URL, now its work. :)
http://localhost:8080/ImageShow/first.jsp