뇨내

[Servlet-Eclipse] 작성해보기 본문

Server

[Servlet-Eclipse] 작성해보기

삐옥 2022. 1. 15. 17:38

 

 

 

 

 

 


 

서블릿 클래스 선언

doGet/doPost 메소드 선언

동적 HTML문서 작성 구현

 Ctrl + F11

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.test.servlet;
 
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;
 
//1. 서블릿 클래스 선언
//a. javax.servlet.http.HttpServlet 클래스를 상속받는다. > 간편함 > 선택
public class Hello extends HttpServlet {
 
    //2. doGet/doPost 메소드 선언
    //a. 매개변수 작성(2개)
    //b. 예외 미루기
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        
        //3. 동적 HTML 문서 작성 구현
        response.setCharacterEncoding("UTF-8");
        PrintWriter writer = response.getWriter();
        
        writer.print("<html>");
        writer.print("<head>");
        writer.print("<meta charset='utf-8'>");
        writer.print("<title>Hello</title>");
        writer.print("</head>");
        writer.print("<body>");
        writer.print("<h1>Hello World!!</h1>");
        writer.print("<p>서블릿으로 만든 페이지입니다.</p>");
        writer.print("</body>");
        writer.print("</html>");
        
        writer.close(); //동적으로 페이지 1장 완성
        
        
        //Ctrl + F11
        
        //자바 파일은 웹(http://)에서 호출이 불가능하다.
        //- http://localhost:8090/servlet/servlet/com.test.serlvet.Hello
        
        //자바를 브라우저를 통해서 호출할 방법 필요!!
        //- 가상 주소
        //- http://localhost:8090/servlet/hello.do
        
        
        //자바 파일의 컴파일
        //1. Ctrl + F11 > 사전 준비용
        //2. 브라우저 요청 > 정석
    }
    
}
 
 
 
 
 
 
 
 
 
 
cs

 

 

 


 

'Server' 카테고리의 다른 글

[JSP-Eclipse] JSTL  (0) 2022.01.15
[Servlet-Eclipse] setCharacterEncoding("UTF-8") 메소드  (0) 2022.01.15
[Servlet] 기초  (0) 2022.01.14
Comments