記錄

Servlet) out.print()에 대한 이해 본문

Web/Servlet

Servlet) out.print()에 대한 이해

surhommejk 2018. 3. 29. 12:50

java.io

Class PrintWriter

java.lang.Object

java.io.Writer

java.io.PrintWriter

All Implemented Interfaces:

Closeable, Flushable, Appendable, AutoCloseable


public class PrintWriter

extends Writer


Prints formatted representations of objects to a text-output stream. This class implements all of the print methods found in PrintStream. It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams.


Unlike the PrintStream class, if automatic flushing is enabled it will be done only when one of the println, printf, or format methods is invoked, rather than whenever a newline character happens to be output. These methods use the platform's own notion of line separator rather than the newline character.


Methods in this class never throw I/O exceptions, although some of its constructors may. The client may inquire as to whether any errors have occurred by invoking checkError().


javax.servlet 

Interface ServletResponse

All Known Subinterfaces:

HttpServletResponse


All Known Implementing Classes:

HttpServletResponseWrapper, ServletResponseWrapper

public interface ServletResponse


Defines an object to assist a servlet in sending a response to the client. The servlet container creates a ServletResponse object and passes it as an argument to the servlet's service method.


To send binary data in a MIME body response, use the ServletOutputStream returned by getOutputStream(). To send character data, use the PrintWriter object returned by getWriter(). To mix binary and text data, for example, to create a multipart response, use a ServletOutputStream and manage the character sections manually.


The charset for the MIME body response can be specified explicitly using the setCharacterEncoding(java.lang.String) and setContentType(java.lang.String) methods, or implicitly using the setLocale(java.util.Locale) method. Explicit specifications take precedence over implicit specifications. If no charset is specified, ISO-8859-1 will be used. The setCharacterEncoding, setContentType, or setLocale method must be called before getWriter and before committing the response for the character encoding to be used.


See the Internet RFCs such as RFC 2045 for more information on MIME. Protocols such as SMTP and HTTP define profiles of MIME, and those standards are still evolving.


getWriter

public java.io.PrintWriter getWriter() throws java.io.IOException


Returns a PrintWriter object that can send character text to the client. The PrintWriter uses the character encoding returned by getCharacterEncoding(). If the response's character encoding has not been specified as described in getCharacterEncoding (i.e., the method just returns the default value ISO-8859-1), getWriter updates it to ISO-8859-1.

Calling flush() on the PrintWriter commits the response.


Either this method or getOutputStream() may be called to write the body, not both.


Returns:

a PrintWriter object that can return character data to the client


Throws:

UnsupportedEncodingException - if the character encoding returned by getCharacterEncoding cannot be used


java.lang.IllegalStateException - if the getOutputStream method has already been called for this response object


java.io.IOException - if an input or output exception occurred

See Also:

getOutputStream(), setCharacterEncoding(java.lang.String)



PrintWriter out = response.getWriter();

out.print()


out.print("~~~") 는 파라미터로 설정된 string을 '클라이언트'(브라우저)에게 보내는 기능이다. 클라이언트는 이를 string형태로 받아서 읽게 되는데 여기 html 태그가 있으면 html 태그로 받아들여서 html 기능으로 인식하고 이를 실행한다.


내가 궁금했던 것은 <script> ~~ </script>만 보내도 기능이 작동하는 것이었는데 이는 간단했다. 굳이 <html>이나 <body>같은 구문들이 없어도 <script>만 있어도 브라우저는 이를 실행 할 수 있었다.


또 내가 궁금했던 것은 그렇게 out.print(" ~~~ ")로 string을 보냈을 때에 이 값을 받는 주체였다. 그 out.print() 메소드가 있는 페이지로 가기 바로 직전의 페이지가 받는 것인가? 하고 생각했었다. 이는 클라이언트와 서버라는 두 주체에 대한 이해가 부족해서 몰랐던 것이다. 


out.print()는 '클라이언트'(브라우저)에게 보내는 것이다. 즉, 해석기 자체에 보내는 것이지 특정 페이지에 보낸다던가 하는 것이 아닌 것이다. 그렇기 때문에 out.print()로 받은 구문을 브라우저가 해석하고 이를 html문서로 인식하고 태그가 있으면 그에 맞는 기능을 실행하고 그게 아니면 스트링만을 출력하는 것이다. 심지어 string 조차 <html>이나 <body> 같은 것이 없어도 그냥 페이지로 출력이 가능했다.

Comments