게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
자바빈즈 사용 질문드립니다..
게시물ID : programmer_13360짧은주소 복사하기
작성자 : 닉눼윔
추천 : 0
조회수 : 258회
댓글수 : 3개
등록시간 : 2015/09/17 23:37:56
안녕하세요..

자바빈즈 사용에 궁금한점이 있어서 질문드립니다..

index.jsp에서 값을 입력받아 Values.java로 값을 넘겨줍니다.
(Values.java는 이클립스에서 new-other 에서 servlet을 선택해서 만들었습니다.)

Values.java는 Beans.java에 있는 set을 이용해서 값을 저장하고 second.jsp로 이동합니다.

second.jsp는 Beans.java에서 get을 이용해 데이터를 출력합니다.


이렇게 하려고 했는데요...

<jsp:useBean id="beans" class = "test.Beans" scope="page"/> 
<jsp:getProperty property="val1" name="beans"/><br>

이런식으로 사용이 안되고 

다른 분이 알려준 ${beans.val1} 이런 방법만 되네요 ㅠ..

저렇게 하면 null값이 나옵니다..

왜 저 방법이 안되는걸까요?




index.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%@ 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> Title </title>
    </head>
    <body>
        <form name = "test" method = "post" action = "Values" >
            <input type = "text" name = "val1"><br>
            <input type = "text" name = "val2"><br>
            <input type = "text" name = "val3"><br>
            <input type = "submit" value = "Button" >
        </form>
    </body>
</html>
cs

Values.java(서블릿?)

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package test;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import test.Beans;
 
/**
 * Servlet implementation class Values
 */
public class Values extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Values() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }
 
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
 
        String val1 = request.getParameter("val1");
        String val2 = request.getParameter("val2");
        String val3 = request.getParameter("val3");
        
        Beans beans = new Beans();
 
        beans.setVal1(val1);
        beans.setVal2(val2);
        beans.setVal3(val3);
        
        request.setAttribute("beans", beans);        
        request.getRequestDispatcher("second.jsp").forward(request, response); 
    }
 
}
 
cs





Beans.java


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
package test;
 
import java.sql.*;
import java.util.*;
 
public class Beans 
{
    private String val1;
    private String val2;
    private String val3;
    
    public String getVal1() {
        return val1;
    }
    public void setVal1(String val1) {
        this.val1 = val1;
    }
    public String getVal2() {
        return val2;
    }
    public void setVal2(String val2) {
        this.val2 = val2;
    }
    public String getVal3() {
        return val3;
    }
    public void setVal3(String val3) {
        this.val3 = val3;
    }
}
cs



second.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
<%@ 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>
        1번 : ${beans.val1}<br>
        2번 : ${beans.val2}<br>
        3번 : ${beans.val3}<br>
    </body>
</html>
cs



web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="UTF-8"?>
  <display-name>test</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>
  <servlet>
    <description></description>
    <display-name>Values</display-name>
    <servlet-name>Values</servlet-name>
    <servlet-class>test.Values</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Values</servlet-name>
    <url-pattern>/Values</url-pattern>
  </servlet-mapping>
</web-app>
cs


src/main/java 폴더 안에
패키지 test를 만들고
그 안에 java 파일을 만들어서
Beans.java파일은 커맨드창에서 수동으로 컴파일 했습니다.
컴파일 해서 나온 .class를 
src-main-webapp-WEB-INF 폴더에 classes폴더를 만들고 Beans.class파일을 집어넣었습니다.


꼬릿말 보기
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호