Install jdk1.4 and tomcat5.0.28, and ie5.5 Get Firefox. Enter the subject
[1] Write the index. jsp file
<% @ 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] Write Servlet/validateservlet. Java class
/*
* Creation date: 2005-8-3
*
* To change the template of the generated file in todo, go
* Window-preferences-Java-code style-Code Template
*/
Package com; // create a package by yourself.
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
*
* To change the template of the type comment generated by todo, go
* Window-preferences-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] writing a 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] Description:
You can test it in IE or Firefox and enter it in the text input box. when the key is lifted, "kill you Bush" is displayed in the layer ". In index.htm, styles.css only beautifies the page without listing the source code. If the servlet outputs Chinese characters to the client, the encoding must be converted.