Javaweb How to pass background data to the foreground page HttpServletResponse and HttpServletRequest

Source: Internet
Author: User

Continue to summarize: After Java passes the action, how to write the data returned in the action to the foreground

First understand the following: HttpServletResponse object

(1). The Web server receives an HTTP request that creates a httpservletrequest and HttpServletResponse object for each request, sends data to the client to find HttpServletResponse, Fetch data from client to find HttpServletRequest;

(2). The HttpServletResponse object can send three types of data to the client: A. Response header B. Status code c. Data
(3). Go to see the HttpServletResponse API yourself
(4). Rsponse return data to the foreground:

A. Use OutputStream to write Chinese to the client:

    Response.setheader ("Content-type", "text/html;charset=utf-8");//Send a response header to the browser, set the browser to decode by UTF-8    String data = "China ";    OutputStream stream = Response.getoutputstream ();    Stream.Write (Data.getbytes ("UTF-8"));

B. Use writer to write Chinese to the client:

   Response.setcharacterencoding ("Utf_8");//Setup response is encoded as UTF-8    response.setheader ("Content-type", "text/ Html;charset=utf-8 ");//Send a response header to the browser, set the browser decoding method for UTF-8, in fact, set the sentence, but also the default setting of response encoding for UTF-8, but the best two sentences in the development of use    // Response.setcontenttype ("Text/html;charset=utf-8"), the same as the code function    printwriter writer = Response.getwriter ();    Writer.write ("China");


Look at how enterprises generally use: no matter how to use, nothing is packaged into a method, to the front desk to return data

Action:

public void searchmodelisused () {<strong>httpservletrequest req = Servletactioncontext.getrequest (); </ Strong>boolean result1 = This.voucherTypeService.queryProductionMode (); if (result1==false) {//non-production mode can modify template string result= "Is_production_mode";this.<strong>actionwrite</strong> ("{success: ' true ', info: ' + result + '}" );} Else{string vmId = Req.getparameter ("VmId"); Boolean result = This.voucherTypeService.checkVoucherModelUsed (vmId); This.actionwrite ("{success: ' true ', info: ' + result + ' '} ');}}
Results are returned to the foreground to display data with Actionwrite

Actionwrite as follows:

public void Actionwrite (String result) {if (result = = null) {result = "";} HttpServletResponse resp = Servletactioncontext.getresponse (); Resp.setcontenttype ("Text/json;charset=utf-8"); Resp.setheader ("Cache-control", "No-cache"); PrintWriter pw = null;try {pw = Resp.getwriter ();p w.write (Result),} catch (IOException e) {throw new Evoucherexception ("Get HTTP Write Inflow exception "+ e.getmessage ());} finally {if (PW = null) {Pw.close ();}}}

You can see that the Actionwrite method is a HttpServletResponse setting related information and using PrintWriter to write data to the forward table

In the foreground JS, is the response response backstage Pass the data is OK.

Callback:function (Options,success,response) {if (success) {checksessionoverdue (response.responsetext); var msg = Ext.JSON.decode (Response.responsetext); var msginfo = msg.info;if (Msginfo = = ' true ') {Ext.Msg.alert ("System Prompt", " The current template already has historical data, only the template name and activation date can be modified! "); Isaddvouchermodel = False;refreshaddvouchermodelform (false);} else if (msginfo== ' Is_production_mode ') {Isaddvouchermodel = True;refreshaddvouchermodelform (false);} Else{isaddvouchermodel = True;refreshaddvouchermodelform (false);}}}





Javaweb How to pass background data to the foreground page HttpServletResponse and HttpServletRequest

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.