• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

一个ajax的经典测试用例

发布: 2009-9-21 17:17 | 作者: 不祥 | 来源: 转载 | 查看: 80次 | 进入软件测试论坛讨论

领测软件测试网

[1]写index.jsp文件
 <%@ page contentType="text/html;charset=gb2312"%>
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 <html>
 <head>

<title>My JSP 'index.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">
 <meta http-equiv="description" content="This is my page">
 <link rel="stylesheet" type="text/css" href="styles.css">
 </head>

<body>
 <script type="text/javascript">
 var req;

function validate() {
 var idField = document.getElementById("userid");
 var url = "servlet/ValidateServlet?id=" + escape(idField.value);
 if (window.XMLHttpRequest) {
 alert("0");
 req = new XMLHttpRequest();
 }else if (window.ActiveXObject) {
 alert("1");
 req = new ActiveXObject("Microsoft.XMLHTTP");
 }
 if(req){
 req.open("GET", url, true);
 req.onreadystatechange = callback;
 req.send(null);
 }
 }
 function callback() {
 if (req.readyState == 4) {
 if (req.status == 200) {
 parseMessage();
 // update the HTML DOM based on whether or not message is valid
 }else{
 alert ("Not able to retrieve description" + req.statusText);
 }
 }
 }
 function parseMessage() {
 var message = req.responseXML.getElementsByTagName("message")[0];
 var name = req.responseXML.getElementsByTagName("name")[0];
 setMessage(message.firstChild.data,name.firstChild.data);
 }
 function setMessage(message,name) {
 var userMessageElement = document.getElementById("userIdMessage");
 userMessageElement.innerHTML = "<font color="red">" + message + " you "+name+"</font>";
 }
 </script>
 <div id="userIdMessage"></div>
 <input type="text"
 size="20"
 id="userid"
 name="id"
 onkeyup="validate();">
 </body>
 </html>
 
 [2] 写servlet/ValidateServlet.java类
 /*
 * 创建日期 2005-8-3
 *
 * TODO 要更改此生成的文件的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
 package com;//com包需要自己创建.

import java.io.IOException;
 import java.io.PrintWriter;
 import java.util.HashMap;

import javax.servlet.ServletConfig;
 import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;

/**
 * @author Administrator
 *
 * TODO 要更改此生成的类型注释的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
 public class ValidateServlet extends HttpServlet {

/**
 * Constructor of the object.
 */

private ServletContext context;
 private HashMap users = new HashMap();

public ValidateServlet() {
 super();
 }

/**
 * Destruction of the servlet. <br>
 */
 public void destroy() {
 super.destroy(); // Just puts "destroy" string in log
 // Put your code here
 }

/**
 * The doGet method of the servlet. <br>
 *
 * This method is called when a form has its tag value method equals to get.
 *
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
 public void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
 response.setContentType("text/xml");
 response.setHeader("Cache-Control", "no-cache");

String targetId = request.getParameter("id");
 System.out.println(targetId.trim());

if ((targetId != null) && users.containsKey(targetId.trim())) {

response.getWriter().write("<info><message>welcome</message><name>sdl</name></info>");
 } else {

response.getWriter().write("<info><message>kill</message><name>bush</name></info>");
 System.out.print("invalid");
 }

}

 /**
 * Initialization of the servlet. <br>
 *
 * @throws ServletException if an error occure
 */
 public void init(ServletConfig config) throws ServletException {
 this.context = config.getServletContext();
 users.put("greg","account data");
 users.put("duke","account data");

}

}
 
 [3]写web.xml文件<?xml version="1.0" encoding="UTF-8"?>

 <web-app version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <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>ValidateServlet</servlet-name>
 <servlet-class>com.ValidateServlet</servlet-class>
 </servlet>

<servlet-mapping>
 <servlet-name>ValidateServlet</servlet-name>
 <url-pattern>/servlet/ValidateServlet</url-pattern>
 </servlet-mapping>

</web-app>
 
 [4]说明:
 你可以在IE或FireFox里测试,在文本输入框里输入,当按键抬起,会在层中显示”kill you bush”。其中index.htm中的styles.css只是美化页面,没有列出来源代码。如果在servlet向客户端输出中文,需要编码转换。

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/

TAG: ajax AJAX Ajax 经典


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网