In this example we are going to see how a UNIX script can be called from a JSP page and the result of the UNIX script is displayed back at the JSP page.
Here is the example illustrating the same:
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>My Page</title>
<script type="text/javascript">
function getXMLObject()
{
var xmlHttp = false;
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e2) {
xmlHttp = false;
}
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
xmlHttp = new XMLHttpRequest();
}
return xmlHttp;
}
var xmlhttp = new getXMLObject();
function getStudentDetails() {
if(xmlhttp) {
var studentId = document.getElementById("studentId").value;
xmlhttp.open("POST","StudentDetails?"+"studentId="+studentId,true);
xmlhttp.onreadystatechange = responseHandler;
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send(null);
}
}
function responseHandler() {
if (xmlhttp.readyState == 4) {
if(xmlhttp.status == 200) {
document.frm.txtarea.value=xmlhttp.responseText;
}else {
alert("Error occured while AJAX call..... Please try again.....");
}
}
}
</script>
</head>
<body>
<form name="frm">
<table width="100%">
<tr style="width: 100%;">
<td style="padding-left: 40px; padding-top: 10px; padding-bottom: 10px">Student Id ::
<input type="text" id="studentId" style="width: 150px">
<a style="padding-right: 40px;">
<input style="background-color:#092A6B;color:#FFFFFF;width: 150px ;font-size: 15; cursor: pointer;" type="button" value="Retrieve Details" onClick="getStudentDetails();">
</a>
</td>
</tr>
<tr style="width: 100%;">
<td style="padding-left: 30px; padding-bottom: 10px">
<textarea name="txtarea" style="width: 670px; height: 100px;"></textarea>
</td>
</tr>
</table>
</form>
</body>
</html>
StudentDetails.java
package com.shakeel;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class StudentDetails
*/
public class StudentDetails extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public StudentDetails() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/xml");
String studentId = request.getParameter("studentId");
System.out.println(studentId);
String command = "sh getDetails.sh "+studentId; // UNIX command to be executed with parameter paased
out.println(executeCommand(command));
}
BufferedWriter bw = null;
private String executeCommand(String command) throws IOException {
StringBuffer output = new StringBuffer();
Process p;
try {
p = Runtime.getRuntime().exec(command); // Process to execute UNIX command
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
File fout = new File("out.txt");
FileOutputStream fos = new FileOutputStream(fout);
bw = new BufferedWriter(new OutputStreamWriter(fos));
String line = "";
while ((line = reader.readLine())!= null) {
output.append(line + "\n");
bw.write(line);
bw.newLine();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
bw.close();
}
return output.toString();
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>UnixCall</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>StudentDetails</display-name>
<servlet-name>StudentDetails</servlet-name>
<servlet-class>com.shakeel.StudentDetails</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>StudentDetails</servlet-name>
<url-pattern>/StudentDetails</url-pattern>
</servlet-mapping>
</web-app>
I hope this example would have given you an idea on how to call a UNIX script from Java.
Here is the example illustrating the same:
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>My Page</title>
<script type="text/javascript">
function getXMLObject()
{
var xmlHttp = false;
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e2) {
xmlHttp = false;
}
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
xmlHttp = new XMLHttpRequest();
}
return xmlHttp;
}
var xmlhttp = new getXMLObject();
function getStudentDetails() {
if(xmlhttp) {
var studentId = document.getElementById("studentId").value;
xmlhttp.open("POST","StudentDetails?"+"studentId="+studentId,true);
xmlhttp.onreadystatechange = responseHandler;
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send(null);
}
}
function responseHandler() {
if (xmlhttp.readyState == 4) {
if(xmlhttp.status == 200) {
document.frm.txtarea.value=xmlhttp.responseText;
}else {
alert("Error occured while AJAX call..... Please try again.....");
}
}
}
</script>
</head>
<body>
<form name="frm">
<table width="100%">
<tr style="width: 100%;">
<td style="padding-left: 40px; padding-top: 10px; padding-bottom: 10px">Student Id ::
<input type="text" id="studentId" style="width: 150px">
<a style="padding-right: 40px;">
<input style="background-color:#092A6B;color:#FFFFFF;width: 150px ;font-size: 15; cursor: pointer;" type="button" value="Retrieve Details" onClick="getStudentDetails();">
</a>
</td>
</tr>
<tr style="width: 100%;">
<td style="padding-left: 30px; padding-bottom: 10px">
<textarea name="txtarea" style="width: 670px; height: 100px;"></textarea>
</td>
</tr>
</table>
</form>
</body>
</html>
StudentDetails.java
package com.shakeel;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class StudentDetails
*/
public class StudentDetails extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public StudentDetails() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/xml");
String studentId = request.getParameter("studentId");
System.out.println(studentId);
String command = "sh getDetails.sh "+studentId; // UNIX command to be executed with parameter paased
out.println(executeCommand(command));
}
BufferedWriter bw = null;
private String executeCommand(String command) throws IOException {
StringBuffer output = new StringBuffer();
Process p;
try {
p = Runtime.getRuntime().exec(command); // Process to execute UNIX command
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
File fout = new File("out.txt");
FileOutputStream fos = new FileOutputStream(fout);
bw = new BufferedWriter(new OutputStreamWriter(fos));
String line = "";
while ((line = reader.readLine())!= null) {
output.append(line + "\n");
bw.write(line);
bw.newLine();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
bw.close();
}
return output.toString();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>UnixCall</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>StudentDetails</display-name>
<servlet-name>StudentDetails</servlet-name>
<servlet-class>com.shakeel.StudentDetails</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>StudentDetails</servlet-name>
<url-pattern>/StudentDetails</url-pattern>
</servlet-mapping>
</web-app>
I hope this example would have given you an idea on how to call a UNIX script from Java.
thanks for sharing keep updating
ReplyDeleteBest Selenium Training in Chennai | Android Training in Chennai | Java Training in chennai | Webdesigning Training in Chennai
Nice blog to read.
ReplyDeleteBest Automation Testing Training in chennai