// select박스 만들어서 넣고 값 가져오기
<!-- ch09/teamInsert.jsp -->
<%@page import="java.util.Vector"%>
<%@page contentType="text/html; charset=UTF-8"%>
<jsp:useBean id="mgr" class="ch09.TeamMgr"/>
<%
Vector<String> vlist = mgr.teamList();
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Team Mgr</title>
<link href="style.css" rel="stylesheet" type="text/css">
<script type="text/javascript">
function check() {
f = document.frm;
if(f.name.value==""){
alert("이름을 입력하세요");
f.name.focus();
return; //함수 빠져나옴
}
if(f.city.value==""){
alert("사는곳을 입력하세요");
f.city.focus();
return; //함수 빠져나옴
}
if(f.age.value==""){
alert("나이를 입력하세요");
f.age.focus();
return; //함수 빠져나옴
}
if(f.team.value==""){
alert("팀을 입력하세요");
f.team.focus();
return; //함수 빠져나옴
}
f.submit();//제출
}
function check2() {
document.frm.action = "teamInsertProc2.jsp";
document.frm.submit();
}
function selectTeam(){
//getElementById 이용해서 값 가져오기
teamselect = document.getElementById("teamselect");
team = teamselect.options[teamselect.selectedIndex].value;
document.frm.team.value = team;
}
function selectTeam1(team){
//매개변수 this.value 이용해서 가져오지
document.frm.team.value=team;
}
</script>
</head>
<body>
<div align = "center">
<h1>Team Insert</h1>
<form name="frm" method="post" action="teamInsertProc.jsp">
<table border="1">
<tr>
<td width="50" align="center">이름</td>
<td width="150"><input name="name" value="홍길동"></td>
</tr>
<tr>
<td align="center">사는곳</td>
<td><input name="city" value="부산"></td>
</tr>
<tr>
<td align="center">나이</td>
<td ><input name="age" value="27"></td>
</tr>
<tr>
<td align="center">팀명</td>
<td>
<input name="team" size="10">
<select id="teamselect" onchange="selectTeam1(this.value)">
<%for(int i=0;i<vlist.size();i++){%>
<option value="<%=vlist.get(i)%>"><%=vlist.get(i)%></option>
<%}%>
</select>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="button" value="SAVE" onclick="check()">
<input type="button" value="SAVE2" onclick="check2()">
</td>
</tr>
</table>
</form><p>
<a href="teamList.jsp">LIST</a>
</div>
</body>
</html>
범위 지정
<% session.setAttribute("sbean", sbean); %>을 많이 사용
package ch09;
public class ScopeBean {
private int num;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
<!-- ch09/scopeBean.jsp -->
<%@ page contentType="text/html; charset=UTF-8"%>
<jsp:useBean id="pbean" class="ch09.ScopeBean" scope="page"/>
<jsp:useBean id="sbean" class="ch09.ScopeBean" scope="session"/>
<!--
scope가 session이면 id가 동일한 객체가 있는지 없는지 검색
-> 없으면 새롭게 생성하지만
-> 있으면 그 객체를 재사용
-> 그러나 세션이 종료 또는 제거가 되면 안에 있는 객체도 같이 없어짐.
-> scope="session" 밑에 코드가 많이 사용
-->
<% session.setAttribute("sbean", sbean); %>
<jsp:setProperty property="num" name="pbean"
value="<%=pbean.getNum()+10 %>"/>
<jsp:setProperty property="num" name="sbean"
value="<%=sbean.getNum()+10 %>"/>
<h1>Scope Bean</h1>
pbean num값 : <jsp:getProperty property="num" name="pbean"/><p>
sbean num값 : <jsp:getProperty property="num" name="sbean"/><p>
<a href="scopeBean2.jsp">세션종료</a>
<%@ page contentType="text/html; charset=UTF-8"%>
<%
//세션 특정한 값만 제거
session.removeAttribute("pbean");
//세션을 전체를 제거, 무효화, 초기화
session.invalidate();
response.sendRedirect("scopeBean.jsp");
%>
<!-- ch12/cookCookie.jsp -->
<%@ page contentType="text/html; charset=UTF-8"%>
<%
String cookieName = "myCookie";
//쿠키를 생성
Cookie cookie = new Cookie(cookieName,"Apple");
cookie.setMaxAge(60); //1분
//쿠키의 값을 변경
cookie.setValue("Melone");
//쿠키 생성은 JSP
response.addCookie(cookie);
%>
쿠키를 만들었습니다.<br>
쿠키 내용은 <a href="tasteCookie.jsp">여기로!!</a>
<%@ page contentType="text/html; charset=UTF-8"%>
<%
//응답된 쿠키의 정보는 request에 저장
Cookie cookies[] = request.getCookies();
if(cookies != null){
for (int i=0;i<cookies.length;i++){
%>
Cookie Name : <%=cookies[i].getName()%><br>
Cookie Value : <%=cookies[i].getValue()%><br>
<%
}
}
%>
<a href="cookCookie.jsp">쿠키요리~</a>
package guestbook;
public class CommentBean {
private int cnum;
private int num;
private String cid;
private String comment;
private String cip;
private String cregDate;
public int getCnum() {
return cnum;
}
public void setCnum(int cnum) {
this.cnum = cnum;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getCid() {
return cid;
}
public void setCid(String cid) {
this.cid = cid;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public String getCip() {
return cip;
}
public void setCip(String cip) {
this.cip = cip;
}
public String getCregDate() {
return cregDate;
}
public void setCregDate(String cregDate) {
this.cregDate = cregDate;
}
}
package guestbook;
public class GuestBookBean {
private int num;
private String id;
private String contents;
private String ip;
private String regdate;
private String regtime;
private String secret;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getContents() {
return contents;
}
public void setContents(String contents) {
this.contents = contents;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getRegdate() {
return regdate;
}
public void setRegdate(String regdate) {
this.regdate = regdate;
}
public String getRegtime() {
return regtime;
}
public void setRegtime(String regtime) {
this.regtime = regtime;
}
public String getSecret() {
return secret;
}
public void setSecret(String secret) {
this.secret = secret;
}
}
package guestbook;
public class JoinBean {
private String id;
private String pwd;
private String name;
private String email;
private String hp;
private String grade;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getHp() {
return hp;
}
public void setHp(String hp) {
this.hp = hp;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
}
package guestbook;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
public class GuestBookMgr {
private DBConnectionMgr pool;
private final SimpleDateFormat SDF_DATE =
new SimpleDateFormat("yyyy'년' M'월' d'일' (E)");
private final SimpleDateFormat SDF_TIME =
new SimpleDateFormat("H:mm:ss");
public GuestBookMgr() {
pool = DBConnectionMgr.getInstance();
}
//Join Login
//select id from tblJoin where id = ? and pwd = ?
public boolean loginJoin(String id, String pwd) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = null;
boolean flag = false;
try {
con = pool.getConnection();
sql = "select id from tblJoin where id = ? and pwd = ?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.setString(2, pwd);
rs = pstmt.executeQuery();
if(rs.next())
{
flag = true;
}
//flag = rs.next();//결과값 있다면 true, 없다면 false
} catch (Exception e) {
e.printStackTrace();
} finally {
pool.freeConnection(con, pstmt, rs);
}
return flag;
}
//Join Information
//select * from tblJoin where id = ?
public JoinBean getJoin(String id){
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = null;
JoinBean bean = new JoinBean();
try {
con = pool.getConnection();
sql = "select * from tblJoin where id = ?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, id);
rs = pstmt.executeQuery();
if(rs.next())
{
bean.setId(rs.getString("id"));
bean.setPwd(rs.getString("pwd"));
bean.setName(rs.getString("name"));
bean.setEmail(rs.getString("email"));
bean.setHp(rs.getString("hp"));
bean.setGrade(rs.getString("grade"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
pool.freeConnection(con, pstmt, rs);
}
return bean;
}
}
<!-- guestbook/login.jsp -->
<%@page contentType="text/html;charset=UTF-8"%>
<jsp:useBean id="login" class="guestbook.JoinBean" scope="session"/>
<%
String id = (String)session.getAttribute("idKey");
%>
<title>로그인</title>
<link href="css/style.css" rel="stylesheet" type="text/css">
<body bgcolor="#996600">
<br><br>
<div align="center">
<%
if(id!=null){
%>
<b><%=login.getName()%></b>님 환영합니다.<br>
<a href="showGuestBook.jsp" >방명록 </a>
<a href="logout.jsp" >로그아웃</a>
<%
}else{
%>
<h2>GuestBook 로그인</h2>
<form method="post" action="loginProc.jsp">
<table border="1">
<tr>
<td>id</td>
<td> <input name="id" value="aaa">
</td>
</tr>
<tr>
<td>pwd</td>
<td><input name="pwd" value="1234"></td>
</tr>
<tr>
<td align="center" colspan="2">
<INPUT TYPE="submit" value="로그인">
</td>
</tr>
</table>
</form>
<%}%>
</div>
</body>
<!-- guestbook/logout.jsp -->
<%@page contentType="text/html;charset=UTF-8"%>
<%
session.invalidate();
%>
<script>
alert('로그아웃 되었습니다.');
location.href="login.jsp";
</script>
<!-- guestbook/loginProc.jsp -->
<%@page contentType="text/html;charset=UTF-8"%>
<jsp:useBean id="mgr" class="guestbook.GuestBookMgr"/>
<jsp:useBean id="login" class="guestbook.JoinBean" scope="session"/>
<!-- login.jsp 요청한 id,pwd 저장 -->
<jsp:setProperty property="*" name="login"/>
<%
String url = "login.jsp";
boolean result = mgr.loginJoin(login.getId(), login.getPwd());
String msg = "로그인 실패";
if(result){
msg = "로그인 성공";
login = mgr.getJoin(login.getId());
session.setAttribute("idKey", login.getId());
session.setAttribute("login", login);
}
%>
<script>
alert("<%=msg%>");
location.href="<%=url%>";
</script>