티스토리 뷰
Action tags
- jsp:forward
- with a parameter: <jsp:forward page="relativeURL | <%= expression %>" />
- without a parameter
- jsp:include
Q. <%@ include file=""%> 와 <jsp:include page=""> 의 차이는?
JavaBean 이란?
Javaclass 처럼 사용하는 패키지이다. JSP에서 객체를 사용할 수 있도록 한 기법이다.
- set와 get method?
JavaBean class에 접근하려면 getter, setter method를 사용해야 한다.
- jsp:useBean: 자바빈 객체를 생성하기 위한 코드
- endtag( </jsp:useBean> )을 사용하지 않을 때는 start tag 맨 끝에 "/"를 꼭 넣도록 하자.
<jsp:useBean id="빈 이름" class="자바빈 클래스명" scope="사용범위"/>
class가 패키지 안에 있다면 class에 패키지 경로까지 적어야 한다.
scope에는 request, page, session, application 4 종류가 존재한다.
- id
- scope
- class
- type
- beanName
- jsp:setProperty: 자바빈 클래스의 속성 값을 설정하기 위한 태그
<jsp:setProperty name="빈 이름" property="속성명" value="설정할 값"/>
<jsp:setProperty name="빈 이름" property="속성명" param="파라미터명"/>
name은 선언된 객체의 id라고 생각하면 되다.
- jsp:getPropertry: 자바빈 클래스의 속성값을 가져오기 위한 태그
jsp:getProperty name="빈 이름" property="속성명"/>
- jsp:plugin
- jsp:param
클래쓰를 만들때는 STS에서 Java resources에 src 부분에 만든다.
예제) Calculator.java 빈 클래쓰를 만들어보자
Calculator.java
package com.javatpoint;
public class Calculator{
public int cube(int n){return n*n*n;}
}
index.jsp
<jsp:useBean id="obj" class="com.javatpoint.Calculator"/>
<%
int m=obj.cube(5);
out.print("cube of 5 is "+m);
%>
참고: https://m.blog.naver.com/start3535/30190419527
jsp:setProperty
기본 형식:
<jsp:setProperty name="instanceOfBean" property= "*" |
property="propertyName" param="parameterName" |
property="propertyName" value="{ string | <%= expression %>}"
/>
모든 value에 대해 속성을 적용 시키고 싶다면:
<jsp:setProperty name="bean" property="*" />
jsp:getProperty
기본 형식:
<jsp:getProperty name="instanceOfBean" property="propertyName" />
Bean을 사용한 예제
다음과 같이 Form을 제출하고 사용자의 입력 값을 출력해주는 JSP 페이지를 만들어보자.
필요한 파일:
- index.html
<%@ 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>
<form action="register_form_process.jsp" method="post">
Name:<input type="text" name="name"><br>
Password:<input type="password" name="password"><br>
Email:<input type="text" name="email"><br>
<input type="submit" value="register">
</form>
</body>
</html>
- process.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="u" class="com.Form.User">
</jsp:useBean>
<jsp:setProperty property="*" name="u"/>
Input value:
<jsp:getProperty property="name" name="u"/><br/>
<jsp:getProperty property="password" name="u"/><br/>
<jsp:getProperty property="email" name="u"/><br/>
</body>
</html>
- User.java
- 여기서 변수 이름은 input type에서 설정한 name과 반드시 같은 변수 이름으로 설정해주어야 한다!!
package com.Form;
public class User {
private String name, password, email;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
자바빈에서 getters와 setters 설정하는 방법:
이 부분에서 마우스 오른쪽 클릭 > Source > Generate Getters and Setters > Select All > 확인
참고 링크: https://kanu.tistory.com/32
'JSP' 카테고리의 다른 글
JSP를 이용해서 form의 값 받아오기 (0) | 2020.08.09 |
---|---|
JSP: EL (0) | 2020.08.07 |
Development in JSP (0) | 2020.08.06 |
JSP Project Hosting (0) | 2020.08.06 |
JSP Directive Elements (0) | 2020.08.04 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- JSP환경구축
- JSP_CRUD
- fontstyle
- HTML #Headings #Paragraph #Styles
- html
- head #title #style
- HTML #Tables
- HTML #Canvas #SVG
- HTML_Formatting
- jsp
- DB4free
- HTML #media #video #YouTube
- STS4
- links lists tables display
- 2020Camp
- HTML_Forms
- css
- annotation
- text_shadow
- meta #link #script #base #HTML
- HTML #class
- 인용문 #주석
- Mavenproject
- Block_element #inline_element
- box_model
- DynamicWebProject
- javascript #datatype
- HTML #id #iframe
- HTML #CSS
- UserBean
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함