ajax与java经验小结

来源:互联网 发布:谭浩强c语言入门视频 编辑:程序博客网 时间:2024/05/07 08:41
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import com.csland.common.util.*;
//rex  Servlet 框架

/**
 * <p>Title: </p>
 * <p>Description: Servlet 类例子,建立servlet 直接依照此类建立<br>
 * 不要使用jbuilder建立Servlet的向导。不修改自身的web.xml配置</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * 
@author yl
 * 
@version 1.0
 
*/

public class Servlet1
    
extends HttpServlet {
  
private static final String CONTENT_TYPE = "text/html; charset=GBK";

  
//Initialize global variables
  public void init() throws ServletException {
  }


  
//Process the HTTP Get request
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, IOException 
{
    response.setContentType(CONTENT_TYPE);
    PrintWriter out 
= response.getWriter();
    String command 
= StrUtil.convNull(request.getParameter("cmd"));
    
if (command.equals("getcolsofview")) {
      String viewname 
= StrUtil.convNull(request.getParameter("viewname"));
      out.println(QueryViewBiz.getViewColsList(viewname));
      out.flush();
    }

    
else if (command.equals("wordcontent")) //获取惯用语内容-----2007.10.18修改开始
      
//String id = StrUtil.convNull(request.getParameter("id"));
     
      out.print(phrasecontent);
    }
//---2007.10.18修改结束
    else if (command.equals("getouttime")) {
    
        out.println(str);
        out.flush();
      }

    }

  }


  
//Process the HTTP Post request
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, IOException 
{
    doGet(request, response);
  }


  
//Clean up resources
  public void destroy() {
  }

}
----------------调用例子
<%@ page contentType="text/html; charset=GBK" %>

<%@ page import="java.util.*" %>
<%@ page import="com.csland.common.util.*" %>
<%@ page import="com.csland.workflow.helper.*" %>
<%@ page import="com.csland.hr.po.OzMember" %>
<%@ page import="com.csland.workflow.po.*" %>

<html>
<head>
<title>常用语</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="odoc.css" rel="stylesheet" type="text/css">
<script language="javascript">
//ajax代码,by yl 2007.10.17
var http_request = false;
function send_request(url) 
{//初始化、指定处理函数、发送请求的函数
  http_request = false;
  
//开始初始化XMLHttpRequest对象
  if(window.XMLHttpRequest) //Mozilla 浏览器
   http_request = new XMLHttpRequest();
  
if (http_request.overrideMimeType) {//设置MiME类别
    http_request.overrideMimeType('text/xml');
  }

  }

  
else if (window.ActiveXObject) // IE浏览器
  try {
      http_request 
= new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e) {
      
try {
              http_request 
= new ActiveXObject("Microsoft.XMLHTTP");
      }
 catch (e) {}
  }

  }

  
if (!http_request) // 异常,创建对象实例失败
   window.alert("不能创建XMLHttpRequest对象实例.");
  
return false;
   }

   http_request.onreadystatechange 
= processRequest;
   
// 确定发送请求的方式和URL以及是否同步执行下段代码
   http_request.open("GET", url, true);//发带参数请求
   http_request.send(null);
}

// 处理返回信息的函数
function processRequest() {
  
if (http_request.readyState == 4// 判断对象状态
  if (http_request.status == 200// 信息已经成功返回,开始处理信息
      
//;
       var str=http_request.responseText;
       
//alert(str)
       if (frm.r1.r1_1.checked)//代替
         opener.document.all['<%=ename.trim()%>'].value=str;
      }

      
else
      
{
        opener.document.all[
'<%=ename.trim()%>'].value+=str;
      }

  }
 else //页面不正常
      alert("您所请求的页面有异常。");
  }

  }

}


<%

int count = list.size();


%>
  
function isEmpty(s)
    
{
        
return ((s == null|| (s.length == 0))
    }


  function selNotes()
{
    
//var va1 = document.all.sel_notes.options[document.all.sel_notes.selectedIndex].text;
    var idx=document.all.sel_notes.selectedIndex;
    
if (idx<0)
    
{
      alert(
'请选择一条常用语!');
      
return ;
    }


    var id 
= document.all.sel_notes.options[idx].value;
    send_request(
'<%=StrUtil.convUrl("/servlet")%>/Servlet1?cmd=wordcontent&id='+id);
    
//send_request('getwordphrasecontent.jsp?id='+id);
    self.close();
  }

</script>
</head>
<body>
<form name="frm" method="POST" action="">
  
<table border="0" width="95%" height="54" cellspacing="1">
    
<tr>
      
<td width="100%" colspan="4" height="12" align="center">


<option value="<%=rolephrase.getId()%>"><%=StrUtil.convNull(rolephrase.getPhrasecontent())%></option>
<%
  }
}
%>

</select>

      
</td>
    
</tr>
<tr>
<td colspan="4" align="center">
<input type="radio" id="r1_2" class="bottom" name="r1" checked/>追加&nbsp;
<input type="radio" name="r1" id="r1_1" class="bottom" />替换
</br>
</br>
</td>
</tr>
    
<tr>
      
<td width="25%" height="12"></td>
      
<td width="25%" height="12">
        
<p align="right"><input type="button" value="确定" class="btnbk" name="btn_ok" tabindex="1" onclick="selNotes();"></td>
      
<td width="25%" height="12"><input type="button" value="取消" class="btnbk" name="btn_cancel" tabindex="2" onclick="self.close();"></td>
      
<td width="25%" height="12"></td>
    
</tr>
  
</table>
</form>
</body>
</html>
 经验:不是使用jsp,会多出空行
原创粉丝点击