관리 메뉴

거니의 velog

231107_JSP 개론 9 본문

대덕인재개발원_웹기반 애플리케이션

231107_JSP 개론 9

Unlimited00 2023. 11. 7. 08:29

[validation.js]

function CheckAddProduct() {

	var productId = document.getElementById("productId");
	var name = document.getElementById("name");
	var unitPrice = document.getElementById("unitPrice");
	var unitsInStock = document.getElementById("unitsInStock");
	
	// 상품아아디 체크
	if (!check(/^P[0-9]{4,11}$/, productId,
			"[상품 코드]\nP와 숫자를 조합하여 5~12자까지 입력하세요\n첫 글자는 반드시 P로 시작하세요"))
		return false;
		
	// 상품명 체크
	if (name.value.length < 4 || name.value.length > 12) {
		alert("[상품명]\n최소 4자에서 최대 50자까지 입력하세요");
		name.select();
		name.focus();
		return false;
	}
	
	// 상품 가격 체크
	if (unitPrice.value.length == 0 || isNaN(unitPrice.value)) {
		alert("[가격]\n숫자만 입력하세요");
		unitPrice.select();
		unitPrice.focus();
		return false;
	}

	if (unitPrice.value < 0) {
		alert("[가격]\n음수를 입력할 수 없습니다");
		unitPrice.select();
		unitPrice.focus();
		return false;
	} else if (!check(/^\d+(?:[.]?[\d]?[\d])?$/, unitPrice,
			"[가격]\n소수점 둘째 자리까지만 입력하세요")){
		return false;
	}

	// 재고 수 체크
	if (isNaN(unitsInStock.value)) {
		alert("[재고 수]\n숫자만 입력하세요");
		unitsInStock.select();
		unitsInStock.focus();
		return false;
	}

	function check(regExp, e, msg) {

		if (regExp.test(e.value)) {
			return true;
		}
		alert(msg);
		e.select();
		e.focus();
		return false;
	}

	 document.newProduct.submit()
}

[addProduct.jsp]

<%@ page pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
	<head>
		<link rel="stylesheet" 	href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
		<title>상품 등록</title>
		<script type="text/javascript" src="${pageContext.request.contextPath}/resources/js/validation.js"></script>
	</head>
	<body>
		<%@ include file="menu.jsp" %>	
		<div class="jumbotron">
			<div class="container">
				<h1 class="display-3">상품 등록</h1>
			</div>
		</div>
		<div class="container">
			<form name="newProduct" action="processAddProduct.jsp" class="form-horizontal" method="post" enctype="multipart/form-data">
				<div class="form-group row">
					<label class="col-sm-2">상품 코드</label>
					<div class="col-sm-3">
						<input type="text" name="productId" id="productId" class="form-control" >
					</div>
				</div>
				<div class="form-group row">
					<label class="col-sm-2">상품명</label>
					<div class="col-sm-3">
						<input type="text" name="name" id="name" class="form-control" >
					</div>
				</div>
				<div class="form-group row">
					<label class="col-sm-2">가격</label>
					<div class="col-sm-3">
						<input type="text" name="unitPrice" id="unitPrice" class="form-control" >
					</div>
				</div>
				<div class="form-group row">
					<label class="col-sm-2">상세 정보</label>
					<div class="col-sm-5">
						<textarea name="description" cols="50" rows="2"
							class="form-control"></textarea>
					</div>
				</div>
				<div class="form-group row">
					<label class="col-sm-2">제조사</label>
					<div class="col-sm-3">
						<input type="text" name="manufacturer" class="form-control">
					</div>
				</div>
				<div class="form-group row">
					<label class="col-sm-2">분류</label>
					<div class="col-sm-3">
						<input type="text" name="category" class="form-control" >
					</div>
				</div>
				<div class="form-group row">
					<label class="col-sm-2">재고 수</label>
					<div class="col-sm-3">
						<input type="text" name="unitsInStock" id="unitsInStock" class="form-control" >
					</div>
				</div>
				<div class="form-group row">
					<label class="col-sm-2">상태</label>
					<div class="col-sm-5">
						<input type="radio" name="condition" value="New"> 신규 제품 
						<input type="radio" name="condition" value="Old"> 중고 제품 
						<input type="radio" name="condition" value="Refurbished"> 재생 제품
					</div>
				</div>
				<div class="form-group row">
					<label class="col-sm-2">이미지</label>
					<div class="col-sm-5">
						<input type="file" name="productImage" class="form-control">
					</div>
				</div>
				<div class="form-group row">
					<div class="col-sm-offset-2 col-sm-10 ">
						<input type="button" class="btn btn-primary" value="등록" onclick="CheckAddProduct()">
					</div>
				</div>
			</form>
		</div>
	</body>
</html>

- http://localhost/addProduct.jsp


* 인증과 인가라는 단어가 떠올라야 한다.

* 인증? 인가?

* 예를 들어, 청와대 못들어감. 들어갈 수 있는 권한이 없다. 가겠다고 예약해서 승인-허가가 떨어지면 들어갈 수 있다. 내가 최종적으로 가고자 하는 목적지. 이를 타겟이라 부름.

* 최초에 가야할 타겟에 도착하기 까지의 모든 과정에서 인증과 인가가 완벽하게 이루어져야 한다. 

* 클라이언트와 서버의 개념으로 보면, 브라우저에서 타겟으로 넘어가는데, 타겟 문 앞에서 문을 두드림. 물어보면 목적지에 해당하는 타겟 컨트롤러가 잠시 대기시키고 출입허가를 살펴봄. 여기에 해당 허용된 사용자의 이름을 확인하고 들여보내는 개념

url-pattern은 사용자가 접근해야 할 target이 된다.
NONE을 기본 설정으로 한다.
BASIC과 FORM을 주로 쓰고, 회사가 개인의 SSL 인증서를 보유하고 있다면 CLIENT-CERT도 종종 쓴다.
name 값에 j_ 를 붙이는 것이 일반적이다. 정해진 규칙임.

* 스프링 프레임워크에서도 구조는 같음. 


[security01.jsp]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html class="no-js" lang="zxx">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="x-ua-compatible" content="ie=edge" />
    <title>쉽게 배우는 JSP 웹 프로그래밍</title>
    <meta name="description" content="" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <%@ include file="/pageModule/headPart.jsp" %>
</head>

<body>
    <%@ include file="/pageModule/header.jsp" %>

    <div class="breadcrumbs" style="padding-top:40px;">
        <div class="container">
            <div class="row align-items-center">
                <div class="col-lg-6 col-md-6 col-12">
                    <div class="breadcrumbs-content">
                        <h1 class="page-title">시큐리티</h1>
                    </div>
                </div>
                <div class="col-lg-6 col-md-6 col-12">
                    <ul class="breadcrumb-nav">
                        <li><a href="/">INDEX</a></li>
                        <li>CH10</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>

    <section class="about-us section">
        <div class="container">
            <div class="row align-items-center justify-content-center">
                <div class="col-lg-12 col-md-12 col-12">
                    <div class="content-left wow fadeInLeft" data-wow-delay=".3s">
						<h3>인증 성공했습니다!</h3>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <%@ include file="/pageModule/footer.jsp" %>

    <%@ include file="/pageModule/footerPart.jsp" %>
</body>

</html>

[tomcat-users.xml]

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
--><tomcat-users version="1.0" xmlns="http://tomcat.apache.org/xml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd">
<user password="admin" roles="manager-gui" username="admin"/>
<!--
  By default, no user is included in the "manager-gui" role required
  to operate the "/manager/html" web application.  If you wish to use this app,
  you must define such a user - the username and password are arbitrary.

  Built-in Tomcat manager roles:
    - manager-gui    - allows access to the HTML GUI and the status pages
    - manager-script - allows access to the HTTP API and the status pages
    - manager-jmx    - allows access to the JMX proxy and the status pages
    - manager-status - allows access to the status pages only

  The users below are wrapped in a comment and are therefore ignored. If you
  wish to configure one or more of these users for use with the manager web
  application, do not forget to remove the <!.. ..> that surrounds them. You
  will also need to set the passwords to something appropriate.
-->
<!--
  <user username="admin" password="<must-be-changed>" roles="manager-gui"/>
  <user username="robot" password="<must-be-changed>" roles="manager-script"/>
-->
<!--
  The sample user and role entries below are intended for use with the
  examples web application. They are wrapped in a comment and thus are ignored
  when reading this file. If you wish to configure these users for use with the
  examples web application, do not forget to remove the <!.. ..> that surrounds
  them. You will also need to set the passwords to something appropriate.
-->
<!--
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <user username="tomcat" password="<must-be-changed>" roles="tomcat"/>
  <user username="both" password="<must-be-changed>" roles="tomcat,role1"/>
  <user username="role1" password="<must-be-changed>" roles="role1"/>
-->
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <user username="tomcat" password="1234" roles="tomcat"/>
  <user username="both" password="1234" roles="tomcat,role1"/>
  <user username="role1" password="1234" roles="role1"/>
</tomcat-users>

[web.xml]

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>JSPBook</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- ch10 기본 인증 처리 방법으로 보안 처리하기 -->
  <security-role>
  	<role-name>role1</role-name>
  </security-role>
  <security-constraint>
  	<web-resource-collection>
  		<web-resource-name>JSPBookProject</web-resource-name>
  		<url-pattern>/ch10/security01.jsp</url-pattern>
  		<http-method>GET</http-method>
  	</web-resource-collection>
  	<auth-constraint>
  		<description></description>
  		<role-name>role1</role-name>
  	</auth-constraint>
  </security-constraint>
  <login-config>
  	<auth-method>BASIC</auth-method>
  </login-config>
  <!-- ch10 기본 인증 처리 방법으로 보안 처리하기 End -->
  
</web-app>

- http://localhost/ch10/security01.jsp

비인가 된 사용자 계정은 계속해서 못들어가게 처리됨


[login.jsp]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html class="no-js" lang="zxx">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="x-ua-compatible" content="ie=edge" />
    <title>쉽게 배우는 JSP 웹 프로그래밍</title>
    <meta name="description" content="" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <%@ include file="/pageModule/headPart.jsp" %>
</head>

<body>
    <%@ include file="/pageModule/header.jsp" %>

    <div class="breadcrumbs" style="padding-top:40px;">
        <div class="container">
            <div class="row align-items-center">
                <div class="col-lg-6 col-md-6 col-12">
                    <div class="breadcrumbs-content">
                        <h1 class="page-title">시큐리티</h1>
                    </div>
                </div>
                <div class="col-lg-6 col-md-6 col-12">
                    <ul class="breadcrumb-nav">
                        <li><a href="/">INDEX</a></li>
                        <li>CH10</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>

    <section class="about-us section">
        <div class="container">
            <div class="row align-items-center justify-content-center">
                <div class="col-lg-12 col-md-12 col-12">
                    <div class="content-left wow fadeInLeft" data-wow-delay=".3s">
						<form action="j_security_check" method="post">
							아이디 : <input type="text" name="j_username" /><br />
							비밀번호 : <input type="text" name="j_password" /><br />
							<button type="submit">전송</button>
						</form>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <%@ include file="/pageModule/footer.jsp" %>

    <%@ include file="/pageModule/footerPart.jsp" %>
</body>

</html>

[login_failed.jsp]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html class="no-js" lang="zxx">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="x-ua-compatible" content="ie=edge" />
    <title>쉽게 배우는 JSP 웹 프로그래밍</title>
    <meta name="description" content="" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <%@ include file="/pageModule/headPart.jsp" %>
</head>

<body>
    <%@ include file="/pageModule/header.jsp" %>

    <div class="breadcrumbs" style="padding-top:40px;">
        <div class="container">
            <div class="row align-items-center">
                <div class="col-lg-6 col-md-6 col-12">
                    <div class="breadcrumbs-content">
                        <h1 class="page-title">시큐리티</h1>
                    </div>
                </div>
                <div class="col-lg-6 col-md-6 col-12">
                    <ul class="breadcrumb-nav">
                        <li><a href="/">INDEX</a></li>
                        <li>CH10</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>

    <section class="about-us section">
        <div class="container">
            <div class="row align-items-center justify-content-center">
                <div class="col-lg-12 col-md-12 col-12">
                    <div class="content-left wow fadeInLeft" data-wow-delay=".3s">
						<h3>인증 실패했습니다!</h3>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <%@ include file="/pageModule/footer.jsp" %>

    <%@ include file="/pageModule/footerPart.jsp" %>
</body>

</html>

[web.xml]

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>JSPBook</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- ch10 기본 인증 처리 방법으로 보안 처리하기 -->
  <!-- <security-role>
  	<role-name>role1</role-name>
  </security-role>
  <security-constraint>
  	<web-resource-collection>
  		<web-resource-name>JSPBookProject</web-resource-name>
  		<url-pattern>/ch10/security01.jsp</url-pattern>
  		<http-method>GET</http-method>
  	</web-resource-collection>
  	<auth-constraint>
  		<description></description>
  		<role-name>role1</role-name>
  	</auth-constraint>
  </security-constraint>
  <login-config>
  	<auth-method>BASIC</auth-method>
  </login-config> -->
  <!-- ch10 기본 인증 처리 방법으로 보안 처리하기 End -->
  
  <!-- ch10 폼 기반 인증 처리 방법으로 보안 처리하기 -->
  <security-role>
  	<role-name>role1</role-name>
  </security-role>
  <security-constraint>
  	<web-resource-collection>
  		<web-resource-name>JSPBookProject</web-resource-name>
  		<url-pattern>/ch10/security01.jsp</url-pattern>
  		<http-method>GET</http-method>
  	</web-resource-collection>
  	<auth-constraint>
  		<description></description>
  		<role-name>role1</role-name>
  	</auth-constraint>
  </security-constraint>
  <login-config>
  	<auth-method>FORM</auth-method>
  	<form-login-config>
  		<form-login-page>/ch10/login.jsp</form-login-page>
  		<form-error-page>/ch10/login_failed.jsp</form-error-page>
  	</form-login-config>
  </login-config>
  <!-- ch10 폼 기반 인증 처리 방법으로 보안 처리하기 End -->
  
</web-app>

- http://localhost/ch10/security01.jsp



getUserPrinciple() 이 가장 중요한 메서드. 계정 정보가 여기에 담김.

[web.xml]

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>JSPBook</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- ch10 기본 인증 처리 방법으로 보안 처리하기 -->
  <!-- <security-role>
  	<role-name>role1</role-name>
  </security-role>
  <security-constraint>
  	<web-resource-collection>
  		<web-resource-name>JSPBookProject</web-resource-name>
  		<url-pattern>/ch10/security01.jsp</url-pattern>
  		<http-method>GET</http-method>
  	</web-resource-collection>
  	<auth-constraint>
  		<description></description>
  		<role-name>role1</role-name>
  	</auth-constraint>
  </security-constraint>
  <login-config>
  	<auth-method>BASIC</auth-method>
  </login-config> -->
  <!-- ch10 기본 인증 처리 방법으로 보안 처리하기 End -->
  
  <!-- ch10 폼 기반 인증 처리 방법으로 보안 처리하기 -->
  <!-- <security-role>
  	<role-name>role1</role-name>
  </security-role>
  <security-constraint>
  	<web-resource-collection>
  		<web-resource-name>JSPBookProject</web-resource-name>
  		<url-pattern>/ch10/security01.jsp</url-pattern>
  		<http-method>GET</http-method>
  	</web-resource-collection>
  	<auth-constraint>
  		<description></description>
  		<role-name>role1</role-name>
  	</auth-constraint>
  </security-constraint>
  <login-config>
  	<auth-method>FORM</auth-method>
  	<form-login-config>
  		<form-login-page>/ch10/login.jsp</form-login-page>
  		<form-error-page>/ch10/login_failed.jsp</form-error-page>
  	</form-login-config>
  </login-config> -->
  <!-- ch10 폼 기반 인증 처리 방법으로 보안 처리하기 End -->
  
  <!-- ch10 프로그래밍적 시큐리티 처리 -->
  <security-role>
  	<role-name>role1</role-name>
  </security-role>
  <security-constraint>
  	<web-resource-collection>
  		<web-resource-name>JSPBookProject</web-resource-name>
  		<url-pattern>/ch10/security02.jsp</url-pattern>
  		<http-method>GET</http-method>
  	</web-resource-collection>
  	<auth-constraint>
  		<description></description>
  		<role-name>role1</role-name>
  	</auth-constraint>
  </security-constraint>
  <login-config>
  	<auth-method>FORM</auth-method>
  	<form-login-config>
  		<form-login-page>/ch10/login.jsp</form-login-page>
  		<form-error-page>/ch10/login_failed.jsp</form-error-page>
  	</form-login-config>
  </login-config>
  <!-- ch10 프로그래밍적 시큐리티 처리 End -->
  
</web-app>

[security02.jsp]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html class="no-js" lang="zxx">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="x-ua-compatible" content="ie=edge" />
    <title>쉽게 배우는 JSP 웹 프로그래밍</title>
    <meta name="description" content="" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <%@ include file="/pageModule/headPart.jsp" %>
</head>

<body>
    <%@ include file="/pageModule/header.jsp" %>

    <div class="breadcrumbs" style="padding-top:40px;">
        <div class="container">
            <div class="row align-items-center">
                <div class="col-lg-6 col-md-6 col-12">
                    <div class="breadcrumbs-content">
                        <h1 class="page-title">시큐리티</h1>
                    </div>
                </div>
                <div class="col-lg-6 col-md-6 col-12">
                    <ul class="breadcrumb-nav">
                        <li><a href="/">INDEX</a></li>
                        <li>CH10</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>

    <section class="about-us section">
        <div class="container">
            <div class="row align-items-center justify-content-center">
                <div class="col-lg-12 col-md-12 col-12">
                    <div class="content-left wow fadeInLeft" data-wow-delay=".3s">
						<h4>결과 확인</h4>
						<hr /><br />
						
						<!-- 인증된 사용자를 가져오도록 request 내장 객체의 getRomoteUser() 함수를 사용 -->
						<p>사용자명 : <%= request.getRemoteUser() %></p>
						<!-- 인증 처리 방식을 가져오도록 request 내장 객체의 getAuthType() 함수를 사용 -->
						<p>인증방법 : <%= request.getAuthType() %></p>
						
						<!-- 
							로그인 시 인증한 사용자의 역할 이름이 tomcat인지, role1인지 확인하도록 request 내장 객체의 
							isUserInRole() 함수 사용
						-->
						<p>
							인증한 사용자명이 역할명 'tomcat'에 속하는 사용자인가요? <br />
							<%= request.isUserInRole("tomcat") %>
						</p>
						<p>
							인증한 사용자명이 역할명 'role1'에 속하는 사용자인가요? <br />
							<%= request.isUserInRole("role1") %>
						</p>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <%@ include file="/pageModule/footer.jsp" %>

    <%@ include file="/pageModule/footerPart.jsp" %>
</body>

</html>

- http://localhost/ch10/security02.jsp


[login.jsp]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html class="no-js" lang="zxx">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="x-ua-compatible" content="ie=edge" />
    <title>쉽게 배우는 JSP 웹 프로그래밍</title>
    <meta name="description" content="" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <%@ include file="/pageModule/headPart.jsp" %>
</head>

<body>
    <%@ include file="/pageModule/header.jsp" %>

    <div class="breadcrumbs" style="padding-top:40px;">
        <div class="container">
            <div class="row align-items-center">
                <div class="col-lg-6 col-md-6 col-12">
                    <div class="breadcrumbs-content">
                        <h1 class="page-title">시큐리티</h1>
                    </div>
                </div>
                <div class="col-lg-6 col-md-6 col-12">
                    <ul class="breadcrumb-nav">
                        <li><a href="/">INDEX</a></li>
                        <li>CH10</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>

    <section class="about-us section">
        <div class="container">
            <div class="row align-items-center justify-content-center">
                <div class="col-lg-12 col-md-12 col-12">
                    <div class="content-left wow fadeInLeft" data-wow-delay=".3s">
                    	<!--  
                    		tomcat-users.xml 에서 설정한 역할당 계정들 중, 인증을 진행할 역할과 동일한 계정으로 인증 시
                    		정상적인 요청 및 인증만으로 로그인이 진행될 수 있다. 하지만, 인증을 진행할 역할과 다른 역할의
                    		계정으로 인증 시, 403 Forbidden 에서로 '엑세스 거부'가 출력된다.
                    		
                    		로그인 인증 처리를 위한 form 태그 작성
                    		- 폼 기반 인증을 처리하도록 action 속성값을 j_security_check로 작성
                    		- 아이디 입력 양식 name 속성 값을 j_username으로 작성
                    		- 비밀번호 입력 양식 name 속성 값을 j_password으로 작성
                    	-->
                    
						<form action="j_security_check" method="post">
							아이디 : <input type="text" name="j_username" /><br />
							비밀번호 : <input type="text" name="j_password" /><br />
							<button type="submit">전송</button>
						</form>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <%@ include file="/pageModule/footer.jsp" %>

    <%@ include file="/pageModule/footerPart.jsp" %>
</body>

</html>

[10장_web_comment.xml]

<web-app>

	<!--  
		JSP 시큐리티 web.xml 설정
		
		1. 시큐리티 역할 설정하는 방법
			<security-role>
				<role-name>role1</role-name> // 역할 이름
			</security-role>
			
		2. 시큐리티 제약사항 설정하기
		<security-constraint>
		  	<web-resource-collection></web-resource-collection> // 웹 자원에 대한 접근 설정
		  	<auth-constraint></auth-constraint> // 웹 자원에 접근할 수 있는 인증된 사용자 설정
		  	<user-data-constraint></user-data-constraint> // 데이터 전송 시 데이터 보호를 설정
		</security-constraint>
		
			2.1 웹 자원에 대한 접근 설정
				<web-resource-collection>
			  		<web-resource-name>JSPBookProject</web-resource-name> // 웹 자원의 이름을 설정하며 생략할 수 있다.
			  		<url-pattern>/ch10/security02.jsp</url-pattern> // 접근 제한을 요청할 때 URL 목록을 설정
			  		<http-method>GET</http-method> // HTTP 메소드를 설정(GET, POST)
			  	</web-resource-collection>
			  	
			2.2 웹 자원에 접근할 수 있는 인증된 사용자 설정
				- auth-constraint를 설정하지 않으면 웹 서버는 사용자 인증을 요구하지 않고 사용자의 요청을 승인한다.
				<auth-constraint>
			  		<description></description> // 설명
			  		<role-name>role1</role-name> // 반드시 tomcat-users.xml에 등록된 역할과 사용자여야 한다.
			  	</auth-constraint>
			  	
			2.3 클라이언트와 서버 간에 데이터를 전송할 때 데이터를 보호하는 방법을 설정
				- NONE : 기본 값으로 데이터를 보호하지 않을 때
				- INTEGRAL : 전송 중 데이터가 변경되지 않았음을 보장(데이터 무결성)
				- CONFIDENTIAL : 전송 중 데이터를 아무도 훔쳐보지 않았음을 보장(데이터 기밀성)
				<user-data-constraint>
					<transport-guarantee>NONE</transport-guarantee>
				</user-data-constraint>
				
		3. 시큐리티 인증 설정하기
			<login-config>
			  	<auth-method>FORM</auth-method> // 웹 자원에 대한 인증 처리 방식 설정
			  	<realm-name></realm-name> // 웹 자원에 접근할 수 있는 인증된 사용자 설정
			  	<form-login-config></form-login-config> // 데이터 전송 시 데이터 보호를 설정
			</login-config>
			
			3-1 웹 애플리케이션의 인증 처리 기법을 설정하는 요소
				- BASIC : 웹 자원을 보호하는 간단하고 일반적인 방법
				- DIGEST : 암호화 메커니즘을 이용하여 전송 (많이 사용되지 않음)
				- FORM : [대표적] 일반적인 폼 페이지를 이용하여 로그인 정보를 서버에 전송하는 방식
						암호화 되지 않은 로그인 정보를 그대로 전송
						*** FORM 기반 인증시에는 별도 form 처리가 필요하다
				- CLIENT-CERT : 클라이언트가 인증서를 가지고 공인 키 인증 방식을 사용하여 로그인하는 방식
								클라이언트가 인증서를 가지고 있어야만 로그인되므로 비즈니스 환경에서만 적용
				<auth-method>FORM</auth-method>
				
			3-2 기본 인증의 영역 이름을 설정
				- FORM 기반 인증이나 다른 인증 방법에 필요하지 않기 때문에 아무런 영향을 미치지 않지만, 데이터를 문서화하는데 일반적으로 사용
				<realm-name>영역 이름</realm-name>
				
			3-3 인증 처리를 위한 로그인 및 오류 페이지를 설정
				- auth-method 요소가 FORM 기반 인증 처리 기법으로 설정되었을 때 사용
				<form-login-config>
					<form-login-page>/ch10/login.jsp</form-login-page>
  					<form-error-page>/ch10/login_failed.jsp</form-error-page>
				</form-login-config>
	-->
	
</web-app>

[tomcat-users.xml]

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
--><tomcat-users version="1.0" xmlns="http://tomcat.apache.org/xml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd">
<user password="admin" roles="manager-gui" username="admin"/>
<!--
  By default, no user is included in the "manager-gui" role required
  to operate the "/manager/html" web application.  If you wish to use this app,
  you must define such a user - the username and password are arbitrary.

  Built-in Tomcat manager roles:
    - manager-gui    - allows access to the HTML GUI and the status pages
    - manager-script - allows access to the HTTP API and the status pages
    - manager-jmx    - allows access to the JMX proxy and the status pages
    - manager-status - allows access to the status pages only

  The users below are wrapped in a comment and are therefore ignored. If you
  wish to configure one or more of these users for use with the manager web
  application, do not forget to remove the <!.. ..> that surrounds them. You
  will also need to set the passwords to something appropriate.
-->
<!--
  <user username="admin" password="<must-be-changed>" roles="manager-gui"/>
  <user username="robot" password="<must-be-changed>" roles="manager-script"/>
-->
<!--
  The sample user and role entries below are intended for use with the
  examples web application. They are wrapped in a comment and thus are ignored
  when reading this file. If you wish to configure these users for use with the
  examples web application, do not forget to remove the <!.. ..> that surrounds
  them. You will also need to set the passwords to something appropriate.
-->
<!--
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <user username="tomcat" password="<must-be-changed>" roles="tomcat"/>
  <user username="both" password="<must-be-changed>" roles="tomcat,role1"/>
  <user username="role1" password="<must-be-changed>" roles="role1"/>
-->
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <role rolename="admin"/>
  <user username="tomcat" password="1234" roles="tomcat"/>
  <user username="both" password="1234" roles="tomcat,role1"/>
  <user username="role1" password="1234" roles="role1"/>
  <user username="admin" password="1234" roles="admin"/>
</tomcat-users>

[web.xml]

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>WebMarket</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <security-role>
  	<role-name>admin</role-name>
  </security-role>
  <security-constraint>
  	<display-name>WebMarket Security</display-name>
  	<web-resource-collection>
  		<web-resource-name>WebMarket</web-resource-name>
  		<description></description>
  		<url-pattern>/addProduct.jsp</url-pattern>
  	</web-resource-collection>
  	<auth-constraint>
  		<description>권한 관리자명</description>
  		<role-name>admin</role-name>
  	</auth-constraint>
  </security-constraint>
  <login-config>
  	<auth-method>FORM</auth-method>
	<form-login-config>
		<form-login-page>/login.jsp</form-login-page>
		<form-error-page>/login_failed.jsp</form-error-page>
	</form-login-config>
  </login-config>
</web-app>

[login.jsp]

<%@ page contentType="text/html; charset=utf-8"%>
<html>
<head>
<link rel="stylesheet" href="${pageContext.request.contextPath }/resources/css/bootstrap.min.css" />
<title>Login</title>
</head>
<body>
	<%@ include file="menu.jsp" %>
	<div class="jumbotron">
		<div class="container">
			<h1 class="display-3">로그인</h1>
		</div>
	</div>
	<div class="container" align="center">
		<div class="col-md-4 col-md-offset-4">
			<h3 class="form-signin-heading">Please sign in</h3>
			<% 
				String error = request.getParameter("error");
				if(error != null) {
					out.println("<div class='alert alert-danger'>");
					out.println("아이디와 비밀번호를 확인해 주세요!");
					out.println("</div>");
				}
			%>
			<form class="form-signin" action="j_security_check" method="post">
				<div class="form-group">
					<label for="inputUserName" class="sr-only">User Name</label> 
					<input	type="text" class="form-control" placeholder="ID" name='j_username' required autofocus>
				</div>
				<div class="form-group">
					<label for="inputPassword" class="sr-only">Password</label> 
					<input 	type="password" class="form-control" placeholder="Password" name='j_password' required>
				</div>
				<button class="btn btn btn-lg btn-success btn-block" type="submit">로그인</button>
			</form>
		</div>
	</div>
</body>
</html>

[login_failed.jsp]

<%
	response.sendRedirect("login.jsp?error=1");
%>

[menu.jsp]

<%@ page language="java" pageEncoding="UTF-8"%>
		<nav class="navbar navbar-expand navbar-dark bg-dark">
			<div class="container">
				<div class="navbar-header">
					<a class="navbar-brand" href="./welcome.jsp">Home</a>
				</div>
			</div>
		</nav>

- http://localhost/addProduct.jsp


[errorPage.jsp]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" 
    errorPage="errorPage_error.jsp"%>
<!DOCTYPE html>
<html class="no-js" lang="zxx">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="x-ua-compatible" content="ie=edge" />
    <title>쉽게 배우는 JSP 웹 프로그래밍</title>
    <meta name="description" content="" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <%@ include file="/pageModule/headPart.jsp" %>
</head>

<body>
    <%@ include file="/pageModule/header.jsp" %>

    <div class="breadcrumbs" style="padding-top:40px;">
        <div class="container">
            <div class="row align-items-center">
                <div class="col-lg-6 col-md-6 col-12">
                    <div class="breadcrumbs-content">
                        <h1 class="page-title">예외 처리</h1>
                    </div>
                </div>
                <div class="col-lg-6 col-md-6 col-12">
                    <ul class="breadcrumb-nav">
                        <li><a href="/">INDEX</a></li>
                        <li>CH11</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>

    <section class="about-us section">
        <div class="container">
            <div class="row align-items-center justify-content-center">
                <div class="col-lg-12 col-md-12 col-12">
                    <div class="content-left wow fadeInLeft" data-wow-delay=".3s">
                    	<!--  
                    		JSP 페이지가 실행되는 도중에 오류가 발생하면 오류 페이지를 호출하도록 page 디렉티브 태그에
                    		errorPage 속성을 작성한다.
                    	-->
						name 파라미터 : <%= request.getParameter("name").toUpperCase() %>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <%@ include file="/pageModule/footer.jsp" %>

    <%@ include file="/pageModule/footerPart.jsp" %>
</body>

</html>

[errorPage_error.jsp]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html class="no-js" lang="zxx">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="x-ua-compatible" content="ie=edge" />
    <title>쉽게 배우는 JSP 웹 프로그래밍</title>
    <meta name="description" content="" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <%@ include file="/pageModule/headPart.jsp" %>
</head>

<body>
    <%@ include file="/pageModule/header.jsp" %>

    <div class="breadcrumbs" style="padding-top:40px;">
        <div class="container">
            <div class="row align-items-center">
                <div class="col-lg-6 col-md-6 col-12">
                    <div class="breadcrumbs-content">
                        <h1 class="page-title">예외 처리</h1>
                    </div>
                </div>
                <div class="col-lg-6 col-md-6 col-12">
                    <ul class="breadcrumb-nav">
                        <li><a href="/">INDEX</a></li>
                        <li>CH11</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>

    <section class="about-us section">
        <div class="container">
            <div class="row align-items-center justify-content-center">
                <div class="col-lg-12 col-md-12 col-12">
                    <div class="content-left wow fadeInLeft" data-wow-delay=".3s">
						<h3>오류가 발생했습니다!</h3>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <%@ include file="/pageModule/footer.jsp" %>

    <%@ include file="/pageModule/footerPart.jsp" %>
</body>

</html>

- http://localhost/ch11/errorPage.jsp


[isErrorPage.jsp]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" 
    errorPage="isErrorPage_error.jsp"%>
<!DOCTYPE html>
<html class="no-js" lang="zxx">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="x-ua-compatible" content="ie=edge" />
    <title>쉽게 배우는 JSP 웹 프로그래밍</title>
    <meta name="description" content="" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <%@ include file="/pageModule/headPart.jsp" %>
</head>

<body>
    <%@ include file="/pageModule/header.jsp" %>

    <div class="breadcrumbs" style="padding-top:40px;">
        <div class="container">
            <div class="row align-items-center">
                <div class="col-lg-6 col-md-6 col-12">
                    <div class="breadcrumbs-content">
                        <h1 class="page-title">예외 처리</h1>
                    </div>
                </div>
                <div class="col-lg-6 col-md-6 col-12">
                    <ul class="breadcrumb-nav">
                        <li><a href="/">INDEX</a></li>
                        <li>CH11</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>

    <section class="about-us section">
        <div class="container">
            <div class="row align-items-center justify-content-center">
                <div class="col-lg-12 col-md-12 col-12">
                    <div class="content-left wow fadeInLeft" data-wow-delay=".3s">
                    	<!--  
                    		JSP 페이지가 실행되는 도중에 오류가 발생하면 오류 페이지를 호출하도록 page 디렉티브 태그에
                    		errorPage 속성을 작성한다.
                    	-->
						name 파라미터 : <%= request.getParameter("name").toUpperCase() %>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <%@ include file="/pageModule/footer.jsp" %>

    <%@ include file="/pageModule/footerPart.jsp" %>
</body>

</html>

[isErrorPage_error.jsp]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" 
    isErrorPage="true"%>
<!DOCTYPE html>
<html class="no-js" lang="zxx">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="x-ua-compatible" content="ie=edge" />
    <title>쉽게 배우는 JSP 웹 프로그래밍</title>
    <meta name="description" content="" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <%@ include file="/pageModule/headPart.jsp" %>
</head>

<body>
    <%@ include file="/pageModule/header.jsp" %>

    <div class="breadcrumbs" style="padding-top:40px;">
        <div class="container">
            <div class="row align-items-center">
                <div class="col-lg-6 col-md-6 col-12">
                    <div class="breadcrumbs-content">
                        <h1 class="page-title">예외 처리</h1>
                    </div>
                </div>
                <div class="col-lg-6 col-md-6 col-12">
                    <ul class="breadcrumb-nav">
                        <li><a href="/">INDEX</a></li>
                        <li>CH11</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>

    <section class="about-us section">
        <div class="container">
            <div class="row align-items-center justify-content-center">
                <div class="col-lg-12 col-md-12 col-12">
                    <div class="content-left wow fadeInLeft" data-wow-delay=".3s">
                    	<!--  
                    		현재 JSP 페이지가 오류 페이지가 되도록 page 디렉티브 태그에 isErrorPage 속성을 작성한다.
                    		오류가 발생했을 때 해당 예외 객체 타입을 가져와 출력하도록 표현문 태그에 exception 내장 객체의
                    		함수를 이용하여 작성한다.
                    	-->
                    	<h3>오류가 발생!!</h3>
                    	<p>예외 유형 : <%= exception.getClass().getName() %></p>
                    	<p>오류 메세지 : <%= exception.getMessage() %></p>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <%@ include file="/pageModule/footer.jsp" %>

    <%@ include file="/pageModule/footerPart.jsp" %>
</body>

</html>

- http://localhost/ch11/isErrorPage.jsp


[exception.jsp]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html class="no-js" lang="zxx">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="x-ua-compatible" content="ie=edge" />
    <title>쉽게 배우는 JSP 웹 프로그래밍</title>
    <meta name="description" content="" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <%@ include file="/pageModule/headPart.jsp" %>
</head>

<body>
    <%@ include file="/pageModule/header.jsp" %>

    <div class="breadcrumbs" style="padding-top:40px;">
        <div class="container">
            <div class="row align-items-center">
                <div class="col-lg-6 col-md-6 col-12">
                    <div class="breadcrumbs-content">
                        <h1 class="page-title">예외 처리</h1>
                    </div>
                </div>
                <div class="col-lg-6 col-md-6 col-12">
                    <ul class="breadcrumb-nav">
                        <li><a href="/">INDEX</a></li>
                        <li>CH11</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>

    <section class="about-us section">
        <div class="container">
            <div class="row align-items-center justify-content-center">
                <div class="col-lg-12 col-md-12 col-12">
                    <div class="content-left wow fadeInLeft" data-wow-delay=".3s">
                    	<form action="exception_process.jsp" method="post">
                    		숫자 1 : <input type="text" name="num1" /><br />
                    		숫자 2 : <input type="text" name="num2" /><br />
                    		<button type="submit">나누기</button>
                    	</form>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <%@ include file="/pageModule/footer.jsp" %>

    <%@ include file="/pageModule/footerPart.jsp" %>
</body>

</html>

[exception_process.jsp]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" 
    errorPage="exception_error.jsp"%>
<!DOCTYPE html>
<html class="no-js" lang="zxx">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="x-ua-compatible" content="ie=edge" />
    <title>쉽게 배우는 JSP 웹 프로그래밍</title>
    <meta name="description" content="" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <%@ include file="/pageModule/headPart.jsp" %>
</head>

<body>
    <%@ include file="/pageModule/header.jsp" %>

    <div class="breadcrumbs" style="padding-top:40px;">
        <div class="container">
            <div class="row align-items-center">
                <div class="col-lg-6 col-md-6 col-12">
                    <div class="breadcrumbs-content">
                        <h1 class="page-title">예외 처리</h1>
                    </div>
                </div>
                <div class="col-lg-6 col-md-6 col-12">
                    <ul class="breadcrumb-nav">
                        <li><a href="/">INDEX</a></li>
                        <li>CH11</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>

    <section class="about-us section">
        <div class="container">
            <div class="row align-items-center justify-content-center">
                <div class="col-lg-12 col-md-12 col-12">
                    <div class="content-left wow fadeInLeft" data-wow-delay=".3s">
                    	<% 
                    		String num1 = request.getParameter("num1");
                    		String num2 = request.getParameter("num2");
                    		
                    		int a = Integer.parseInt(num1);
                    		int b = Integer.parseInt(num2);
                    		
                    		int c = a / b;
                    		out.println(num1 + " / " + num2 + " = " + c);
                    	%>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <%@ include file="/pageModule/footer.jsp" %>

    <%@ include file="/pageModule/footerPart.jsp" %>
</body>

</html>

[exception_error.jsp]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" 
    isErrorPage="true"%>
<!DOCTYPE html>
<html class="no-js" lang="zxx">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="x-ua-compatible" content="ie=edge" />
    <title>쉽게 배우는 JSP 웹 프로그래밍</title>
    <meta name="description" content="" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <%@ include file="/pageModule/headPart.jsp" %>
</head>

<body>
    <%@ include file="/pageModule/header.jsp" %>

    <div class="breadcrumbs" style="padding-top:40px;">
        <div class="container">
            <div class="row align-items-center">
                <div class="col-lg-6 col-md-6 col-12">
                    <div class="breadcrumbs-content">
                        <h1 class="page-title">예외 처리</h1>
                    </div>
                </div>
                <div class="col-lg-6 col-md-6 col-12">
                    <ul class="breadcrumb-nav">
                        <li><a href="/">INDEX</a></li>
                        <li>CH11</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>

    <section class="about-us section">
        <div class="container">
            <div class="row align-items-center justify-content-center">
                <div class="col-lg-12 col-md-12 col-12">
                    <div class="content-left wow fadeInLeft" data-wow-delay=".3s">
                    	<h4>오류가 발생했습니다!</h4>
                    	<p>예외 : <%= exception %></p>
                    	<p>toString() : <%= exception.toString() %></p>
                    	<p>getClass() : <%= exception.getClass() %></p>
                    	<p>getMessage() : <%= exception.getMessage() %></p>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <%@ include file="/pageModule/footer.jsp" %>

    <%@ include file="/pageModule/footerPart.jsp" %>
</body>

</html>

- http://localhost/ch11/exception.jsp


* 400 에러 발생하는 이유?
   (1) 데이터 누락 : 서버로 보내야할 데이터가 5개인데 3개면 받는 개수와 보내는 개수가 다르기 때문에 나타남

   (2) 타입이 다를 때 : 클라이언트로 데이터를 보낼 때 보내는 타입과 받는 타입이 다른 경우

* 데이터를 주고받는 과정에서 400 에러가 뜨는 경우는 출발지와 목적지에 따른 설정의 에러는 에러 검증을 다르게 해야 함. 클라이언트의 도착지 설정을 확인하고, 서버쪽에서도 도착지 설정을 확인해야 한다.

* 디버깅 스킬 늘려야 하고, 각각의 에러가 왜 났는지에 대한 범위를 좁혀나가야 한다.


[errorCode.jsp]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html class="no-js" lang="zxx">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="x-ua-compatible" content="ie=edge" />
    <title>쉽게 배우는 JSP 웹 프로그래밍</title>
    <meta name="description" content="" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <%@ include file="/pageModule/headPart.jsp" %>
</head>

<body>
    <%@ include file="/pageModule/header.jsp" %>

    <div class="breadcrumbs" style="padding-top:40px;">
        <div class="container">
            <div class="row align-items-center">
                <div class="col-lg-6 col-md-6 col-12">
                    <div class="breadcrumbs-content">
                        <h1 class="page-title">예외 처리</h1>
                    </div>
                </div>
                <div class="col-lg-6 col-md-6 col-12">
                    <ul class="breadcrumb-nav">
                        <li><a href="/">INDEX</a></li>
                        <li>CH11</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>

    <section class="about-us section">
        <div class="container">
            <div class="row align-items-center justify-content-center">
                <div class="col-lg-12 col-md-12 col-12">
                    <div class="content-left wow fadeInLeft" data-wow-delay=".3s">
                    	<form action="errorCode_process.jsp" method="post">
                    		숫자 1 : <input type="text" name="num1" /><br />
                    		숫자 2 : <input type="text" name="num2" /><br />
                    		<button type="submit">나누기</button>
                    	</form>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <%@ include file="/pageModule/footer.jsp" %>

    <%@ include file="/pageModule/footerPart.jsp" %>
</body>

</html>

[errorCode_process.jsp]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html class="no-js" lang="zxx">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="x-ua-compatible" content="ie=edge" />
    <title>쉽게 배우는 JSP 웹 프로그래밍</title>
    <meta name="description" content="" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <%@ include file="/pageModule/headPart.jsp" %>
</head>

<body>
    <%@ include file="/pageModule/header.jsp" %>

    <div class="breadcrumbs" style="padding-top:40px;">
        <div class="container">
            <div class="row align-items-center">
                <div class="col-lg-6 col-md-6 col-12">
                    <div class="breadcrumbs-content">
                        <h1 class="page-title">예외 처리</h1>
                    </div>
                </div>
                <div class="col-lg-6 col-md-6 col-12">
                    <ul class="breadcrumb-nav">
                        <li><a href="/">INDEX</a></li>
                        <li>CH11</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>

    <section class="about-us section">
        <div class="container">
            <div class="row align-items-center justify-content-center">
                <div class="col-lg-12 col-md-12 col-12">
                    <div class="content-left wow fadeInLeft" data-wow-delay=".3s">
                    	<% 
                    		String num1 = request.getParameter("num1");
                    		String num2 = request.getParameter("num2");
                    		
                    		int a = Integer.parseInt(num1);
                    		int b = Integer.parseInt(num2);
                    		
                    		int c = a / b;
                    		out.println(num1 + " / " + num2 + " = " + c);
                    	%>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <%@ include file="/pageModule/footer.jsp" %>

    <%@ include file="/pageModule/footerPart.jsp" %>
</body>

</html>

[errorCode_error.jsp]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html class="no-js" lang="zxx">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="x-ua-compatible" content="ie=edge" />
    <title>쉽게 배우는 JSP 웹 프로그래밍</title>
    <meta name="description" content="" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <%@ include file="/pageModule/headPart.jsp" %>
</head>

<body>
    <%@ include file="/pageModule/header.jsp" %>

    <div class="breadcrumbs" style="padding-top:40px;">
        <div class="container">
            <div class="row align-items-center">
                <div class="col-lg-6 col-md-6 col-12">
                    <div class="breadcrumbs-content">
                        <h1 class="page-title">예외 처리</h1>
                    </div>
                </div>
                <div class="col-lg-6 col-md-6 col-12">
                    <ul class="breadcrumb-nav">
                        <li><a href="/">INDEX</a></li>
                        <li>CH11</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>

    <section class="about-us section">
        <div class="container">
            <div class="row align-items-center justify-content-center">
                <div class="col-lg-12 col-md-12 col-12">
                    <div class="content-left wow fadeInLeft" data-wow-delay=".3s">
                    	<h3>errorCode 500 오류가 발생하였습니다!</h3>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <%@ include file="/pageModule/footer.jsp" %>

    <%@ include file="/pageModule/footerPart.jsp" %>
</body>

</html>

[web.xml]

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>JSPBook</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- ch10 기본 인증 처리 방법으로 보안 처리하기 -->
  <!-- <security-role>
  	<role-name>role1</role-name>
  </security-role>
  <security-constraint>
  	<web-resource-collection>
  		<web-resource-name>JSPBookProject</web-resource-name>
  		<url-pattern>/ch10/security01.jsp</url-pattern>
  		<http-method>GET</http-method>
  	</web-resource-collection>
  	<auth-constraint>
  		<description></description>
  		<role-name>role1</role-name>
  	</auth-constraint>
  </security-constraint>
  <login-config>
  	<auth-method>BASIC</auth-method>
  </login-config> -->
  <!-- ch10 기본 인증 처리 방법으로 보안 처리하기 End -->
  
  <!-- ch10 폼 기반 인증 처리 방법으로 보안 처리하기 -->
  <!-- <security-role>
  	<role-name>role1</role-name>
  </security-role>
  <security-constraint>
  	<web-resource-collection>
  		<web-resource-name>JSPBookProject</web-resource-name>
  		<url-pattern>/ch10/security01.jsp</url-pattern>
  		<http-method>GET</http-method>
  	</web-resource-collection>
  	<auth-constraint>
  		<description></description>
  		<role-name>role1</role-name>
  	</auth-constraint>
  </security-constraint>
  <login-config>
  	<auth-method>FORM</auth-method>
  	<form-login-config>
  		<form-login-page>/ch10/login.jsp</form-login-page>
  		<form-error-page>/ch10/login_failed.jsp</form-error-page>
  	</form-login-config>
  </login-config> -->
  <!-- ch10 폼 기반 인증 처리 방법으로 보안 처리하기 End -->
  
  <!-- ch10 프로그래밍적 시큐리티 처리 -->
  <security-role>
  	<role-name>role1</role-name>
  </security-role>
  <security-constraint>
  	<web-resource-collection>
  		<web-resource-name>JSPBookProject</web-resource-name>
  		<url-pattern>/ch10/security02.jsp</url-pattern>
  		<http-method>GET</http-method>
  	</web-resource-collection>
  	<auth-constraint>
  		<description></description>
  		<role-name>role1</role-name>
  	</auth-constraint>
  </security-constraint>
  <login-config>
  	<auth-method>FORM</auth-method>
  	<form-login-config>
  		<form-login-page>/ch10/login.jsp</form-login-page>
  		<form-error-page>/ch10/login_failed.jsp</form-error-page>
  	</form-login-config>
  </login-config>
  <!-- ch10 프로그래밍적 시큐리티 처리 End -->
  
  <error-page>
  	<error-code>500</error-code>
  	<location>/ch11/errorCode_error.jsp</location>
  </error-page>
  
</web-app>

- http://localhost/ch11/errorCode_process.jsp


[exceptionType.jsp]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html class="no-js" lang="zxx">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="x-ua-compatible" content="ie=edge" />
    <title>쉽게 배우는 JSP 웹 프로그래밍</title>
    <meta name="description" content="" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <%@ include file="/pageModule/headPart.jsp" %>
</head>

<body>
    <%@ include file="/pageModule/header.jsp" %>

    <div class="breadcrumbs" style="padding-top:40px;">
        <div class="container">
            <div class="row align-items-center">
                <div class="col-lg-6 col-md-6 col-12">
                    <div class="breadcrumbs-content">
                        <h1 class="page-title">예외 처리</h1>
                    </div>
                </div>
                <div class="col-lg-6 col-md-6 col-12">
                    <ul class="breadcrumb-nav">
                        <li><a href="/">INDEX</a></li>
                        <li>CH11</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>

    <section class="about-us section">
        <div class="container">
            <div class="row align-items-center justify-content-center">
                <div class="col-lg-12 col-md-12 col-12">
                    <div class="content-left wow fadeInLeft" data-wow-delay=".3s">
                    	<form action="exceptionType_process.jsp" method="post">
                    		숫자 1 : <input type="text" name="num1" /><br />
                    		숫자 2 : <input type="text" name="num2" /><br />
                    		<button type="submit">나누기</button>
                    	</form>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <%@ include file="/pageModule/footer.jsp" %>

    <%@ include file="/pageModule/footerPart.jsp" %>
</body>

</html>

[exceptionType_process.jsp]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html class="no-js" lang="zxx">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="x-ua-compatible" content="ie=edge" />
    <title>쉽게 배우는 JSP 웹 프로그래밍</title>
    <meta name="description" content="" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <%@ include file="/pageModule/headPart.jsp" %>
</head>

<body>
    <%@ include file="/pageModule/header.jsp" %>

    <div class="breadcrumbs" style="padding-top:40px;">
        <div class="container">
            <div class="row align-items-center">
                <div class="col-lg-6 col-md-6 col-12">
                    <div class="breadcrumbs-content">
                        <h1 class="page-title">예외 처리</h1>
                    </div>
                </div>
                <div class="col-lg-6 col-md-6 col-12">
                    <ul class="breadcrumb-nav">
                        <li><a href="/">INDEX</a></li>
                        <li>CH11</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>

    <section class="about-us section">
        <div class="container">
            <div class="row align-items-center justify-content-center">
                <div class="col-lg-12 col-md-12 col-12">
                    <div class="content-left wow fadeInLeft" data-wow-delay=".3s">
                    	<% 
                    		String num1 = request.getParameter("num1");
                    		String num2 = request.getParameter("num2");
                    		
                    		int a = Integer.parseInt(num1);
                    		int b = Integer.parseInt(num2);
                    		
                    		int c = a / b;
                    		out.println(num1 + " / " + num2 + " = " + c);
                    	%>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <%@ include file="/pageModule/footer.jsp" %>

    <%@ include file="/pageModule/footerPart.jsp" %>
</body>

</html>

[exceptionType_error.jsp]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html class="no-js" lang="zxx">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="x-ua-compatible" content="ie=edge" />
    <title>쉽게 배우는 JSP 웹 프로그래밍</title>
    <meta name="description" content="" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <%@ include file="/pageModule/headPart.jsp" %>
</head>

<body>
    <%@ include file="/pageModule/header.jsp" %>

    <div class="breadcrumbs" style="padding-top:40px;">
        <div class="container">
            <div class="row align-items-center">
                <div class="col-lg-6 col-md-6 col-12">
                    <div class="breadcrumbs-content">
                        <h1 class="page-title">예외 처리</h1>
                    </div>
                </div>
                <div class="col-lg-6 col-md-6 col-12">
                    <ul class="breadcrumb-nav">
                        <li><a href="/">INDEX</a></li>
                        <li>CH11</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>

    <section class="about-us section">
        <div class="container">
            <div class="row align-items-center justify-content-center">
                <div class="col-lg-12 col-md-12 col-12">
                    <div class="content-left wow fadeInLeft" data-wow-delay=".3s">
                    	<h3>exceptionType 오류가 발생하였습니다!</h3>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <%@ include file="/pageModule/footer.jsp" %>

    <%@ include file="/pageModule/footerPart.jsp" %>
</body>

</html>

[web.xml]

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>JSPBook</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- ch10 기본 인증 처리 방법으로 보안 처리하기 -->
  <!-- <security-role>
  	<role-name>role1</role-name>
  </security-role>
  <security-constraint>
  	<web-resource-collection>
  		<web-resource-name>JSPBookProject</web-resource-name>
  		<url-pattern>/ch10/security01.jsp</url-pattern>
  		<http-method>GET</http-method>
  	</web-resource-collection>
  	<auth-constraint>
  		<description></description>
  		<role-name>role1</role-name>
  	</auth-constraint>
  </security-constraint>
  <login-config>
  	<auth-method>BASIC</auth-method>
  </login-config> -->
  <!-- ch10 기본 인증 처리 방법으로 보안 처리하기 End -->
  
  <!-- ch10 폼 기반 인증 처리 방법으로 보안 처리하기 -->
  <!-- <security-role>
  	<role-name>role1</role-name>
  </security-role>
  <security-constraint>
  	<web-resource-collection>
  		<web-resource-name>JSPBookProject</web-resource-name>
  		<url-pattern>/ch10/security01.jsp</url-pattern>
  		<http-method>GET</http-method>
  	</web-resource-collection>
  	<auth-constraint>
  		<description></description>
  		<role-name>role1</role-name>
  	</auth-constraint>
  </security-constraint>
  <login-config>
  	<auth-method>FORM</auth-method>
  	<form-login-config>
  		<form-login-page>/ch10/login.jsp</form-login-page>
  		<form-error-page>/ch10/login_failed.jsp</form-error-page>
  	</form-login-config>
  </login-config> -->
  <!-- ch10 폼 기반 인증 처리 방법으로 보안 처리하기 End -->
  
  <!-- ch10 프로그래밍적 시큐리티 처리 -->
  <security-role>
  	<role-name>role1</role-name>
  </security-role>
  <security-constraint>
  	<web-resource-collection>
  		<web-resource-name>JSPBookProject</web-resource-name>
  		<url-pattern>/ch10/security02.jsp</url-pattern>
  		<http-method>GET</http-method>
  	</web-resource-collection>
  	<auth-constraint>
  		<description></description>
  		<role-name>role1</role-name>
  	</auth-constraint>
  </security-constraint>
  <login-config>
  	<auth-method>FORM</auth-method>
  	<form-login-config>
  		<form-login-page>/ch10/login.jsp</form-login-page>
  		<form-error-page>/ch10/login_failed.jsp</form-error-page>
  	</form-login-config>
  </login-config>
  <!-- ch10 프로그래밍적 시큐리티 처리 End -->
  
  <error-page>
  	<error-code>500</error-code>
  	<location>/ch11/errorCode_error.jsp</location>
  </error-page>
  
  <error-page>
  	<exception-type>java.lang.Exception</exception-type>
  	<location>/ch11/exceptionType_error.jsp</location>
  </error-page>
  
</web-app>

- http://localhost/ch11/exceptionType.jsp


[tryCatch.jsp]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html class="no-js" lang="zxx">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="x-ua-compatible" content="ie=edge" />
    <title>쉽게 배우는 JSP 웹 프로그래밍</title>
    <meta name="description" content="" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <%@ include file="/pageModule/headPart.jsp" %>
</head>

<body>
    <%@ include file="/pageModule/header.jsp" %>

    <div class="breadcrumbs" style="padding-top:40px;">
        <div class="container">
            <div class="row align-items-center">
                <div class="col-lg-6 col-md-6 col-12">
                    <div class="breadcrumbs-content">
                        <h1 class="page-title">예외 처리</h1>
                    </div>
                </div>
                <div class="col-lg-6 col-md-6 col-12">
                    <ul class="breadcrumb-nav">
                        <li><a href="/">INDEX</a></li>
                        <li>CH11</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>

    <section class="about-us section">
        <div class="container">
            <div class="row align-items-center justify-content-center">
                <div class="col-lg-12 col-md-12 col-12">
                    <div class="content-left wow fadeInLeft" data-wow-delay=".3s">
                    	<form action="tryCatch_process.jsp" method="post">
                    		숫자 1 : <input type="text" name="num1" /><br />
                    		숫자 2 : <input type="text" name="num2" /><br />
                    		<button type="submit">나누기</button>
                    	</form>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <%@ include file="/pageModule/footer.jsp" %>

    <%@ include file="/pageModule/footerPart.jsp" %>
</body>

</html>

[tryCatch_process.jsp]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html class="no-js" lang="zxx">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="x-ua-compatible" content="ie=edge" />
    <title>쉽게 배우는 JSP 웹 프로그래밍</title>
    <meta name="description" content="" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <%@ include file="/pageModule/headPart.jsp" %>
</head>

<body>
    <%@ include file="/pageModule/header.jsp" %>

    <div class="breadcrumbs" style="padding-top:40px;">
        <div class="container">
            <div class="row align-items-center">
                <div class="col-lg-6 col-md-6 col-12">
                    <div class="breadcrumbs-content">
                        <h1 class="page-title">예외 처리</h1>
                    </div>
                </div>
                <div class="col-lg-6 col-md-6 col-12">
                    <ul class="breadcrumb-nav">
                        <li><a href="/">INDEX</a></li>
                        <li>CH11</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>

    <section class="about-us section">
        <div class="container">
            <div class="row align-items-center justify-content-center">
                <div class="col-lg-12 col-md-12 col-12">
                    <div class="content-left wow fadeInLeft" data-wow-delay=".3s">
                    	<% 
                    		try{
                    			String num1 = request.getParameter("num1");
                        		String num2 = request.getParameter("num2");
                        		
                        		int a = Integer.parseInt(num1);
                        		int b = Integer.parseInt(num2);
                        		
                        		int c = a / b;
                        		out.println(num1 + " / " + num2 + " = " + c);
                    		}catch(NumberFormatException e){
                    			request.getRequestDispatcher("tryCatch_error.jsp").forward(request, response);
                    		}
                    	%>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <%@ include file="/pageModule/footer.jsp" %>

    <%@ include file="/pageModule/footerPart.jsp" %>
</body>

</html>

[tryCatch_error.jsp]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html class="no-js" lang="zxx">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="x-ua-compatible" content="ie=edge" />
    <title>쉽게 배우는 JSP 웹 프로그래밍</title>
    <meta name="description" content="" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <%@ include file="/pageModule/headPart.jsp" %>
</head>

<body>
    <%@ include file="/pageModule/header.jsp" %>

    <div class="breadcrumbs" style="padding-top:40px;">
        <div class="container">
            <div class="row align-items-center">
                <div class="col-lg-6 col-md-6 col-12">
                    <div class="breadcrumbs-content">
                        <h1 class="page-title">예외 처리</h1>
                    </div>
                </div>
                <div class="col-lg-6 col-md-6 col-12">
                    <ul class="breadcrumb-nav">
                        <li><a href="/">INDEX</a></li>
                        <li>CH11</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>

    <section class="about-us section">
        <div class="container">
            <div class="row align-items-center justify-content-center">
                <div class="col-lg-12 col-md-12 col-12">
                    <div class="content-left wow fadeInLeft" data-wow-delay=".3s">
                    	<h3>잘못된 데이터가 입력되었습니다!</h3>
                    	<p><%= "숫자 1 : " + request.getParameter("num1") %></p>
                    	<p><%= "숫자 2 : " + request.getParameter("num2") %></p>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <%@ include file="/pageModule/footer.jsp" %>

    <%@ include file="/pageModule/footerPart.jsp" %>
</body>

</html>

- http://localhost/ch11/tryCatch.jsp

 

'대덕인재개발원_웹기반 애플리케이션' 카테고리의 다른 글

231108_JSP 개론 10  (0) 2023.11.08
231107_JSP 과제 5  (0) 2023.11.07
231106_JSP 개론 8  (0) 2023.11.06
231103_JSP 과제 4  (0) 2023.11.03
231103_JSP 개론 7  (0) 2023.11.03