Request and response at a glance

1.HTTP SERVLETRESPONSE简介

set heade
set int head
set data head
add haed
add int head
add int head

The byte stream can write any data;
the character stream can only write character data;

Use output stream to output data and output Chinese.

At this time, pay attention to the encoding table at this time.
The html language <mata> tag can simulate an HTTP response header to control the behavior of the browser
<mata> tag
When your program is written incorrectly, the browser will prompt to download the
browser is a text display tool
The display of characters and numbers in the browser Different

Two:
Common applications of RESPONSE

PRINT WRITE character output stream does not need to cast characters

Output data through the write stream of the response, pay attention to the problem of garbled characters

Solve related garbled problems

Foreigners like to use the ISO8859 code table
to solve the problem of garbled characters. Find the displayed code table and change it to the code table you want. At the same time, you need to control the browser to
open the code table to ensure the correct output of the file data.
Control response Select the code of the code table: response .setCharacterEncoding("UTF-8");

Control the code for selecting the code table when the browser is opened:
response.setHeader("context-type","text/html;chaeset=UTF-8");
Convenient method:response.setContextType("text/html;charset=UTF -8");
three:
download application of response

Downloading of files:
Create a down-load directory to ensure downloaded resources


Steps: 1. Get the file, use the stream to read the data resource in the web file, and use the context to read the resource in the servlet .
Get the absolute address of the resource and then read the resource and write the read resource to the browser
. The code written to the browser is as follows: out=response.getoutputstream();

The file name under development is generally a Chinese file.
If the downloaded file is a Chinese file, the file name requires URL encoding:
for example: response.setHeader("context-disposition","attachment;filename"+URLEncoder.encode(filename,"UTF-8 "));

four:
common applications: display random application pictures:

Steps: first build an image to find a bufferedImage

ImageIO class graphics input and output
stream response.utput stream() associated with the browser for image transmission

Font rotation method: graphics2D: rotate method rotate

Five:
common application timing refresh
control browser timing refresh refresh to control

refresh refresh header

It is very practical to use the parsing technology between browsers to parse
the browser's jump information
to make the browser automatically jump
. .setDateHeader("expries",system.currerTimeMillis()+1000*3600);


Code is cached for one hour.
Seven: Request redirection
Common response applications: The address of implementing request redirection and transferring the request to another service resource
will change. It is recommended not to use request redirection
, which will increase the burden
on the server. It must be used: 1. Request redirection must be used when logging in.
2. If forwarding is used when shopping, it will continue to repeat the last request
and will continue to purchase purchased items. Items that request redirection will only be
requested once.
Features: 1. The browser will send two requests to the server, which means there are two requests and responses
. 2. Using redirection technology, the browser address will change.

details of response

1. The mutual exclusion of getoutputstream and getwrite cannot be called at the same time. Note that the (easiest)
exception performance code cannot be written in the two forwarding programs: getoutputstream() has already been called for this response
and subsequent calls can basically use bytes There is no need to deal with the character stream
when the stream is done. When redirection is used, this problem does not occur because two requests are issued, and there are two responses.

2. The code data of the servlet will be written to the response, the server will read the data from the response and send it to the browser, and then the browser will parse it

The explanation of the request object The client's request The client's request is placed in the request. The explanation of the HTTPservletrequestAPI
is used to obtain the
request method. URI URI can represent any resource is a URL can represent a resource on the internet so the URI is the parent of the URL used more is the URI getHeadName can get the names of all the headers you can get the value of the headers by calling getHeads you can get all the same headers Value common methods: getRequestURL: Returns the complete URL when the client sends a request getRequestURI: Returns the complete URI when the client sends a request. For other common methods, see the picture of QQ friend handsome. Use request to get the header and request data: 1.String headVaule=reuqest.getHeader("Acceot-Encoding"); system.out.println(HeadVaule);













2.request.getHeads("名称");

3.e=request.getHeadNames();

There are two ways to get customers and the data they bring
- one way to bring
<a href="web resource">points to you</a> with hyperlinks

The second is to bring data to the server in a form
3. Get the value by name

request.getParamterNames();
4.MAP map=request.getparameterMap();

BeanUtils.populate(file, map)//username string[]//populate the Bean with the data of the map collection

BeanUtile.copyPropertites (user, forbean) 5.request of a copy of the
bean. getIntputStream(); How
to use and obtain the stream.
The most common way to get data:
string value=request.getParameter("username");
if(value!=null&&!value.trim().equals("")){
system.out.println(value) ;
}

Collect user data: Collect user data
through forms:
Collection must define name to the server

1. The form type that collects names is the text form2. The form
that collects passwords is the form that uses password3. The form
that collects gender is the form that uses radio4. The form
that collects the user's location
5. The form that collects hobbies uses the checkbox
6. The user profile form uses rows and cols forms
. 7. The form type that collects photos is the form type. The file input item must be added with name.
8. When the hidden input item is
obtained with the hidden type, use request.getparameter(""); to obtain the corresponding customer. The twelfth video of the data can be practiced
!!!!!!!!!!!!!!!!!!!!! The data must be checked before use! ! ! ! ! ! ! ! !

If the encoded data followed by the url is to be encoded in Chinese, submit the
Chinese garbled problem:
the way to solve the garbled problem : output
the control code table in the request:
request. setcharacterEncoding("UTF-8"); Only valid for post submission.
If get submission, we can only solve it manually: after getting the data, convert the username data to
UTF-8 code table, the code is as follows:
String name=request.getparameter( "username");
username=new String (username.getBytes("iso8859-1"),"UTF-8");
system.out.println(username);
The second way to solve the problem of garbled files is to directly change the configuration The file is only explained but not used, so I wo
n't explain too much here. For a detailed explanation, please see the 13th video of day06.


Forwarding of request and using the request domain object to bring data to the forwarding resource (important)
The request of request forwarding request is also a domain object
. The domain of the request and the scope of
the request.

A special output method in $(data)//jsp finds data from four fields and outputs it

Explanation and principle of Mvc technology
servlet receives the request and generates data, uses Javabean encapsulation to output
the data to jsp through forwarding technology and brings it to the request field, and then takes the data from the request field and finally outputs it.


The print write output stream
cannot be closed before the jump. If it is closed, an exception will be thrown.
2. For word will clear the data in the response.
For word Features: 1. The client only sends a request once and the server has multiple resource calls
2. Client browsing address bar does not change


The include method of the forwarding method in the request
uses the include to implement the technology of the page included in the servlet. The
included page does not appear in the global schema tag

In the web project, the writing method of various addresses is
best to start with /. For the browser, it represents the website
to the server, which represents the web application
request common application anti-leech .

request。getHeader("referer");
if(referer==null&&referer.startswith("http//localhost")){
response.sendredirect("/day06index.jsp")
return;
}
String data="";
response.getwriter().write(data);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324857755&siteId=291194637