jsp页面部分代码:
<form name="myForm" method="get">
<input type="text" name="input1" />
<input type="button" value="Click" οnclick="ajaxFunctio();" />
<input type="text" name="output1" />
</form>
ajaxFunction所在的js代码,其中就有ajax的内容:
function ajaxFunction()
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("您的浏览器不支持AJAX!");
return false;
}
}
}
url="output.action?salt"+new Date();
xmlHttp.open("get",url,false);
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{ if(xmlHttp.status==200){
document.myForm.output1.value=xmlHttp.responseText.toString();
}
}
}
xmlHttp.send();
}
action中的get方法:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("here is the get function");
PrintWriter out = response.getWriter();
response.setContentType("text/html");
String input1=request.getParameter("input1");
System.out.println(input1);
if("mage".equalsIgnoreCase(input1)){
System.out.println("you have entered mage"); //不要怪我,我也是v大迷。
out.print("vurtne");
out.flush();
out.close();}else{
System.out.println("you have not entered mage");
out.print("drakedog"); //也不要怪我,我觉得dd打的也不错。
out.flush();
out.close();
}
}
web.xml配置内容:
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>OutputAction</servlet-name>
<servlet-class>com.test.action.OutputAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>OutputAction</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<form name="myForm" method="get">
<input type="text" name="input1" />
<input type="button" value="Click" οnclick="ajaxFunctio();" />
<input type="text" name="output1" />
</form>
ajaxFunction所在的js代码,其中就有ajax的内容:
function ajaxFunction()
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("您的浏览器不支持AJAX!");
return false;
}
}
}
url="output.action?salt"+new Date();
xmlHttp.open("get",url,false);
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{ if(xmlHttp.status==200){
document.myForm.output1.value=xmlHttp.responseText.toString();
}
}
}
xmlHttp.send();
}
action中的get方法:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("here is the get function");
PrintWriter out = response.getWriter();
response.setContentType("text/html");
String input1=request.getParameter("input1");
System.out.println(input1);
if("mage".equalsIgnoreCase(input1)){
System.out.println("you have entered mage"); //不要怪我,我也是v大迷。
out.print("vurtne");
out.flush();
out.close();}else{
System.out.println("you have not entered mage");
out.print("drakedog"); //也不要怪我,我觉得dd打的也不错。
out.flush();
out.close();
}
}
web.xml配置内容:
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>OutputAction</servlet-name>
<servlet-class>com.test.action.OutputAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>OutputAction</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>