jsjava代码字符串 js运行字符串代码

java字符串如何解析成能运行的java代码?

java字符串如何解析成运行的java代码

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名注册、网络空间、营销软件、网站建设、杜尔伯特网站维护、网站推广。

有些情况下,不得不动态运行Java代码,以便提供更加灵活的方式,以下代码可参考(在JDK 1.5+平台上运行通过):

public static void main(String[] args) {

int i = 10;

String code = "System.out.println(\"Hello World!\"+(13+2*5/3));";

code += "for(int i=0;i" + i + ";i++){";

code += " System.out.println(Math.pow(i,2));";

code += "}";

try {

run(code);

} catch (Exception e) {

e.printStackTrace();

}

}

private synchronized static File compile(String code) throws Exception {

File file = File.createTempFile("JavaRuntime", ".java", new File(System.getProperty("user.dir")));

file.deleteOnExit();

// 获得类名

String classname = getBaseFileName(file);

// 将代码输出到文件

PrintWriter out = new PrintWriter(new FileOutputStream(file));

out.println(getClassCode(code, classname));

out.close();

// 编译生成的java文件

String[] cpargs = new String[] { "-d",

System.getProperty("user.dir") + "\\WebRoot\\WEB-INF\\classes",

file.getName() };

int status = Main.compile(cpargs);

if (status != 0) {

throw new Exception("语法错误!");

}

return file;

}

private static synchronized void run(String code) throws Exception {

String classname = getBaseFileName(compile(code));

new File(System.getProperty("user.dir")

+ "\\WebRoot\\WEB-INF\\classes\\" + classname + ".class")

.deleteOnExit();

try {

Class cls = Class.forName(classname);

Method main = cls.getMethod("method", null);

main.invoke(cls, null);

} catch (Exception se) {

se.printStackTrace();

}

}

private static String getClassCode(String code, String className) {

StringBuffer text = new StringBuffer();

text.append("public class " + className + "{\n");

text.append(" public static void method(){\n");

text.append(" " + code + "\n");

text.append(" }\n");

text.append("}");

return text.toString();

}

private static String getBaseFileName(File file) {

String fileName = file.getName();

int index = fileName.indexOf(".");

String result = "";

if (index != -1) {

result = fileName.substring(0, index);

} else {

result = fileName;

}

return result;

}

在JS中实现与此JAVA代码同样的功能:str中字符为字母则添加至另一字符串,不是则用UNICODE的十六进制替换

function jsEncode(str) {

var retValue = new Array();

var strLength = str.length;

for(var index = 0;index strLength ;index++){

var tmpChar = str.charAt(index);

//A-Z or a-z

if((('A' = tmpChar tmpChar = 'Z')|| ('a' = tmpChar tmpChar = 'z'))){

retValue.push(tmpChar);

}else{

//escape() 函数是用来转换为unicode字符的。

retValue.push(escape(tmpChar));

}

}

//alert(retValue.join(""));

return retValue.join("")

}

javascript怎么得到后台java字符串参数

java代码:String javaStr= "testChar";通过sevlet 的 request.setAttribute("javaStr",javaStr);或者session的方式

jsp页面:取到上面的变量javaStr,用String javaStr=request.getAttribute("javaStr");或者通过session都能取到

js代码:var javascriptStr = '%="javaStr"%';

试试看

js数据类型与java数据类型之间如何转化数字如何转化为字符串

朋友首先,您应该是知道的,js是客户端脚本,JAVA是服务器端代码首先二者不是同一种语言,因此不存在狭义上的数据类型转换概念,只能说是,服务器与客户端之间数据传输获得后做处理,因此这就是个双方向的问题,(1)当浏览器传给服务器的参数,多数是字符串,服务器端接受后如(Integer)request.getparameter("参数名").parseInt();(2)客户端浏览器获得服务器端数据后你可以对把计算任务放到servlet中或JAVABEAN中,客户端仅用于显示结果即可

不必要做类型转换,可以根据显示的字符串判断后,自定义JS

数字类型变量,参与数学运算,完成相关功能,

所以说

JS与JAVA之间没有类型转换,只有间接的数据处理


当前题目:jsjava代码字符串 js运行字符串代码
网站地址:http://scyanting.com/article/dopeeis.html