我对JS和JSP相当陌生,但我在与Java类通信时遇到了困难。我不确定我是否想让java类成为bean或applet,或者两者都不想。
我有来自用户的两个文本输入。我希望用户输入两个字符串。在用户输入字符串之后,我希望他们单击一个按钮并执行以下操作:
下面是我对代码的尝试:
JSP:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Button Pusher</title>
</head>
<body>
<button onclick="myFunction()">Compare</button>
<BR>
<dd>Path 1: <INPUT TYPE=TEXT NAME="Path1Name" SIZE=20 ID="Path1ID"><BR>
Path 2: <INPUT TYPE=TEXT NAME="Path2Name" SIZE=20 ID="Path2ID"><BR></dd>
<br>
<jsp:useBean id="mybean" class="my.MyBean" scope="session" >
<jsp:setProperty name="mybean" property="path1" value="pathname" />
</jsp:useBean>
<p id="demo"></p>
<script>
function myFunction()
{
var y=5;
var x=y+2;
var demoP=document.getElementById("demo")
var argument1 = document.getElementById("Path1ID")
demoP.innerHTML= "Display html from MyBean.compare(path1,path2) method here";
}
</script>
</body>
</html> Java代码:
package my;
public class MyBean {
private String name=new String();
private String path1 = new String();
private String path2 = new String();
public String getPath1() {
return path1;
}
public String getPath2() {
return path2;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setPath1(String path1) {
this.path1=path1;
}
public void setPath2(String path2) {
this.path2=path2;
}
public StringBuffer compare(String path1, String path2) {
StringBuffer strBuff = new StringBuffer();
strBuff.append("<b>" + path1+"</b>"+"<i>"+path2+"</i>");
return strBuff;
}
} 所有的帮助都是非常感谢的!
发布于 2013-07-19 17:21:56
您必须提交表单,然后将参数输入您的页面。我认为您需要阅读一些关于JSP的非常基本的教程。以这种方式使用javascript是不正确的。Javascript与JSP中的Java代码之间没有交互。
https://stackoverflow.com/questions/17751962
复制相似问题