
ajax获取jsp并解析需要怎么做?
2个回答
2016-01-11 · 做真实的自己 用良心做教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注

展开全部
ajax获取jsp并解析需要先调用后台接口,交互层有db,获取到数据后就可以直接显示了。
举例如下:
<html>
<head>
<title>Ajax测试页面</title>
</head>
<script type="text/javascript">
var xmlHttpRequest;
function createXmlHttpRequest(){
if(window.ActiveXObject){ //如果是IE浏览器
//alert("是");
return new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){ //非IE浏览器
//alert("非");
return new XMLHttpRequest();
}
}
function check(){
var name=document.getElementById('uName');
var pass=document.getElementById('pass');
if(!name.value){
alert("请输入用户名!");
name.focus();
return;
}else if(!pass.value){
alert("请输入密码!");
pass.focus();
return;
}
var url="ajax.do?uName=hyw"; //这里写上跳转到structs的路径和方法,并把参数传进去
url=encodeURI(url);
url=encodeURI(url);
xmlHttpRequest=createXmlHttpRequest();
xmlHttpRequest.onreadystatechange=callBack;
xmlHttpRequest.open("POST",url,true); //true是异步请求
xmlHttpRequest.send(null);
}
function callBack(){
var r_value=xmlHttpRequest.responseText;
var r_status=xmlHttpRequest.readyState;
alert(r_value+":::"+r_status);
}
</script>
<body>
<form action="ajax.do" method="get">
用户名: <input type="text" size="15" name="uName"/> <br/>
密 码: <input type="text" size="15" name="pass" /> </br>
<input type="button" value="登录" onclick="check()"/>
</form>
</body>
</html>
structs处理的代码:
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String uName=request.getParameter("uName");
uName=URLDecoder.decode(uName, "utf-8"); //把Ajax的传值,转换成utf-8
System.out.println(uName);
out.print("这就是Ajax的返回值");
举例如下:
<html>
<head>
<title>Ajax测试页面</title>
</head>
<script type="text/javascript">
var xmlHttpRequest;
function createXmlHttpRequest(){
if(window.ActiveXObject){ //如果是IE浏览器
//alert("是");
return new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){ //非IE浏览器
//alert("非");
return new XMLHttpRequest();
}
}
function check(){
var name=document.getElementById('uName');
var pass=document.getElementById('pass');
if(!name.value){
alert("请输入用户名!");
name.focus();
return;
}else if(!pass.value){
alert("请输入密码!");
pass.focus();
return;
}
var url="ajax.do?uName=hyw"; //这里写上跳转到structs的路径和方法,并把参数传进去
url=encodeURI(url);
url=encodeURI(url);
xmlHttpRequest=createXmlHttpRequest();
xmlHttpRequest.onreadystatechange=callBack;
xmlHttpRequest.open("POST",url,true); //true是异步请求
xmlHttpRequest.send(null);
}
function callBack(){
var r_value=xmlHttpRequest.responseText;
var r_status=xmlHttpRequest.readyState;
alert(r_value+":::"+r_status);
}
</script>
<body>
<form action="ajax.do" method="get">
用户名: <input type="text" size="15" name="uName"/> <br/>
密 码: <input type="text" size="15" name="pass" /> </br>
<input type="button" value="登录" onclick="check()"/>
</form>
</body>
</html>
structs处理的代码:
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String uName=request.getParameter("uName");
uName=URLDecoder.decode(uName, "utf-8"); //把Ajax的传值,转换成utf-8
System.out.println(uName);
out.print("这就是Ajax的返回值");
展开全部
JSP技术使用Java编程语言编写类XML的tags和scriptlets,来封装产生动态网页的处理逻辑。网页还能通过tags和scriptlets访问存在于服务端的资源的应用逻辑。JSP将网页逻辑与网页设计的显示分离,支持可重用的基于组件的设计,使基于Web的应用程序的开发变得迅速和容易。JSP(JavaServerPages)是一种动态页面技术,它的主要目的是将表示逻辑从Servlet中分离出来。
jsp需要放到tomcat,jetty等容器中才能被通过http访问到。
jsp需要放到tomcat,jetty等容器中才能被通过http访问到。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询