MemberBean.java
package com.sw.javabeans;
public class MemberBean {
private String name;
private String userid;
//getter, setter
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
//매개변수가 있는 생성자
public MemberBean(String name, String userid) {
super();
this.name = name;
this.userid = userid;
}
//매개변수 없는 디폴트 생성자
public MemberBean() {
super();
// TODO Auto-generated constructor stub
}
//참조변수만 기술해도 출력될 수 있도록 toString() 오버라이딩
@Override
public String toString() {
return "MemberBean [name=" + name + ", userid=" + userid + "]";
}
}
회원 정보를 저장한 자바 빈 객체를 생성하기 위해 서블릿 클래스를 생성한다.
MemberServlet.java
package unit09;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sw.javabeans.MemberBean;
@WebServlet("/MemberServlet")
public class MemberServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
MemberBean member=new MemberBean("전수빈", "pinksubin");
request.setAttribute("member", member);
RequestDispatcher dispatcher = request.getRequestDispatcher("09_el.jsp");
dispatcher.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>EL과 JSTL</title>
</head>
<body>
이름: ${member.name}<br>
아이디: ${member["userid"]}
</body>
</html>