일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
- ssafy
- 이진법
- ERD
- java
- 알고리즘
- 예외처리
- CS 기초지식
- 삼성청년SW아카데미
- 부스트코스
- Computer Science
- exception
- CS 기초
- 관계형 데이터베이스
- CS기초지식
- 상속
- 모두를 위한 컴퓨터 과학(CS50)
- SW
- Compute Science
- w3schools
- edwith
- Java Programming
- CS50
- WebProgramming
- 모두를 위한 컴퓨터 과학
- til
- SSAFY 9기
- 기초프로그래밍
- 면접을 위한 CS 전공지식 노트
- 데이터베이스 모델링
- 객체지향
- Today
- Total
Joslynn의 하루
MSA Full-Stack 개발자 양성 과정 - JSP 예외 처리, Servlet 기초_221005 본문
MSA Full-Stack 개발자 양성 과정 - JSP 예외 처리, Servlet 기초_221005
Joslynn 2022. 10. 5. 18:05WAS에 저장하는 기술
: 웹에서 가장 중요한 것은 페이지를 이동할 때마다 데이터의 영속성을 유지하는 기술
- pageContext : 페이지가 유지되는 동안
- request : 연결이 유지되는 동안
- session : 브라우저가 유지되는 동안
- application : 서버가 종료되기 전까지
scope의 크기
pageContext < request < session < application
*공통메소드
1) 저장
~.setAttribute(String name, Object value);
2) 조회
Object value = ~.getAttribute(String name);
: name으로 value를 꺼내면 Object로 반환하기 때문에 적절한 DownCasting이 필요함
예외 처리
: 각 페이지마다 예외가 발생하면, 특정 예외 처리 페이지로 이동해서 예외에 대한 처리를 한 번에 해줄 수 있다.
**예외 처리 방법
1) 각 페이지마다 errorPage = "" 설정
: 모든 예외를 한 페이지에서 처리할 수 있음
: 예외 타입별로 페이지를 다르게 설정할 수 없음
2) web.xml 문서 설정
: 예외 타입별 다른 페이지 설정 가능
1) 에러페이지 설정 예제>
<%@ page language="java" contentType="text/html; charset= UTF-8"
pageEncoding="UTF-8" isErrorPage="true" %>
<!-- isErroPage 속성: 에러페이지임을 인정, 500번 코드가 브라우저에 내려감 -->
<%
// 상태 코드를 was에서 강제적으로 정상 코드인 200번으로 변경
//조금 더 안전한 코드
response.setStatus(200);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>예외 발생</h3>
예외 종류 : <%=exception %><p>
예외 메세지 : <%=exception.getMessage() %><p>
예외 클래스 : <%=exception.getClass() %><p>
<hr>
<a href="exceptionForm.jsp">폼으로 이동</a>
</body>
</html>
2) 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"
id="WebApp_ID" version="3.1">
<display-name>step06_JSPExam</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>
<exception-type>java.lang.NumberFormatException</exception-type>
<location>/ex1005/exception/ex02/error/number.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.ArithmeticException</exception-type>
<location>/ex1005/exception/ex02/error/arith.jsp</location>
</error-page>
</web-app>
Servlet문서 작성하기
: 반드시 javax.servlet.http.HttpServlet를 상속받는다.(public class)
ex) public class A extends HttpServlet{
//필요한 메소드 재정의해서 기능 부여
}
Servlet 작성 방법
1) 반드시 public class
2) HttpServlet 상속
3) 필요한 메소드 재정의
4) 실행을 위해서 등록을 한다
- 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"
id="WebApp_ID" version="3.1">
<display-name>step07_ServeletExam</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>
<!-- servlet 등록
lifeCycle = new lifeCycleServelt(); 와 동일
load-on-startup 옵션을 설정하면 tomcat start될 때, 서블릿을 생성한다.
(숫자 옵션: 순서 번호, 순서 없으면 기본으로 1 지정)
load-on-startup 태그가 있어야만 서버 시작시 객체 생성 및 init 메소드 실행
-->
<servlet>
<servlet-name>lifeCycle</servlet-name>
<servlet-class>ex1005.servlet.lifeCycleServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 브라우저에서 요청할 때, 특정 서블릿이 호출될 수 있도록 mapping 필요
url-pattern -> (ex) http://localhost:9000/contextPath/life (반드시 지정 필요) -->
<servlet-mapping>
<servlet-name>lifeCycle</servlet-name>
<url-pattern>/life</url-pattern>
</servlet-mapping>
</web-app>
- @annotation 등록
**HttpServlet 에 있는 관련 메소드
init()
: 서블릿문서가 초기화 될때(최초에 처음 실행될때 호출됨)
: 서블릿은 단 하나만 만들어지고, 사용자들은 하나의 서블릿을 공유하며, thread로 동작
**init-param 정보
1) Servlet 당 전달되는 init-param
: 서블릿 내에서만 사용
2) ServletContext 전달되는 init-param
: ServletContext - application, 프로젝트 당 하나씩 만들어짐
: 글로벌 사용 가능
service(ServletRequest request, ServletResponse response)
: init이 실행된후 호출됨(서블릿문서 새로고침하면 service실행됨)
사용자 요청이 get/post인지를 구분하여 doGet or doPost호출함.
→ 요청 방식을 정하지 않으면 기본 방식: Get 방식
→ doGet 메소드를 자동 실행
: jsp 코드는 servlet으로 변환되면서 service 메소드에 들어가서 전부 실행됨
doGet(HttpServletRequest request , HttpServletResponse response)
: 사용자 요청 get방식일경우 실행됨
doPost(HttpServletRequest request , HttpServletResponse response)
: 사용자 요청이 post방식일때 실행됨.
destory()
: 서블릿문서가 종료될때호출됨.
**주의사항
: service 메소드를 재정의하면 doGet 또는 doPost를 호출하지 않음
1) 요청 방식을 구분할 필요 없이 할 일이 있을 경우 → service 메소드 오버라이드
2) 요청 방식 구분 필요할 경우 → doGet / doPost 메소드 오버라이드
'MSA Full-Stack 개발자 양성과정 > JSP&Servlet' 카테고리의 다른 글
[Servlet] annotation과 xml 설정 (0) | 2022.10.11 |
---|---|
MSA Full-Stack 개발자 양성 과정 - JSP 표현언어(EL)&JSTL_221006 (0) | 2022.10.06 |
MSA Full-Stack 개발자 양성 과정 - JSP 내장객체 session, application / Java AtomicInteger_221004 (1) | 2022.10.04 |
MSA Full-Stack 개발자 양성 과정 - JSP 페이지 이동방식, 내장객체 request, response_220930 (0) | 2022.09.30 |
MSA Full-Stack 개발자 양성 과정 - JSP_220929 (0) | 2022.09.29 |