Ajax Test Case
Related Tags:
Ajax Test Case:
[Prerequisite]
Installation jdk1.4 and tomcat5.0.28, IE5.5 of FireFox. Access is that
[1] document written index.jsp
<% @ Page contentType = "text / html; gb2312 charset ="%>
<! 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 (the "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] write servlet / ValidateServlet.java category
/ *
* Date created 2005-8-3
*
* TODO to change this generation of the document template, go to
* Window - for the first option - Java - code style - code template
* /
Package com; / / com need to create their own packages.
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 to change this type Notes generated template, go to
* Window - for the first option - Java - code style - code template
* /
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] write web.xml file
<? 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] Note:
You can IE or FireFox, testing, and enter the text input when the lift button will be displayed in the layer "kill you bush." Styles.css index.htm in which only beautify the pages, not listed source code. If the client servlet output to the Chinese, need transcoding.
[Redundant]
You need to know tomcat works. Familiar with xmlHttpRequest, java servlet, html knowledge.
- AJAX Case Study of Gmail
- A case XP style button production
- AJAX case study of the detailed analysis of Gmail
- HTML, JavaScript, css reference case
- Text-align: justify achieve alignment case ends
- CSS2 use case
- Fireworks production Logo case
- Personal Website failure analysis of case studies (all)
- AJAX Case Study of google reader
- Ajax Test Case
- DIV forms a Test Case
- CSS case of the use of elements POSITION
- Courseware case of multiple-choice
- Flash failure to resolve the case of loading
- Flash MX 2004 features a case study
- JavaScript modify the registry case
- FireWorks case on the production of Logo
- CSS Case Analysis
- CSS Case Analysis 2
- Flash Case study: a randomized-ray results




