JSP

[JSP] 4. 서블릿과 JSP 파일 만들어 보기

Song hyun 2024. 6. 30. 16:00
728x90
반응형

[JSP] 4. 서블릿과 JSP 파일 만들어 보기

 

 

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

<h1>home.jsp 파일 입니다.</h1>

<p>주소 설계: http://localhost:8080/demo_3/home.jsp</p>

</body>

</html>

package com.tenco;

import java.io.IOException;
import java.io.PrintWriter;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@WebServlet(name = "echo2", urlPatterns = {"/echo2"})
public class Echo2 extends HttpServlet {
    
    public Echo2() {
        super();
    }
    
    // get 요청 방식 
    // http://localhost:8080/demo_3/echo2
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // request -> req(
        System.out.println("doGet 메서드 호출 확인 ");
        // 자바.io 객체 (스트림 통해 데이터를 넣을 예정) 
        PrintWriter pw = resp.getWriter(); 
        pw.print("<!DOCTYPE html>");
        pw.print("<html lang=\"en\">");
        pw.print("<head>");
        pw.print("    <meta charset=\"UTF-8\">");
        pw.print(" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">");
        pw.print("    <title>Document</title>");
        pw.print("</head>");
        pw.print("<body>");
        pw.print("<section>");
        pw.print("<p style=\"color: red;\" >Hello First Srvlet 반가워</p>");
        pw.print("</section>");
        pw.print("</body>");
        pw.print("</html>");
        resp.setContentType("text/html; charset=utf-8");
    }
    
    
    
    // post 요청 방식 
    // http://localhost:8080/demo_3/echo2
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // TODO Auto-generated method stub
        super.doPost(req, resp);
    }
    
}
package com.tenco;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "echo", urlPatterns = { "/echo" })
public class Echo extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Echo() {
		super();
	}

	@Override
	public void init() throws ServletException {
		System.out.println("Echo 서블릿 클래스가 --> 컴파일 --> .class 파일로 변환");
		System.out.println("init() 메서드는 인스턴스화 될 때 딱 한번 호출 되는 메서드");
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	
	// POST 요청시 동작 하는 메서드
	// 주소 설계
	// http://localhost:8080/demo_3/echo
	protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
	
	
	}

}

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>home.jsp 파일 입니다.</h1>
	<p>주소 설계: http://localhost:8080/demo_3/home.jsp</p>
</body>
</html>
package com.tenco;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "echo", urlPatterns = { "/echo" })
public class Echo extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Echo() {
		super();
	}

	@Override
	public void init() throws ServletException {
		System.out.println("Echo 서블릿 클래스가 --> 컴파일 --> .class 파일로 변환");
		System.out.println("init() 메서드는 인스턴스화 될 때 딱 한번 호출 되는 메서드");
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	
	// POST 요청시 동작 하는 메서드
	// 주소 설계
	// http://localhost:8080/demo_3/echo
	protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		System.out.println("안녕 서버측 콘솔에서 출력을 합니다.");
	}
	
	@Override
	public void destroy() {
		System.out.println("메모리에서 내려갈 때 호출되는 메서드");
	}

}
728x90
반응형