본문 바로가기
JSP

JSP & Servlet

by TR. 2021. 1. 18.

JSP  |  Java Server Page

ㄴ 자바로 서버 페이지 작성을 하기 위한 언어 (HTML+JSP 태그 사용)

 

  • JSP : HTML 문서 안에 자바 코드 작성 
<!-- <%@ page> : JSP태그로 페이지 내 전반적인 환경설정 -->
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>test.jsp</title>
</head>
<body>
	<%
		int a = 1;
		int b = 2;
		int c = a+b;
	%>
	<%=a%> + <%=b%> = <%=c%>
</body>
</html>

 

  • Servlet : 자바 문서 안에 HTML 작성
package day00;

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

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

/**
 * Servlet implementation class Ex01
 */
@WebServlet("/Ex01")
public class Ex01 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Ex01() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		int a = 10;
		int b = 20;
		int c = a + c;
        
		PrintWriter out = response.getWriter();
		
        	out.print("<html><head><title>Addition</title></head>");
		out.print("<body>" + a + "+" + b + "=" + c + "</body></html>");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

 

'JSP' 카테고리의 다른 글

[2] JSP 내장 객체  (0) 2021.01.19
[1] JSP 태그  (0) 2021.01.19
[0] 준비  (0) 2021.01.19

댓글