我原先刚开始学AJAX的时候,忘了从什么地方拉下来的一篇文章,不过,如果没有AJAX基础,最好不看这篇示例,因为你肯定看不懂,因为这个示例页不能够给你立即看到立即实现的效果,你应该先去看一个最简单的示例,怎么样用AJAX去把一个文本文件的内容读回来。然后再回来看这篇文章,这里你可以看到使用AJAX分别把内容提交到STRUTS,SERVLET,WEBWORK,是怎么样操作的。我在看这篇文章以前,都是把一个JSP的处理请求,发给另外一个JSP页面处理,并在那个页面理采用BEAN的方式,虽然我是采用的STRUTS结构,但是却不知道可以直接将内容提交到ACTION,这样就可以减少一道工序了,人笨就是这样。
写这个页面的朋友是谁,我真的忘了,不过,还是非常感谢。示例就自己看吧: 86oo.com欢迎你

<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
ajax
</title>
</head>
<body bgcolor="#ffffff">
<h1>
<input name="username" type="text" maxlength="20" />
<input id="chk-name-btn" type="button" value="检测帐号" οnclick="testName('<%=request.getContextPath()%>')" />
<div id="view_name"></div>
</h1>
</body>
</html>
<script language="javascript">
  if (window.ActiveXObject && !window.XMLHttpRequest) {
      window.XMLHttpRequest=function() {
      return new ActiveXObject((navigator.userAgent.toLowerCase().indexOf('msie 5') != -1) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP');
   };
  }//取得XMLHttpRequest对象

欢迎各位访问86oo.com

 

function testName(path){ 您所浏览的文章来自86oo.com

//path是取得系统路径
                var view_name=document.getElementById("view_name");
      var req=new XMLHttpRequest();
      if (req) {
          req.onreadystatechange=function() {
           if (req.readyState==4 && req.status==200) {//判断状态,4是已发送,200已完成
                 if(req.responseText==0){
                 view_name.style.color='green';
                view_name.innerHTML='该用户名可以正常使用';
                 }else if(req.responseText==1){

86oo.com


                view_name.style.color='red';
                   view_name.innerHTML='该用户名已经被使用';
                       }else{
                     view_name.style.color='red';
                     view_name.innerHTML='该用户名含有非法字符!';
                     }
                 }
         } http://www.86oo.com
   req.open('POST', path+'/ajax.jsp');//jsp
                        req.open('POST', path+'/ajax.do');//struts
                        //req.open('POST', path+'/ajax.servlet');//servlet
                        //req.open('POST', path+'/ajax.action');//webwork
                        req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                        req.send("");//发送参数如果有参数req.send("username="+user_name);用request取得
http://www.86oo.com

      }
}
</script>
86oo.com提供各类教程

 

这个jsp页面并没有取得用户名,就是演示一下。还有<div>可以换span,具体作用问美工吧。 http://www.86oo.com

接受ajax请求的action。 www.86oo.com

import org.apache.struts.action.*;
import javax.servlet.http.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter; 86oo精彩教程

/**
 * <p>Title:AjaxAction </p>
 */

www.86oo.com

 

public class AjaxAction extends Action {
  public ActionForward execute(ActionMapping mapping,
                               ActionForm form,
                               HttpServletRequest request,
                               HttpServletResponse response)
      throws Exception {
    PrintWriter out = response.getWriter();
    out.print(1);//ajax取得都是字符的输出。如果数据量大的话,还可以用xml来发送和接受
    return null;
  }
} 86oo.com提供各类教程

struts-config.xml 86oo精彩教程

    <action type="test.whw.upload.AjaxAction" validate="false" scope="request"  path="/ajax"/>
---------------------------- www.86oo.com

如果是servlet www.86oo.com

web.xml 欢迎各位访问86oo.com

 <servlet>
  <servlet-name>AjaxServlet </servlet-name>
  <servlet-class>servlet.AjaxServlet </servlet-class>
 </servlet>
    <servlet-mapping>
        <servlet-name>AjaxServlet </servlet-name>
        <url-pattern>/AjaxServlet .servlet</url-pattern>
    </servlet-mapping> 86oo.com

AjaxServlet.java 86oo精彩教程

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

86oo.com

 

public class AjaxServlet extends HttpServlet {
 Logger log = Logger.getLogger(this.getClass());
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException,IOException {
  response.setContentType("text/xml; charset=GBK");
  PrintWriter out = response.getWriter();
    out.print(2);
   }
 }
 //Process the HTTP Post request
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  doGet(request, response);
 } 86oo.com

 //Process the HTTP Put request
 public void doPut(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
 } 欢迎各位访问86oo.com

 //Process the HTTP Delete request
 public void doDelete(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
 }


评论关闭
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!