Ajax + jsp send large string code

Source: Internet
Author: User

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 ());
}
}
%>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.