问题
I have previously written a java class that was executed from a bash file, now I need to allow its execution from a JSP page using a Javascript/HTML button, I wonder how do I do that?
First, my class looks like this:
public class Sync
{
public static void main(String[] args)
{
//my content here
}
}
This Sync class has been run from a bash script as follows:
cd /root/tomcat/webapps/project/WEB-INF/classes/
echo "Executing first part..."
/usr/local/java/bin/java classes/CLRSyncCLI 120.0.0.1 up false Y ${IPS[@]}
echo "Executing second part..."
/usr/local/java/bin/java classes/CLRSyncCLI 127.0.0.1 down false Y ${IPS[@]}
note that:classes is the directory where all compiled java classes are within the TomCat web server.
now within jsp page I need something like:
<input type="button" value="Execute" name="to" action="run" onClick="path here">
How do I run this sync classes with its arguments twice from a single button click, and display that the class has been executed with a message.
回答1:
First, I'd like to say what you are proposing is a bad idea. You should really refactor Sync as described in the previous answers.
However, if you are dead-set on using Sync as-is, here is some code that might help:
<% if( request.getParameter( "to" ) == null ) { %>
<p>
Click 'Execute' to begin processing.
</p>
<form>
<input type="submit" value="Execute" name="to" />
</form>
<% } else { %>
<p>
Processing...
</p>
<pre>
<%
PrintStream sysout = System.out;
try {
File syncFile = File.createTempFile( "Sync", ".tmp" );
PrintStream syncOs= new PrintStream( syncFile );
System.setOut( syncOs );
String[] myArgs = ...; // set up your args
Sync.main( myArgs );
// ... do whatever else you need to do
syncOs.close();
syncFile.close();
FileReader syncRdr = new FileReader( syncFile );
String line = null;
while( ( line = syncRdr.readLine() ) {
out.println( line );
}
syncRdr.close();
syncFile.delete();
} catch( Exception ex ) {
out.print( ex );
} finally {
System.setOut( sysout );
}
%>
</pre>
<% } %>
There are some potential pitfalls here, especially with multithreading. But maybe you can use this as a starting point? Also, I have not debugged it, so you might need to do more work.
Good luck,
DC
回答2:
Your class has to extend javax.servlet.http.HttpServlet and override at least one method, if you are doing a form, then it would be a method doPost()
Then you need to add a <servlet/> and a <servlet-mapping/> to WEB-INF/web.xml to point to your class (which is now a Servlet).
Judging by the path, you already have Tomcat installed. javax.servlet.* can be found in your tomcat/lib under the name servlet-api.jar. You only need them for compiling. Tomcat provides them during runtime.
Only then can you create a form in your JSP like this:
<form method="POST" action="MyServlet">...<input.../>...</form>
assuming MyServlet is the name of the Servlet you added to web.xml (replace with your own if needed).
Here is an example: http://met.guc.edu.eg/OnlineTutorials/JSP%20-%20Servlets/A%20servlet%20example.aspx
It always makes sense to read at least Chapter 4 of JavaEE guide: http://docs.oracle.com/javaee/5/tutorial/doc/bnafd.html
回答3:
Short answer: onClick="path here" should link to a second JSP that runs the class, not the class itself.
Long answer:
Copy your class in tomcat lib folder
Create a second JSP file
onClick="path here"on first JSP should link to that new JSP that will run the class, not the class itself.Add classpath to the JSP header
import="com.mypackage.*"You will have to create a method that receives the parameters and runs the logic, you will not be able to run the main method in the way you do in command line.
Add the calling code to the JSP
<%
CLRSyncCLI o = new CLRSyncCLI();
o.method();
%>
来源:https://stackoverflow.com/questions/12675988/how-do-i-run-a-java-class-with-arguments-from-a-jsp-page