Jsp라는 새로운 프로젝트 생성

버전은 1.0

localhost:8081에서 보이는 고양이 화면은 이 파일 경로의 index.jsp
이 파일 경로의 WEB-INF는 웹이 연결된 것.
Tomcat server: 톰켓 설치 폴더 선택
.jsp 확장자
이 index.jsp를 실행하면!

이렇게 결과를 확인할 수 있다.
(단, cmd에서 localhost:8081 고양이 화면을 실행 중이라면 shutdown 시켜야 함)
<% %> 스크립트릿: 모든 자바 코드 기술 가능<%@ %> 지시자: 페이지 속성을 지정<%! %> 선언자: 변수나 메서드 선언<%= %> 표현식: 결과 값을 출력<%-- --%> 주석: 코드 주석처리손님이 메뉴(JSP)를 주문하면:
<%
String name = "홍길동";
out.println("<h1>안녕하세요, " + name + "님!</h1>");
%>
➡ 브라우저에 전달되는 건 아래처럼 순수 HTML
<h1>안녕하세요, 홍길동님!</h1>
Java 코드는 클라이언트에 전혀 보이지 않는다.
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Jsp 태그</title>
</head>
<body>
<%
// 모든 자바 코드가 들어갈 수 있음
int a = 10;
if(a >= 10){
out.print("참!");
}else{
out.print("거짓!");
}
%>
<hr>
<% for(int i=0; i<5; i++){ %>
<input type="checkbox" name="hobby">
<% } %>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>헤더</title>
</head>
<body>
<header>
<h2>여기는 헤더 영역</h2>
<nav>
<ul>
<li>메뉴1</li>
<li>메뉴2</li>
<li>메뉴3</li>
</ul>
</nav>
</header>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="2_include_header.jsp"%> // header 포함
<html>
<body>
<section>
<p>여기는 본문입니다.</p>
<p>아래 footer가 include 됩니다.</p>
</section>
<%@ include file="2_include_footer.jsp"%> // footer 포함
</body>
<head>
<title>Title</title>
</head>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<footer>
copyright 2025 현서
</footer>
</body>
</html>
<%@ page import="java.util.Arrays" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>import</title>
</head>
<body>
<%
List<String> list = Arrays.asList("apple", "banana", "orange", "melon");
%>
<h2>select 태그에 list의 값을 반복문으로 생성</h2>
<select>
<% for(int i = 0; i<list.size(); i++){ %>
<option value="<%=list.get(i)%>"><%=list.get(i)%></option>
<% } %>
</select>
</body>
</html>
javax.servlet.http.HttpServletRequest<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>request</title>
</head>
<body>
<%--<form method="get" action="4_request_ok.jsp">--%>
<form method="post" action="4_request_ok.jsp">
<p>아이디: <input type="text" name="userid"></p>
<p>비밀번호: <input type="password" name="userpw"></p>
<p><a href="4_request_ok.jsp?userid=banana&userpw=1004">클릭하세요</a></p>
<p>취미: 게임<input type="checkbox" name="hobby" value="게임">운동<input type="checkbox" name="hobby" value="운동">영화<input type="checkbox" name="hobby" value="영화">음악<input type="checkbox" name="hobby" value="음악"></p>
<p><button type="submit">전송</button></p>
</form>
</body>
</html>
<%@ page import="java.util.Arrays" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String userid = request.getParameter("userid");
String userpw = request.getParameter("userpw");
String[] hobby = request.getParameterValues("hobby");
%>
<html>
<head>
<title>request</title>
</head>
<body>
<p>아이디: <%=userid%></p>
<p>비밀번호: <%=userpw%></p>
<p>취미: <%=Arrays.toString(hobby)%></p>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>response</title>
</head>
<body>
<form method="post" action="5_response_ok.jsp">
<p>이름: <input type="text" name="name"></p>
<p>나이: <input type="text" name="age"></p>
<p><button type = "submit">확인</button></p>
</form>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>response</title>
</head>
<body>
<h2>미성년자는 주류 구매 불가</h2>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String name = request.getParameter("name");
String age = request.getParameter("age");
int a = Integer.parseInt(age);
if(a > 19){
// 주류 구매 페이지로 이동
response.sendRedirect("5_response_success.jsp");
}else{
response.sendRedirect("5_response_fail.jsp");
}
%>
<html>
<head>
<title>response</title>
</head>
<body>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>response</title>
</head>
<body>
<h2>성인입니다! 주류 구매 가능</h2>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
Cookie [] cookies = request.getCookies();
if(cookies != null){
for(Cookie cookie: cookies){
String cookieName = cookie.getName();
if(cookieName.equals("userid")){
String cookieValue = cookie.getValue();
out.print("userid 쿠키의 값: " + cookieValue);
}
}
}else{
out.print("쿠키가 존재하지 않습니다.");
}
%>
<html>
<head>
<title>cookie</title>
</head>
<body>
<h2>쿠키 가져오기</h2>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
// 쿠키는 서버에서 생성, 클라이언트로 전달
Cookie cookie1 = new Cookie("userid", "apple");
Cookie cookie2 = new Cookie("username", "김사과");
cookie1.setMaxAge(1800); // 1800초
cookie2.setMaxAge(30); // 30초
response.addCookie(cookie1);
response.addCookie(cookie2);
%>
<html>
<head>
<title>cookie</title>
</head>
<body>
<h2>쿠키 설정</h2>
<p><a href = "6_cookie_get.jsp">쿠키 가져오기</a></p>
</body>
</html>