ajaxのjspでの応用


まずjspページとスクリプトですが、1つに書きやすいように
これは、ユーザー名が存在するかどうかを検出する一般的な機能です.
ここではstrutsを使っています
<%@ 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="    " onclick="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  

function testName(path){

//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){
                 view_name.style.color='red';
                    view_name.innerHTML='         ';
                        }else{
                      view_name.style.color='red';
                      view_name.innerHTML='          !';
                      }
                  }
          }
                         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  
       }
}
</script>
 
このjspページはユーザー名を取得していませんが、プレゼンテーションです.また
はspanを変えることができて、具体的な作用は美工に聞きましょう.
ajaxリクエストのactionを受け入れます.
 
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;

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

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;
   }
}

struts-config.xml

     <action type="test.whw.upload.AjaxAction" validate="false" scope="request"   path="/ajax"/>
 
----------------------------
servletなら
 
web.xml

<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>
 
AjaxServlet.java
 
 
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;


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);
}

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

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

//Clean up resources
public void destroy() {
}
}
 
-------------------------------------
Webworkの場合
 
xwork.xml

    <action name="ajax" class="com.whw.upload.action.webwork.AjaxAction" method="ajax"/>
 
AjaxAction.java
 
import java.io.PrintWriter;

public class AjexAlbumAction extends ActionSupport implements Action{

   public void ajax()throws IOException {
              PrintWriter pw = ServletActionContext.getResponse().getWriter();
             ServletActionContext.getResponse().setContentType("text/html;charset=GBK");
             pw.print(1);
             pw.close();
   }

}
 
 
この例はwinXPsp 2、JB 9、Eclipse 3、j 2 sdk 1である.4.1 、Tomcat5、Tomcat4.1すべて実行可能