티스토리 뷰

휴지통/JSP

jsp:useBean

LeReve 2013. 5. 23. 19:18

jsp:useBean 형식

<jsp:useBean class="A" id="obj" scope="page"/>
   - 클래스 객체생성 (영역:page, request, session, application)

 

 start.jsp

 <%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<%--start.jsp --%>
<body>
 <h3>나 스타트</h3>
 <a href="forward_test.jsp">forward_test.jsp로 이동</a>
</body>
</html>

forward_test.jsp

 <%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<jsp:useBean class="j0523.Person" id="p" scope="application"></jsp:useBean>
<%--j0523.Person p = new j0523.Person(); --%>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<%--foward_test.jsp --%>
<%
 String str = "너무쉽다";
 request.setAttribute("k", str);//저장:setAttribute(String 속성명(key), Object 속성값(value));
 p.setName("홍길동");
 p.setAge(13);
 p.setJob("학생");
%>
<body>
 <h3>forward테스트</h3>
 <jsp:forward page="end.jsp"/>
</body>
</html>

end.jsp

 <%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<jsp:useBean class="j0523.Person" id="p" scope="application"></jsp:useBean>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<%--end.jsp --%>
<body>
 <%
  out.print("이름:"+p.getName()+"<br>");
  out.print("나이:"+p.getAge()+"<br>");
  out.print("직업:"+p.getJob()+"<br>");
  %>
 <h3>나는 종착페이지</h3>
 <%
  String st = (String)request.getAttribute("k"); //저장
 %>
 STR=<%=st%>
</body>
</html>

Person.java

 package j0523;

public class Person {
 private String name;
 private int age;
 private String job;
 
 public Person() {
  System.out.println("기본생성자");
 }

 public Person(String name, int age, String job) {
  super();
  this.name = name;
  this.age = age;
  this.job = job;
 }

 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 String getJob() {
  return job;
 }

 public void setJob(String job) {
  this.job = job;
 }
}

 

 page

 

 request

 

 

 session

 

 

 

브라우저 재시작 후에 end.jsp 실행 시 이름, 나이, 직업이 null,0,null

 

 

'휴지통 > JSP' 카테고리의 다른 글

setProperty, getProperty  (0) 2013.05.24
forward와 redirect의 차이점  (0) 2013.05.24
jsp:forword  (0) 2013.05.23
<%@ include %>, 액션include 차이점  (0) 2013.05.23
AppletTest  (0) 2013.05.23
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/07   »
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
글 보관함