빅데이터교육과정/WEB

DAY.5 Beans, Error Page

Listeria 2021. 3. 8. 17:54

1. Beans

Beans란?

자바에서 데이터를 구현하기 위해 만드는 클래스이다.

public class Example{

	private int num;
    
    public int getNum(){
    	return num;
    }
    public void setNum(int num){
    	this.num=num;
    }
}
        

위와 같은 형태를 가지는 것을 빈이라고 한다.

JSP의 빈은 JAVA의 빈 컴포넌트와 상호작용을 지원하며 기본적인 형식은 아래와 같다.

 

<jsp:useBean id="변수명" class="빈즈 클래스명" scope="영역"/>
<jsp:getProperty id="변수명" property="속성명"/>
<jsp:setProperty id="변수명" property="속성명" value="값"/>

 

JSP Bean의 형태는 Java Bean과 유사한 형태를 띄고 있으며 useBean는 Java의 new와 유사한 역할을 하며 get과 set Property는 Java의 getter와 setter와 유사한 역할을 한다.

하지만 MVC 모델에서는 useBean 태그를 사용할 수 없기때문에 jsp만 활용할 때 사용된다.

(class에서 useBean 태그 사용이 불가능함.)


2. Error Code 구현

<?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_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>DAY2</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
    <error-page><!--400에러처리-->
    <error-code>400</error-code>
    <location>/404err.jsp</location>
  </error-page>
      <error-page><!--401에러처리-->
    <error-code>401</error-code>
    <location>/404err.jsp</location>
  </error-page>
      <error-page><!--402에러처리-->
    <error-code>402</error-code>
    <location>/404err.jsp</location>
  </error-page>
      <error-page><!--403에러처리-->
    <error-code>403</error-code>
    <location>/404err.jsp</location>
  </error-page>
      <error-page><!--404에러처리-->
    <error-code>404</error-code>
    <location>/404err.jsp</location>
  </error-page>
  
  
  <error-page><!--500에러처리-->
    <error-code>500</error-code>
    <location>/500err.jsp</location>
  </error-page>
  
  
</web-app>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>못찾겠다 페이지</title>
</head>

<body>
	
	<jsp:forward page="forTable2.jsp">
		<jsp:param name="errCode" value="404"/>	//400번대 에러 코드가 발생했을때 나타나는 메세지를 forward방식으로 호출하여 나타낸다.
	</jsp:forward>


</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%=request.getParameter("errCode") %>	//전달받은 errCoder를 출력한다.
<hr>					//구분선
<h2>nothing in this page hahahaha</h2>	//정해진 문구 출력

</body>
</html>

XML 파일에 위와 같이 처리를 해주면 해당 에러코드가 발생했을때 특정 파일로 이동하도록 설정할 수 있다. 위 코드를 실행하면 먼저 구구단 표가 작성되며 아래에 무의미한 곳으로 연결하는 링크가 실행된다. 그리고 이를 누르면 4xx번대 코드로 연결되는 jsp 파일이 연결되며 이 파일이 다시 문구를 출력하는 함수를 forward방식으로 불러와 최종 페이지에 디스플레이 된다.