A common JSP

Source: Internet
Author: User
<% @ Page contenttype = "text/html; charset = gb2312" %> <% @ page Language = "Java" Import = "Java. SQL. *, javax. servlet. *, javax. servlet. HTTP. *, Java. util. *, java.net. *, Java. io. * "%>
<%
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 'opposivesendxml. 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">
<! --
<LINK rel = "stylesheet" type = "text/CSS" href = "styles.css">
-->

</Head>

<Body>
Send XML to server <br>
<%!
Public String sendxml (string strposturl, string strxml, int nreadtimeout) throws exception
{
URL url = new URL (strposturl );
Urlconnection connection = URL. openconnection ();
Httpurlconnection httpconn = (httpurlconnection) connection;
Outputstream out = NULL;
String strtemprecxml = "";
Datainputstream Dis = NULL;
Try
{
Byte [] B = strxml. getbytes ();
Httpconn. setconnecttimeout (10000 );
Httpconn. setreadtimeout (nreadtimeout );
Httpconn. setrequestproperty ("Content-Length", String. valueof (B. Length ));
Httpconn. setrequestproperty ("Content-Type", "text/XML; charset = GBK ");
Httpconn. setrequestmethod ("Post ");
Httpconn. setdooutput (true );
Httpconn. setdoinput (true );
Out = httpconn. getoutputstream ();
Out. Write (B );
Out. Close ();
Byte brecbyte [] = new byte [1, 1025];
Dis = new datainputstream (httpconn. getinputstream ());
Bufferedinputstream Bis = new bufferedinputstream (DIS );
Int nbyte =-1;
Bytearrayoutputstream baostream = new bytearrayoutputstream ();
Nbyte = bis. Read (brecbyte, 0,1024 );
While (nbyte! =-1)
{
Baostream. Write (brecbyte, 0, nbyte );
Nbyte = bis. Read (brecbyte, 0,1024 );
}
Byte abyte [] = baostream. tobytearray ();
Strtemprecxml = new string (abyte, "GBK ");
Strtemprecxml = strtemprecxml. replaceall ("& lt;", "<");
Strtemprecxml = strtemprecxml. replaceall ("& gt;", "> ");
}
Catch (exception E)
{
System. Out. println ("an exception occurred when sending the POST request! "+ E );
E. printstacktrace ();
}
// Use finally blocks to close the input stream
Finally
{
Try
{
If (OUT! = NULL)
{
Out. Close ();
Out = NULL;
}
If (DIS! = NULL)
{
Dis. Close ();
Dis = NULL;
}
If (httpconn! = NULL)
{
Httpconn. Disconnect ();
Httpconn = NULL;
}
}
Catch (ioexception ex)
{
Ex. printstacktrace ();
}
}
Return strtemprecxml;
}

%>

<Form name = "frm" Action = "testbossresponse. jsp" method = "Post">
<Center> URL <input type = "text" name = "url" style = "width: 150px"/>
<Br/>
Time: <input type = "text" name = "time" style = "width: 164px"/>
<Br/>
<Textarea name = "testxml" rows = "20" Cols = "50"> </textarea>
<Br/>
<Input type = "hidden" name = "subtag" value = ">
<Input type = "Submit" value = "Submit" onclick = "javascript: Document. frm. subtag. value = 'sub'">
</Center>
</Form>

<%
String subtag = request. getparameter ("subtag") = NULL? "": Request. getparameter ("subtag ");
String MSG = "";
If (subtag. Equals ("sub ")){
String rs_url = request. getparameter ("url ");
String rs_time = request. getparameter ("time ");
String rs_value = request. getparameter ("testxml ");
MSG = sendxml (rs_url, rs_value, integer. parseint (rs_time ));
If (msg. Length ()> 0)
{
Out. println ("Result \ n:" + MSG );
Out. println ("sending simulated account information \ n ");
Sendxml ("http: // localhost: 8090/mobilead/mainservers", "<operation_out>"
+ "<SERVICE_NAME> pcs_mobileadvertise_agent_information </SERVICE_NAME>"
+ "<REQUEST_TYPE> 1001 </REQUEST_TYPE>"
+ "<Sysfunc_id> 93001001 </sysfunc_id>"
+ "<Request_seq> 100000031186 </request_seq>"
+ "<Response_time> 20091013113712 </response_time>"
+ "<Response_seq> 101313103 </response_seq>"
+ "<Request_source> 201015 </request_source>"
+ "<Response>"
+ "<Start_time> 0 </start_time>"
+ "<Brand_id> 1012 </brand_id>"
+ "<Total_amount> 20 </total_amount>"
+ "</Response>"
+ "<Content/>"
+ "</Operation_out>", 10000 );
}
Else
{
Out. println ("Result \ n:" + "No returned result ");
}
}




%>



</Body>
</Html>

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.