SSENI's
search
sseni
말하는 감자에서 자라기
Today
Yesterday
[웹서버프로그래밍] 2022.05.24 리스너와 필터, Django 셋업
# 리스너
컨테이너에서 발생하는 특정 이벤트 상황을 모니터링하다가 실행되는 특수한 형태의 서블릿
- ServletContext (생명주기 변화, 속성 변화)
- Session
- Request
상태나 속성의 변화를 모니터링
[실습1] ServletContextListener 구현
- Book.java
package jspbook.ch13;
public class Book {
// 멤버변수 선언
private String title;
private String author;
private int price;
private String publisher;
// 기본 생성자, 파라미터로 데이터 초기화
public Book(String title,String author, int price, String publisher) {
this.title = title;
this.author = author;
this.price = price;
this.publisher = publisher;
}
public String getAuthor() {
return author;
}
public int getPrice() {
return price;
}
public String getPublisher() {
return publisher;
}
public String getTitle() {
return title;
}
}
- TestContextListener.java
package jspbook.ch13;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class TestContextListener implements ServletContextListener {
public TestContextListener() {
}
// 리스너 실행 메서드
public void contextInitialized(ServletContextEvent arg0) {
ServletContext ctx = arg0.getServletContext();
// Book 객체를 만들어 application scope 에 저장
Book mybook = new Book("자바웹프로그래밍","황희정",20000,"한빛미디어");
ctx.setAttribute("book", mybook);
System.out.println("TestContextListener 시작됨..");
}
public void contextDestroyed(ServletContextEvent arg0) {
}
}
- ListenerTest.jsp
<%@ 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>
<div aign="center">
<H2>ch13:ListenerTest.jsp</H2>
<HR>
도서명 : ${book.title} <BR>
저자명 : ${book.author} <BR>
가격 : ${book.price} <BR>
출판사 : ${book.publisher} <BR>
</div>
</body>
</html>
# 필터
특정 요청에 대해서만 동작하는 특수한 형태의 웹 프로그램
++ DJango
Ⅱ. Preparation
① conda create -n hellodjango python=3.8
② conda activate hellodjango
③ pip install django
Ⅲ. Generate a Starter Django Project
① django-admin startproject hellodjango
② code hellodjango
Ⅳ. Create a Database and Run the Project
① python manage.py migrate
② python manage.py runserver
③ http://127.0.0.1:8000
④ python manage.py createsuperuser
Ⅴ. Practice Using the Admin
① python manage.py runserver
② http://127.0.0.1:8000/admin/
conda activate hellodjango
cd hellodjango
python manage.py startapp homepage
- views.py
from django.shortcuts import render
from django.views.generic import TemplateView
class HomepageView(TemplateView):
template_name = 'index.html'
from django.contrib import admin
from django.urls import path
from homepage.views import HomepageView
urlpatterns = [
path('admin/', admin.site.urls),
path('', HomepageView.as_view(), name='home'),
]
- templates/index.html
<h1>Greeting</h1>
<p>Hi?</p>
[웹서버프로그래밍] 2022.06.07 파일 업로드, 간이 웹하드 구현 (1) | 2022.06.07 |
---|---|
[웹서버프로그래밍] 2022.05.31 Django (0) | 2022.05.31 |
[웹서버프로그래밍] 2022.05.17 JSTL 태그 실습, DB 커넥션 풀과 트랜잭션, 초기화 매개변수 (0) | 2022.05.17 |
[웹서버프로그래밍] 2022.05.10 커스텀 태그, JSTL 태그 (0) | 2022.05.10 |
[웹서버프로그래밍] 2022.05.03 database connections, 표현 언어 (0) | 2022.05.03 |