[英]Java webpage execute script
I am new to Java, please help. 我是Java新手,请帮忙。
I wrote html page index.jsp that suppose to get value from user and execute a script with that value. 我写了html页面index.jsp,该页面想从用户那里获取价值并执行具有该价值的脚本。
The code is- 该代码是-
<body> <form name="Execute-Java" action=""> <p> <label for="s">IP- </label> <input type="text" name="server" value="xxxx"> </p><br> <input type="submit" value="Execute java" name="Submit"/> </form> </body>
Also wrote a java code, compiled it using javac and ran it using java command-line successfully. 还编写了一个Java代码,使用javac进行了编译,并使用Java命令行成功运行了该代码。
public class SendEmail {
public static void main(String [] args){
String from = "from@from.com";
String to = "to@to.com";
String subject = "Test Subject";
String body_email = "Body test";
String host = "x.x.x.x";
//Get the session object
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
Session session = Session.getDefaultInstance(properties);
//compose the message
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(subject);
message.setText(body_email);
// Send message
Transport.send(message);
System.out.println("message sent successfully....");
}catch (MessagingException mex) {mex.printStackTrace();}
} } }}
Now i would like the index.jsp page to somehow execute the java code when user click submit using the host value given by the user. 现在,我希望index.jsp页面在用户使用用户提供的主机值单击Submit时以某种方式执行Java代码。
Can it be done and what should i do? 能做到吗,我该怎么办?
If you want to do some calculations with user input from html (jsp) on server-side you should use Application-Layer protocol from OSI (HTTP, HTTPS fe) . 如果要使用服务器端html(jsp)的用户输入进行一些计算,则应使用OSI的Application-Layer协议(HTTP,HTTPS fe) 。
Approach - using Ajax send http request to Server (java-servlet resource, deployed on Tomcat or another servlet container). 方法-使用Ajax将http请求发送到服务器(java-servlet资源,部署在Tomcat或另一个servlet容器上)。 Or you may use default behavior of form.submit() .
或者,您可以使用form.submit()的默认行为。
Make SendEmail class an Servlet extending it from HTTPServlet, implement doPost method(or doGet), set address for servlet. 使SendEmail类成为从HTTPServlet扩展它的Servlet,实现doPost方法(或doGet),设置Servlet的地址。
Set action and method attributes for form. 设置表单的操作和方法属性。
Submit form. 提交表格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.