패키지 com.shop.model;
LoginDAO.java & UserVo.java & LoginServlet
LoginDAO.java
package com.shop.model;
import java.sql.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.*;
public class LoginDAO {
// Tomcat Context 로부터 jdbc/oracle 이름의 DataSource 를 가져와서 DB CONNECTION 객체를 생성해서 리턴
public Connection dbCon(){
Connection con = null;
try{
Context ctx = new InitialContext(); // 톰캣에 namingContext가 있다. 자원을 가져오기 위해서는
Context tomcat = (Context)ctx.lookup("java:comp/env"); // 톰캣에 있는 컨텍스를 가져온다.
DataSource ds = (DataSource)tomcat.lookup("jdbc/oracle"); // 데이터 소스는 톰캣이 시작할 때 생성한다. maxidle 최소 10개 생성.
// server.xml에서 확인한다.
con = ds.getConnection(); // 데이터 소스로부터 url, passwd를 가져온다.
}catch(Exception e){
e.printStackTrace();
}
return con;
}
public void dbClose(Connection con, Statement stat, ResultSet rs){
try{
if(rs!=null)rs.close();
if(stat!=null)stat.close();
if(con!=null)con.close();
}catch(Exception e){
e.printStackTrace();
}
} // dbClose() end
public UserVo loginProc(String uid, String pwd){
Connection con = null;
PreparedStatement stat = null;
ResultSet rs = null;
String sql = "select * from userinfo where userid=? and passwd=?";
UserVo vo = null;
try{
con = dbCon();
stat = con.prepareStatement(sql);
stat.setString(1,uid);
stat.setString(2,pwd);
rs = stat.executeQuery();
while(rs.next()){
vo.setAddress(rs.getString("classees"));
vo.setUserid(uid);
vo.setPasswd(pwd);
vo.setBirth(rs.getDate("birth"));
vo.setBlog(rs.getString("blog"));
vo.setEmail(rs.getString("emailk"));
vo.setGender(rs.getString("gender").charAt(0));
vo.setSms(rs.getString("sms").charAt(0));
vo.setLetter(rs.getString("letter").charAt(0));
vo.setPhone(rs.getString("phone"));
vo.setMobile(rs.getString("mobile"));
vo.setUsername(rs.getString("username"));
} // while end
}catch(Exception e){
e.printStackTrace();
}finally{
dbClose(con, stat, rs);
}
return vo;
}//loginCheck()
}
UserVo.java
package com.shop.model;
import java.sql.*;
public class UserVo {
private String userid;
private String passwd;
private String username;
private String address;
private char gender;
private Date birth;
private String email;
private String phone;
private String mobile;
private String blog;
private String interest;
private char letter;
private char sms;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getBlog() {
return blog;
}
public void setBlog(String blog) {
this.blog = blog;
}
public String getInterest() {
return interest;
}
public void setInterest(String interest) {
this.interest = interest;
}
public char getLetter() {
return letter;
}
public void setLetter(char letter) {
this.letter = letter;
}
public char getSms() {
return sms;
}
public void setSms(char sms) {
this.sms = sms;
}
}
LoginServlet.java
package com.shop.model;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/Login.do")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//요청 처리후 응답을 위한 마임타입과 문자셋 설정
response.setContentType("text/html;charset=utf-8");
//응답 결과를 클라이언트에게 보내기 위해서 문자스트림을 생성
PrintWriter out = response.getWriter();
//파라미터 받아오기
ServletContext sc = getServletContext();
RequestDispatcher rd = null;
String uid = request.getParameter("userid");
String pwd = request.getParameter("passwd");
HttpSession session = request.getSession();
LoginDAO dao = new LoginDAO();
}
}
join1.html (소스는 참고 사이트에서 가져와서 약간의 수정을 했으므로 공개하지 않습니다.)