Data Interaction between flex and Servlet

Source: Internet
Author: User
Summarize the data interaction between flex and Servlet:
Code 1-Flex code:
<? XML version = "1.0" encoding = "UTF-8"?> <Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" creationcomplete = "sendrequest ();"> <mx: SCRIPT> <! [CDATA [import MX. controls. alert; Public Function sendrequest (): void {var request: URLRequest = new URLRequest ("http: // localhost: 8080/flexandservlet/testservlet"); // first send the Servlet request: flexandservlet is the Java Web project name, And testservlet is the Servlet Request for processing data sent by Flex. method = urlrequestmethod. post; request. data = new urlvariables ("name = Gao huanjie"); // name indicates the parameter name var Loader: urlloader = new urlloader (); loader. load (request); loader. addeventlistener (event. complete, onloadercomplete);} private function onloadercomplete (Event: Event): void {var responseresult: String = (event.tar get) as urlloader ). data; // The data passed back from the servlet. show (responseresult, "") ;}]]> </MX: SCRIPT> </MX: Application>
Code 2-servlet code:
Package COM. ghj. packageofservlet; 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 testservlet extends httpservlet {Private Static final long serialversionuid =-response; Public void doget (httpservletrequest request, response) throws servletexception, ioexception {dopost (request, response );} public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {request. setcharacterencoding ("UTF-8"); response. setcontenttype ("text/pain; charset = UTF-8"); printwriter out = response. getwriter (); try {string name = request. getparameter ("name"); // obtain the system parameter value corresponding to the name parameter sent back by Flex. out. print ("name:" + name); out. println ("the servlet value is" + name);} finally {out. close ();}}}
Code 3-web.xml:
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><servlet><servlet-name>TestServlet</servlet-name><servlet-class>com.ghj.packageofservlet.TestServlet</servlet-class></servlet><servlet-mapping><servlet-name>TestServlet</servlet-name><url-pattern>/TestServlet</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>

Code 4-jsp code:

<%@ Page Language = "Java" contenttype = "text/html; charset = UTF-8" pageencoding = "UTF-8" %> <% string basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + request. getcontextpath () + "/"; %> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <HTML> 【
0-point download example]

Data Interaction between flex and Servlet

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.