선언문(declaration)
<%! int count = 3; %>
이걸 쓰면 자바의 문법을 쓸 수 있음 <%!
int count = 3;
String makeItLower(String data){
return data.toLowerCase();
}
%>
위는 멤버 변수와 멤버 함수로 내부에서 인식
= 얘는 클래스의 변수로 들어가게 되고
스크립틀릿(scriptlet)
<%
for(int i = 0; i<count; i++){
out.println("java server page "+ i + "<br>");
}
%>
표현문(expression)
<%=makeItLower("Hello world")%>
https://kyun2.tistory.com/47
https://codevang.tistory.com/197
*ln 안먹으니까 br
%!로 전역변수로 만들어야함
<%! int count = 0 ; %>
Page Count is :
<%
count++;
out.print(count);
%>
오늘은 :
<%
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy년 MM월 dd일 HH시 mm분 ss초");
String formmattedDate = formatter.format(date);
%>
<%= formmattedDate %>
<h1>현재 날짜를 표시하는 화면</h1>
<h3>이 페이지에는 날짜 관련 코드가 없습니다.</h3>
<%@ include file="./timer_js.jsp" %>
<h2>forward 액션 태그</h2>
<jsp:forward page="./timer_js.jsp"></jsp:forward>
<p>-----------------------------------------------------
<!-- forward를 만났기 때문에 p가 표시되지 않음:
클라이언트가 서버한테 foward.jsp줘 했지만
페이지를 전환하는 forward 명령어가 있어서 timer를 주는 것
클라이언트는 'forward 액션 태그'라는 파일을 호출했지만
서버가 알아서 timer를 주는 것
-->
<h3>이 파일은 first.jsp입니다.</h3>
<jsp:include page="./second.jsp"></jsp:include>
<p>java Server Page</p>
<h3>이 파일은 first.jsp입니다.</h3>
<jsp:include page="./second.jsp">
<jsp:param value="hong gil dong" name="myname"/>
</jsp:include>
<p>java Server Page</p>
<h1>이름은: <%=request.getParameter("myname") %> </h1>
<h3>이 파일은 second.jsp입니다.</h3>
<p>아이디 : <%=request.getParameter("id") %> </p>
<p>이름 : <%=URLDecoder.decode(request.getParameter("name"))%> </p>
<h3>param 액션 태그</h3>
<jsp:forward page="./param01_data.jsp">
<jsp:param value="admin" name="id"/>
<jsp:param value='<%=URLEncoder.encode("관리자") %>' name="name"/>
</jsp:forward>
@@여기부터 day2@@
java.io.Serializable 인터페이스를 구현
기본생성자가 존재해야 함
모든 변수는 private
변수에 대한 getter/setter가 존재
시스템에서 제공하는 클래스를 빈으로 등록 사용 가능
@@ 근데 singletonedemo에서 왜 객체 생성없이 getinstance로 사용해야하는 거지?
예제1
<jsp:useBean id="d" class="java.util.Date"></jsp:useBean>
<h1>오늘의 날짜? </h1>
<%=d %>
<%-- 위와 같은 의미
<% Date d = new Date(); %>
--%>
예제2
package dao;
public class Calculator {
public int myMethod(int n) {
return n*n*n;
}
}
<jsp:useBean id="ca" class="dao.Calculator"></jsp:useBean>
<h1> 5의 3제곱? </h1>
<%=ca.myMethod(5) %>
예제3
// usebean4
<jsp:useBean id="person" class="dao.Person" scope="request"></jsp:useBean>
<h4> 아이디: <%= person.getId() %> </h4>
<h4> 이름: <%= person.getName() %> </h4>
<%
person.setId(200);
person.setName("길복순");
%>
--------------
<h3>include usebean3 데려온 부분 </h3>
<jsp:include page="usebean3.jsp"></jsp:include>
<p> * (scope="page") -> 100, 호랑이 --- 다른 jsp 페이지에서는 안바뀜
<p> * scope="request" -> 200, 길복순 --- 다른 jsp 페이지에서도 바뀜
<h4>단 (jsp:useBean id="person" ) 그 jsp 파일에서도 id가 같게 설정된 경우만</h4>
<h4>그리고 두 jsp 파일 다 scope="request" 되어있어야 </h4>
--------------
<h4> set 후 아이디: <%= person.getId() %> </h4>
<h4> set 후 이름: <%= person.getName() %> </h4>
// usebean3
<jsp:useBean id="person" class="dao.Person" scope="request"></jsp:useBean>
<h4> 아이디: <%= person.getId() %> </h4>
<h4> 이름: <%= person.getName() %></h4>
예제4
//
package dao;
import java.io.Serializable;
public class Product implements Serializable{
private static final long serialVersionUID = 1L;
private String productId; // 상품아이디
private String pname; // 상품명
private Integer unitPrice; // 상품 가격
private String description; // 상품 설명
private String manufacturer; // 제조사
private String category; // 분류
private long unitInStock; // 재고수
private String condition; // 신품, 중고품, 재생품
public Product() {};
public Product(String productId, String pname, Integer unitPrice, String description, String manufacturer,
String category, long unitInStock, String condition) {
this.productId = productId;
this.pname = pname;
this.unitPrice = unitPrice;
this.description = description;
this.manufacturer = manufacturer;
this.category = category;
this.unitInStock = unitInStock;
this.condition = condition;
}
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public Integer getUnitPrice() {
return unitPrice;
}
public void setUnitPrice(Integer unitPrice) {
this.unitPrice = unitPrice;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public long getUnitInStock() {
return unitInStock;
}
public void setUnitInStock(long unitInStock) {
this.unitInStock = unitInStock;
}
public String getCondition() {
return condition;
}
public void setCondition(String condition) {
this.condition = condition;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
// 5
public class SingleToneDemo {
public static void main(String[] args) {
SingleTone s1 = SingleTone.getInstance();
s1.setId(100);
System.out.println("s1:"+ s1.getId());
SingleTone s2 = SingleTone.getInstance();
s2.setId(200);
System.out.printf("s1 id = %d s2 id = %d\n", s1.getId(), s2.getId());
SingleTone s3 = SingleTone.getInstance();
System.out.println("s3:"+ s3.getId());
SingleTone s4 = new SingleTone();
s4.setId(300);
System.out.println("s4:"+ s4.getId());
SingleTone s5 = new SingleTone();
s4.setId(400);
System.out.printf("s4 id = %d s5 id = %d\n", s4.getId(), s5.getId());
}
실행결과
s1:100
s1 id = 200 s2 id = 200
s3:200ㅇ
s4:300
s4 id = 400 s5 id = 0
1) SingleTone s1 = SingleTone.getInstance();
2) s1 = new singletone();
위 두개의 차이점
1) s4를 바꾸면 s5도 바뀜
2) s2를 바꿔도 s1은 안바뀜
(?) bean은 동일 객체다
// scope = request이면 같은 범위이다.
// 자바빈을 만들 때는 그냥 클래스명 만으로 할 ㅜㅅ 있는데
// 기존 걸 그대로 쓰는 것이 ??@@
https://sky17777.tistory.com/93
https://code4.tistory.com/category/JSP
여기에 other - class로
앗 패키지 만들고: 타이핑으로 할 수도 있음
package dao;