반응형
[1] 테스트 환경
hardware | |
cpu : intel i7-7700, gpu : intel hd 630, ram : ddr4 16GB, mainboard : samsung DB400S7A-Z51, secondary storage : 256GB SSD, 1TB HDD |
OS |
windows pro 64bits |
software |
jdk version 1.8.0_301 eclipse version 2021-09 (4.21.0) apache-tomcat version 9.0.54 |
[2] 강의 정리
()
applicationGet.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String name = null;
int age = 0;
if(application.getAttribute("name") != null &&
application.getAttribute("age") != null){
name = (String)application.getAttribute("name");
age = (Integer)application.getAttribute("age");
}
%>
<h2>어플리케이션에 저장된 이름 : <%=name %></h2>
<h2>어플리케이션에 저장된 나이 : <%=age %></h2>
</body>
</html>
applicationSet.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%--
application 객체는 jsp페이지에서 선언하지 않아도 사용가능한 내장객체이다.
application 객체는 자신이 속한 웹어플리케이션 범위 안에 모든 jsp에서 공유된다.
--%>
<%
application.setAttribute("name", "홍길동");
application.setAttribute("age", 20);
// setAttribute("데이터 이름", 데이터 값);
// 이름은 String 타입으로 들어가고 값은 Object 타입이다.
%>
<h2>어플리케이션에 이름과 나이를 저장했습니다.</h2>
<a href="applicationGet.jsp">어플리케이션 GET</a>
</body>
</html>
()
applicationEx.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%--
application 객체를 사용해서
웹브라우저에 (applicationEx.jsp) 방문 시
방문자수 출력하기 >> [현재 방문자수는 : **명 입니다]
--%>
<%
int count = 0;
if(application.getAttribute("visit") != null){
count = (Integer)application.getAttribute("visit");
}
count++;
application.setAttribute("visit", count);
%>
<h2>현재 방문자수는 : <%=count %>명 입니다.</h2>
<script>
var i = <%=count%>
if(i==11){
alert('11번째 방문에 감사드립니다.');
}
</script>
</body>
</html>
()
error.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page errorPage="errorPage.jsp" %>
<%--
현재 jsp 페이지에서 예외가 발생했을 때 사용자에게 보여줄
예외화면을 처리할 jsp페이지의 경로를 지정한다.
--%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
int i = 10 / 0;
%>
</body>
</html>
errorPage.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page isErrorPage="true" %>
<%--
isErrorPage : 속성값이 true면 현재 jsp페이지가
예외처리 페이지라는 것을 명시하며 exception 객체를
사용할 수 있게된다. (예외에 대한 정보를 얻을 수 있다.)
--%>
<%
response.setStatus(200);
%>
<%--
에러페이지가 정상적으로 호출이 되려면 HTTP 상태코드가 정상값인 200이 되야한다.
response 객체에 상태값 200을 셋팅해야 한다.
--%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>에러가 발생!!!</h2>
<h2><%= exception.getMessage()%></h2>
</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_4_0.xsd" version="4.0">
<display-name>JSP03</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>
<error-code>404</error-code>
<location>/practice09/error404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/practice09/error500.jsp</location>
</error-page>
</web-app>
errorEx1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="jsp1004.jsp">jsp1004.jsp</a>
</body>
</html>
errorEx2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
int i = 10 / 0;
%>
</body>
</html>
error404.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page isErrorPage="true" %>
<%
response.setStatus(200);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<script>
alert('요청 페이지가 없습니다.');
</script>
<h2>404 에러입니다.</h2>
<h2>관리자에게 문의하세요.</h2>
</body>
</html>
error500.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page isErrorPage="true" %>
<%
response.setStatus(200);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<script>
alert('서버에서 예기치 못한 오류가 발생되었습니다.')
</script>
<h2>500에러 입니다.</h2>
<h2>관리자에게 문의하세요~!</h2>
</body>
</html>
()
error.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page errorPage="errorPage.jsp" %>
<%--
현재 jsp 페이지에서 예외가 발생했을 때 사용자에게 보여줄
예외화면을 처리할 jsp페이지의 경로를 지정한다.
--%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
int i = 10 / 0;
%>
</body>
</html>
errorPage.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page isErrorPage="true" %>
<%--
isErrorPage : 속성값이 true면 현재 jsp페이지가
예외처리 페이지라는 것을 명시하며 exception 객체를
사용할 수 있게된다. (예외에 대한 정보를 얻을 수 있다.)
--%>
<%
response.setStatus(200);
%>
<%--
에러페이지가 정상적으로 호출이 되려면 HTTP 상태코드가 정상값인 200이 되야한다.
response 객체에 상태값 200을 셋팅해야 한다.
--%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>에러가 발생!!!</h2>
<h2><%= exception.getMessage()%></h2>
</body>
</html>
()
bean.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="stu" class="com.java.jsp.Student" scope="page" />
<%--
id : jsp페이지에서 자바빈 객체에 접근할때 사용하는 이름 (빈 이름)
class : 패키지명을 포함한 자바빈 클래스의 완전한 이름 입력
scope : 자바빈 객체가 저장될 영역을 지정
(page, request, session, application 중 하나를 값으로 설정. 기본값 page)
--%>
<jsp:setProperty name="stu" property="studentID" value="202201" />
<jsp:setProperty name="stu" property="name" value="홍길동" />
<jsp:setProperty name="stu" property="age" value="20" />
<jsp:setProperty name="stu" property="grade" value="1" />
<%--
name : 자바빈 이름명시
property : 속성이름
value : 데이터(값) 설정
--%>
학번 : <jsp:getProperty name="stu" property="studentID" /> <br>
이름 : <jsp:getProperty name="stu" property="name" /> <br>
나이 : <jsp:getProperty name="stu" property="age" /> <br>
학년 : <jsp:getProperty name="stu" property="grade" />
</body>
</html>
main>java>com.java.jsp
student.java
package com.java.jsp;
public class Student {
// 자바빈 규약
// 1. 빈이 패키지화 되어있어야 한다. (default 패키지x)
// 2. 기본 생성자를 반드시 가지고 있어야 한다.
// (getter / setter 로 프로퍼티값을 할당할 목적으로 만들어졌으므로)
// 3. 멤버변수의 접근제한자는 private으로 선언한다.
// 4. 멤버변수에 접근하기 위한 public 접근자인 getter / setter가 있어야한다.
private int studentID;
private String name;
private int age;
private int grade;
public Student() {
}
public int getStudentID() {
return studentID;
}
public void setStudentID(int studentID) {
this.studentID = studentID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
}
()
beanEx.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%--
1. useBean 액션태그로 Member 빈객체 생성하기
2. setProperty 액션태그로 Member 빈객체에 데이터 넣기
3. getProperty 액션태그로 Member 빈객체의 데이터 가져와서 출력하기
--%>
<jsp:useBean id="member" class="com.java.jsp.Member" />
<jsp:setProperty property="id" name="member" value="hong" />
<jsp:setProperty property="pw" name="member" value="1234" />
<jsp:setProperty property="name" name="member" value="홍길동" />
아이디 : <jsp:getProperty property="id" name="member"/><br>
비밀번호 : <jsp:getProperty property="pw" name="member"/><br>
이름 : <jsp:getProperty property="name" name="member"/><br>
</body>
</html>
main>java>com.java.jsp
Member.java
package com.java.jsp;
public class Member {
private String id;
private String pw;
private String name;
public Member() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPw() {
return pw;
}
public void setPw(String pw) {
this.pw = pw;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
반응형
'강의 > JSP 강의 정리' 카테고리의 다른 글
JSP 강의 정리 13일차 (0) | 2022.07.22 |
---|---|
JSP 강의 정리 12일차 (0) | 2022.07.21 |
JSP 강의 정리 10일차 (0) | 2022.07.19 |
JSP 강의 정리 9일차 (0) | 2022.07.18 |
JSP 강의 정리 8일차 (0) | 2022.07.15 |