챕터11 수정한 로그인!!
#--loginFb.jsp--#
<%@ page contentType="text/html;charset=utf-8" %>
<%@ taglib uri="struts/bean" prefix="bean" %>
<%@ taglib uri="struts/html" prefix="html" %>
<html>
<body>
<html:errors/>
<html:form action="/loginFb" focus="id">
<bean:message key="prompt.id"/>: <html:text property="id"/><br>
<bean:message key="prompt.passwd"/>: <html:password property="passwd"/><br>
<html:submit>
<bean:message key="button.submit"/>
</html:submit>
</html:form>
</body>
</html>
#--admin.jsp--#
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ taglib uri="jstl/core" prefix="c" %>
<%@ page import="shop.login.*" %>
<%
// String[] name=(String[])request.getAttribute("userInfo");
// for(int i=0; i<name.length; i++)
// {
// out.println(name[i]);
// }
String id = (String)session.getAttribute("userInfo.id");
out.println("[request] id : "+id+"<br>");
String name = (String)request.getAttribute("userInfo.name");
out.println("[request] name : "+name+"<br>");
UserInfoBean user = new UserInfoBean();
out.println("[UserInfoBean] user : "+user.getId()+"<br>");
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>admin</title>
</head>
<body><c:when test="${empty userInfo}"></c:when>
</body>
</html>
#--LoginFbAction.java--#
package itexpert.chap11;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;
import org.apache.commons.beanutils.PropertyUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.log4j.Logger;
import shop.login.UserInfoBean;
import shop.util.DBResource;
import shop.util.ShopConstant;
import exception.ShopDAOException;
import org.apache.log4j.Logger;
public class LoginFbAction extends Action {
Logger log = Logger.getLogger(LoginFbAction.class);
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)throws ShopDAOException
{
String next = "main"; // forward 페이지
String id = request.getParameter("id");
String passwd = request.getParameter("passwd");
DBResource db = new DBResource();
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
// 폼빈에서 id와 passwd를 가져온다.
log.debug("ID : "+id + "/ PW :" + passwd); // 디버그를 위한 로그를 남긴다.
if (id.equals("") || passwd.equals("")) {
return null;
}
String sql = "SELECT name, pass, address, bn_name, bn_type, sa_num, lv, tel1, email, bank_name, accunt, y_n, country, sdate FROM USER_ID WHERE id=?";
con = db.getConnection(ShopConstant.SHOP_DATASOURCE);
pstmt = con.prepareStatement(sql);
pstmt.setString(1, id);
rs = pstmt.executeQuery();
if (rs.next()) {
// 데이타베이스에 있는 것과 입력한 패스워드가 일치하는지 확인
if (rs.getString("pass").equals(passwd)) {
UserInfoBean userInfo = new UserInfoBean();
userInfo.setId(id);
userInfo.setName(rs.getString("name"));
userInfo.setAdd(rs.getString("address"));
userInfo.setBn_name(rs.getString("bn_name"));
userInfo.setBn_type(rs.getString("bn_type"));
userInfo.setSa_num(rs.getString("sa_num"));
userInfo.setLv(rs.getString("lv"));
userInfo.setTel(rs.getString("tel1"));
userInfo.setEmail(rs.getString("email"));
userInfo.setBank_name(rs.getString("bank_name"));
userInfo.setAccunt(rs.getString("accunt"));
userInfo.setY_n(rs.getString("y_n"));
userInfo.setCountry(rs.getString("country"));
HttpSession session = request.getSession();
// session 영역에 UserInfoBean 객체를 저장한다.
session.setAttribute("userInfo", userInfo);
if(userInfo.getY_n().equals("z"))
{
next = "admin";
}
else
{
next = "main";
}
} else {
next = "error";
}
} else {
next = "error";
log.debug("Else checkResult: " + next);
}
} catch (Exception e) {
log.error(e.toString());
throw new ShopDAOException(e.getMessage());
} finally {
db.close(rs, pstmt, con);
}
return mapping.findForward(next);
}
}
#--struts-config.xml--#
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<!-- Form Bean Definitions -->
<form-beans>
<form-bean name="loginForm" type="itexpert.chap11.LoginForm" />
</form-beans>
<!-- Global Forward Definitions -->
<global-forwards>
<forward name="login" path="/login.jsp" />
<forward name="main" path="/main.jsp" />
<forward name="error" path="/error.jsp" />
</global-forwards>
<!-- Action Mapping Definitions -->
<action-mappings>
<action path="/login" type="itexpert.chap11.LoginAction" scope="request" input="login.jsp">
<forward name="admin" path="/admin.jsp" />
</action>
<action path="/main" forward="/main.jsp" />
<action path="/loginFb" type="itexpert.chap11.LoginFbAction" name="loginForm" scope="request"
input="/loginFb.jsp">
<forward name="admin" path="/admin.jsp" />
</action>
</action-mappings>
<message-resources parameter="itexpert.chap11.message.ApplicationResources" />
</struts-config>
# Server : OC4J
# 도구 : 이클립스 3.2
인데 loginFb.do 로 해서 id와 pw 맞춰서 admin.jsp 로
넘어가는 건 되는데! 세션정보 즉 admin 정보를 갖고오지 못하네요 ㅠㅠ;;
tld는 오류나고.. sessionScope는 먹히도 않고 ㅠㅠ;;;
고수님들 제발 !! 도와주십시오! ㅠ_ㅠ..