If you don't want to talk about it, just give it to the code. I believe all you need is code instead of a bunch of crap ......
1. java code:
Copy codeThe Code is as follows:
Package tt;
Import java. io. IOException;
Import java. io. PrintWriter;
Import javax. servlet. ServletException;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Public class TestA extends HttpServlet {
Private static final long serialVersionUID =-7999241892841130740L;
Protected void doGet (HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Request. setCharacterEncoding ("UTF-8 ");
String name = request. getParameter ("name ");
Response. setContentType ("text/xml; charset = UTF-8 ");
Response. setHeader ("Cache-Control", "no-cache ");
PrintWriter out = response. getWriter ();
Out. println ("<pront> ");
If (name. equals ("yangjinde ")){
Out. println ("<content>" + "sorry, 'yangjinde' has been registered" + "</content> ");
} Else {
Out. println ("<content>" + "can be registered" + "</content> ");
}
Out. println ("</pront> ");
Out. close ();
}
}
2. jsp code:
Copy codeThe Code is as follows:
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<%
String path = request. getContextPath ();
String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
%>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Base href = "<% = basePath %>">
<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 = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "description" content = "This is my page">
<Script>
// Set a variable
Var XMLHttpReq = false;
// Create an XMLHttpRequest object
Function createXMLHttpRequest (){
If (window. XMLHttpRequest) {// Mozilla
XMLHttpReq = new XMLHttpRequest ();
}
Else if (window. ActiveXObject ){
Try {
XMLHttpReq = new ActiveXObject ("Msxml2.XMLHTTP ");
} Catch (e ){
Try {
XMLHttpReq = new ActiveXObject ("Microsoft. XMLHTTP ");
} Catch (e ){}
}
}
}
// Send the request Function
Function send (url ){
CreateXMLHttpRequest ();
XMLHttpReq. open ("GET", url, true );
XMLHttpReq. onreadystatechange = proce; // specify the Response Function
XMLHttpReq. send (null); // send the request
}
Function proce (){
If (XMLHttpReq. readyState = 4) {// object status
If (XMLHttpReq. status = 200) {// The message is returned successfully. Start to process the information.
Var res = XMLHttpReq. responseXML. getElementsByTagName ("content") [0]. firstChild. data;
Window. alert (res );
Document. getElementById ("data"). innerHTML = res;
Document. getElementById ("name"). value = res;
} Else {
Window. alert ("sorry, the requested page has an exception ");
}
}
}
// Authentication
Function check (){
Var name = document. getElementById ("name"). value;
If (name = ""){
Alert ("Enter the content ");
Return false;
}
Else {
Send ('login? Name = '+ name );
}
}
</Script>
</Head>
<Body>
<Form action = "login" method = "post">
<Tr> <td> name: <input id = "name" type = "text" name = "name"/>
<Input type = "button" value = "Click here to try" onClick = "check ()"/>
</Td>
</Tr>
<Tr> <td> <div id = "data"> I will change it later !! </Div> </td> </tr>
</Form>
</Body>
</Html>
3. xml configuration file code:
Copy codeThe Code is as follows:
<? 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>
<Servlet-name> login </servlet-name>
<Servlet-class> tt. TestA </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name> login </servlet-name>
<Url-pattern>/login </url-pattern>
</Servlet-mapping>
<Welcome-file-list>
<Welcome-file> index. jsp </welcome-file>
</Welcome-file-list>
</Web-app>