Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Java Web HttpServletResponse object

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to use Java Web HttpServletResponse objects". Friends who are interested might as well take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor show you how to use Java Web HttpServletResponse objects.

(1) concept

There are HttpServletRequest and HttpServletResponse objects in the parameters of the doGet, doPost, and service methods.

HttpServletRequest represents a http request, which is used when you want to get the data in the request.

HttpServletResponse represents the http response and is used when there is data to be sent to the browser.

(2) Common methods

1) setHeader (String name, String value)

Set the response header:

① sets text formatting and encoding

② regular refresh

The timing refresh is set as the response header through Refresh, and the jump url can be realized after how long.

/ / another way to write, use the setHeader method response.setHeader ("Content-Type", "text/html;charset=utf-8"); / / get the character output stream PrintWriter pw=response.getWriter (); pw.write ("enter Baidu in 5 seconds") / / set timing refresh-how long will it take to jump to response.setHeader ("Refresh", "5 Refresh" = http://www.baidu.com"); / / how often to self-refresh response.setHeader ("Refresh", "5") / / use request as a domain object to store data request.setAttribute ("name", "zhangsan")

2) getWriter ()-- > PrintWriter

GetOutputStream ()-- > ServletOuputStream / / output information to browser side / / to get byte stream, write data to browser side ServletOutputStream sos=response.getOutputStream (); sos.write ("200.getBytes ()); sos.write ("\ n ".getBytes ()); byte [] bs=" world ".getBytes () Sos.write (bs); sos.write (".getBytes ()); sos.write (" China ".getBytes (" utf-8 "))

3) setContentType ()

Set the Chinese encoding and text format of the response to the browser

/ / sets the text format and encoding response.setContentType ("text/html;charset=utf-8") to be displayed on the browser side

4) redirect

SendRedirect ("xxxx") / / xxx represents the redirected address

① is used for the transfer of internal resources on the server side, and can also be used to jump between different applications and different servers.

A variable occurs in the ② redirect browser address bar, and the browser address bar will be the address after the redirection

③ redirection is equivalent to 2 requests and 2 responses

④ redirects are usually used when things are done, just to jump to the next component.

/ / redirect / / usually things are done, but for the next component jump, the address of the browser address bar will change / / redirect is two requests, data stored in request scope cannot be shared / / redirect is response.sendRedirect ("index.jsp") called through HttpServletResponse object / / response.sendRedirect ("register.html"); at this point, I believe you have a better understanding of "how to use Java Web HttpServletResponse objects". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report