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. 




Related articles:

Fireworks produced by the tutorial cool icon
  Not long ago on the site to see such an icon (as below), a time that the bomb modeling cool, three-dimensional sense is very good, very cute, and now we use Fireworks MX learn how to do one, and on the following simple to tell us about the production process.    Steps:   ...
Flash ActionScript Learning: AS written to the MC
  Demonstration effects:  <embed src=http://bbs.flash8.net/bbs/UploadFile/2005-9/20059922476884.swf pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width=550 height=400>   Click here to dow...
Making a form px border Methods
  Had experience in the production of web friends must know: Generally speaking, the forms borders defined as a px, which border = 1, and is actually 2 px.    And we need that in many cases the real borders for a px the form below to introduce several ways.    First, use ne...
How CSS definition forms the background color to turn?
  Application div + css page layout, the form has been used very little, at least in table form just as content data, and no longer as a means of page layout.    Form data is a common data format, such as product lists, product parameters, numerical, and so on, and sometimes every l...
Briefly: Website Design and role LOGO
  Fair-goers to the Internet to also find a full article on LOGO, which is open ONLY LOOG that such a precedent.    Poorfish but reluctance to personally操刀, Couchu the following paragraphs of text, write themselves McGREGOR is very simple, shallow-po, if there are any problems, please...