Ajax + jsp send large string code
Ajax + jsp tutorial sending large string code
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> send a large string </title>
</Head>
<Body>
<Input type = "button" value = "Send" onClick = "send ();"/>
</Body>
<Script type = "text/javascript tutorial">
Var xmlHttp = null;
Function createXMLHttpRequest (){
If (window. ActiveXObject ){
XmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ");
} Else if (Window. XMLHttpRequest ){
XmlHttp = new XMLHttpRequest ();
}
}
Var bigstr = "$ ";
For (var I = 0; I <500; I ++ ){
// Format: keyi $ valuei $
Bigstr + = "key" + I + "$ value" + I + "$ ";
}
Function send (){
CreateXMLHttpRequest ();
XmlHttp. onreadystatechange = process;
XmlHttp. open ("POST", "TestGetBigStr", true );
XmlHttp. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded ");
XmlHttp. send (bigstr );
}
Function process (){
If (xmlHttp. readyState = 4 ){
If (xmlHttp. status = 200 ){
Alert ("sent successfully! ");
}
}
}
</Script>
</Html>
<%
Servlet code:
Package com. test;
Import java. io. BufferedReader;
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 TestGetBigStr extends HttpServlet {
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
DoPost (request, response );
}
Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
StringBuffer sb = new StringBuffer ();
BufferedReader br = new BufferedReader (request. getReader ());
String line = "";
While (line = br. readLine ())! = Null ){
Sb. append (line );
}
System. out. println (sb. toString ());
}
}
%>