UNIX Code :
1 #!/bin/bash
2
3 function myscript(){
4 set a = 10
5 echo ID from JSP is $id
6 echo 'i m here babe' >> /usr12/SPHI/SIR03174/sanjeev/a.txt
7 ls -lrt
8 return 122
9 }
10
11 myscript
Line 1: Most important Line the shell script. If this is missed Java module won't be able to invoke the Shell Script.
Lin 3: Name of the function, which need to be invoked by shell script at last (see line 11).
Line 4: Create a variable.
Line 5: The env variable id (expressed as $id) will be set in Java module and sent to this function while calling shell script.
Line 11: calling the Unix Function at the end of script, otherwise the code will not be invoked.
JSP Code : This will call the above Unix Shell script, pass a parameter to shell script and read the result from Unix script.
1 <%--
2 Document : proc.jsp
3 Created on : Nov 1, 2010, 2:48:14 PM
4 Author : sanjeev pandey
5 --%>
6
7 <%@page contentType="text/html" pageEncoding="UTF-8"%>
8
9 <%@page import="java.io.BufferedReader"%>
10 <%@page import="java.io.File"%>
11 <%@page import="java.io.InputStream"%>
12 <%@page import="java.io.InputStreamReader"%>
13
14
15 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
16 "http://www.w3.org/TR/html4/loose.dtd">
17
18 <html>
19 <head>
20 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
21 <title>JSP Page</title>
22 </head>
23 <body>
24 <h1>Hello World!</h1>
25 <%
26 String [] env = {"id=pheonix1"};
27 Runtime rt = Runtime.getRuntime();
28 out.println("Runtime : "+rt);
29
30 Process p = rt.exec("/usr/scpt.sh", env);
31 out.println("<br><br>Process : "+p);
32
33 InputStream is = p.getInputStream();
34 out.println("<br><br>InputStream : "+is);
35
36 BufferedReader br = new BufferedReader(new InputStreamReader(is));
37 out.println("<br><br>BufferedReader : "+br);
38
39 String line = "";
40 out.println("<br><br>line is space : "+line);
41
42 while(line != null) {
43 line = br.readLine();
44 out.println("<br><br>Reading inputstream : "+ line);
45 }
46 %>
47 </body>
48 </html>
Line 26: Creating String Variable to be passed to shell script. In shell script its treated as environment variable.
Line 30: Passing the Variable embedded in 'env' to shell script before executing it from java.
Line 33: Getting the inputstream, created after execution of the Unix script. This input stream contains all the things which are printed on unix when the shell script is executed.
Line 42-45: Printing the unix output on JSP.
Line 30: Passing the Variable embedded in 'env' to shell script before executing it from java.
Line 33: Getting the inputstream, created after execution of the Unix script. This input stream contains all the things which are printed on unix when the shell script is executed.
Line 42-45: Printing the unix output on JSP.
Hi Sanjeev,
ReplyDeletethanks for commenting on my blog post 2 solution of OutofMemoryError in Java, I see you have also got a good blog and you are adding some content. all the best and keep it up.
Thanks
Javin
10 examples of Enum in Java