웹 프로그래밍 14-2(JSP,DB,servlet)-가입, 로그인, 수정 페이지
*로그인
가입과 같이 다음 루틴으로 만들어준다.
login.html => id와 비밀번호를 받아 LoginOk에 넘겨준다.
LoginOk.java(servlet) => 넘겨받은 id를 이용하여 db검색을 한다.
넘겨받은 비밀번호와 db검색한 pw를 비교한다.
맞으면 loginResult.jsp에 id,name,pw,phone값들을 넘겨준다.
틀리면 login.html로 넘겨준다.
loginResult.jsp => 이름을 표시하고 접속한 화면을 보여준다.
-login.html
로그인화면을 보여준다.
loginOk.java(servlet)
받아온 id와 pw를 이용하여 db검색 후 pw 비교하여 넘겨준다.
loginResult.jsp
이름을 표시해주고 접속한 화면을 보여준다.
============================소스===============================
-login.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<!DOCTYPE html> <html> <head> <meta charset="EUC-KR"> <title>Insert title here</title> </head> <body> <h1>로그인 하세요</h1> <br />
<form action="loginOk" method="POST"><br /> 아이디 : <input type="text" name="id" size="10"><br /> 비밀번호 : <input type="password" name="pw" size="10"><br /> <input type="submit" value="로그인"><br /> </form>
<br /> <br /> <a href="join.html">회원가입</a> </body> </html> |
-loginOk.java(servlet)
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
package com.java.homepage;
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 javax.servlet.http.HttpSession;
import java.sql.*; /** * Servlet implementation class loginOk */ @WebServlet("/loginOk") public class loginOk extends HttpServlet {
Connection conn = null; Statement statement = null; ResultSet resultSet = null; String orgin_id; String orgin_pw; String orgin_name; String orgin_phone;
private static final long serialVersionUID = 1L;
/** * @see HttpServlet#HttpServlet() */ public loginOk() { 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 response.getWriter().append("Served at: ").append(request.getContextPath()); }
/** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//utf-8 //request.setCharacterEncoding("EUC-KR"); //response.setContentType("text/html;charset='EUC-KR'");
//post getparameter String post_id = request.getParameter("id"); String post_pw = request.getParameter("pw");
//db String url = "jdbc:oracle:thin:@localhost:1521:clouddb"; String uid = "testid"; String upw = "password"; String query = "select * from member where id='"+post_id+"' and pw='"+ post_pw+"'"; boolean login_b = false;
try { Class.forName("oracle.jdbc.driver.OracleDriver"); conn = DriverManager.getConnection(url,uid,upw); statement = conn.createStatement(); resultSet = statement.executeQuery(query);
while(resultSet.next()) { orgin_id = resultSet.getString("id"); orgin_pw = resultSet.getString("pw"); orgin_name = resultSet.getString("name"); orgin_phone = resultSet.getString("phone"); login_b = true; }
if(login_b) { System.out.println("로그인 성공 하셨습니다."); HttpSession httpSession = request.getSession(); httpSession.setAttribute("name", orgin_name); httpSession.setAttribute("id", orgin_id); httpSession.setAttribute("pw", orgin_pw); httpSession.setAttribute("phone", orgin_phone);
response.sendRedirect("loginResult.jsp"); } else { System.out.println("아이디 또는 비밀번호가 다릅니다."); response.sendRedirect("login.html"); }
}catch(SQLException e) { e.getStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if(conn != null) conn.close(); if(statement != null) statement.close(); if(resultSet != null) resultSet.close(); } catch(Exception e){ e.getStackTrace(); } }
}
}
|
-loginResult.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <!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=EUC-KR"> <title>Insert title here</title> </head> <body>
<h1>회원가입이 되었습니다!</h1><br />
<a href="login.html">로그인</a>
</body> </html> |
'프로그래밍 정리 > JSP, Servlet, DB(oracle)' 카테고리의 다른 글
웹 프로그래밍 15(JSP,DB)-DAO, DTO (1) | 2020.01.03 |
---|---|
웹 프로그래밍 14-3(JSP,DB,servlet)-가입, 로그인, 수정 페이지 (0) | 2020.01.03 |
웹 프로그래밍 14-1(JSP,DB,servlet)-가입, 로그인, 수정 페이지 (0) | 2020.01.03 |
웹 프로그래밍 13(JSP,DB,servlet)-Ojdbc연동 (0) | 2020.01.03 |
웹 프로그래밍 12(DB)-오라클 시작하기 (0) | 2020.01.03 |