웹 프로그래밍 14-3(JSP,DB,servlet)-가입, 로그인, 수정 페이지
*수정 페이지
루틴은 앞쪽에 배운것과 같다
modify.jsp
세션으로 넘겨온 id,pw를 가지고 db검색한다.
검색 결과를 input value에 넣어 이름과 전화번호, 성별을 먼저 입력해둔다.※필자는 안했다!!(당당)
비밀번호는 세션pw와 디비pw를 비교한다. 맞으면 수정완료 틀리면 login.html로 보내진다.
modifyOk.java(servlet)
값을 넘겨받아 update문을 작성 후 실행
값이 수정되었으면 modifyResult.jsp로 이동
수정된 값이 없으면 다시 modfiy.jsp로 이동
modifyResult.jsp
수정이 정상적으로 처리되었다고 출력
다시 수정하기와 로그아웃 링크를 달아둔다.
logout.jsp
세션을 끝낸다.
login.html로 갈 수 있는 링크를 달아둔다.
================================소스==================================
modify.jsp
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 |
<%@page import="java.sql.*"%> <%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<%! //세션으로 넘겨온 아이디 String id;
//쿼리로 나온 정보 String name; String pw; String phone;
//쿼리 String query;
//DB 연결정보 Connection conn = null; Statement stm = null; ResultSet res = null; %> <!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>
<% id = (String)session.getAttribute("id");
query = "select * from member where id='"+id+"'";
try{ Class.forName("oracle.jdbc.driver.OracleDriver"); conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:clouddb","testid","password"); stm = conn.createStatement(); res = stm.executeQuery(query);
while(res.next()) { name = res.getString("name"); phone = res.getString("phone");
} }catch(Exception e) { e.getStackTrace(); } %> <form action="modifyOk" method="POST"> 아이디 : <%=id %><br /> 비밀번호 : <input type="password" name="pw" size = "20"> <br /> 이름 : <input type="text" name="name" size = "10" value="<%= name%>"> <br /> 전화번호 : <select name="num1"> <option value="">----선택----</option> <option value="010">010</option> <option value="011">011</option> <option value="017">017</option> <option value="019">019</option> </select> <input type="text" name="num2" size="10"> <input type="text" name="num3" size="10"><br />
성별 : <br /> 남 <input type="radio" name="se" value="man"> 여 <input type="radio" name="se" value="여"> <br /> <input type="submit" value="수정하기"><br /> <input type="reset" value="초기화"><br />
</form> </body> </html> |
modifyOk.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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
package com.java.homepage;
import java.io.IOException; import java.io.UnsupportedEncodingException;
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 modifyOk */ @WebServlet("/modifyOk") public class modifyOk extends HttpServlet {
//db connect Connection conn = null; Statement sta = null; HttpSession hs = null;
String name; String id; String pw; String num1; String num2; String num3;
private static final long serialVersionUID = 1L;
/** * @see HttpServlet#HttpServlet() */ public modifyOk() { 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 action(request,response); }
/** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub action(request,response); }
private void action(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("EUC-KR"); //response.setContentType("text/html;charset='EUC-KR'");
name = request.getParameter("name"); pw = request.getParameter("pw"); num1 = request.getParameter("num1"); num2 = request.getParameter("num2"); num3 = request.getParameter("num3");
hs = request.getSession(); id = (String)hs.getAttribute("id");
String driver = "oracle.jdbc.driver.OracleDriver"; String url = "jdbc:oracle:thin:@localhost:1521:clouddb"; String uid = "testid"; String upw = "password"; String query_update = "update member set pw='"+ pw + "', name='" + name + "', phone='" + num1+"-"+num2+"-"+num3+"' where id='"+id+"'";
if(pwConfirm()) {
try{ System.out.println("try 시작"); Class.forName(driver); conn = DriverManager.getConnection(url,uid,upw); sta = conn.createStatement(); int i= sta.executeUpdate(query_update);
if(i==1) { System.out.println("성공적으로 수정 되었습니다."); response.sendRedirect("modifyResult.jsp"); } else { System.out.println("수정 실패."); response.sendRedirect("modify.jsp"); }
} catch(Exception e) { e.getStackTrace(); } finally { try { if(conn != null) conn.close(); if(sta != null) sta.close(); } catch(Exception e) { e.getStackTrace(); } } }else { System.out.println("비밀번호가 틀립니다."); response.sendRedirect("login.html"); }
}
private boolean pwConfirm() { boolean result=false;
String sessionpw = (String)hs.getAttribute("pw");
if(pw.equals(sessionpw)) { result=true; } else { result=false; }
return result; }
}
|
modifyResult.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<%@ 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>
<a href="modify.jsp">다시 수정하기</a> <a href="logout.jsp">로그아웃</a> </body> </html> |
logout.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<%@ 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> <% session.invalidate(); %>
로그아웃 되었습니다!!
<br /> <br /> <a href="login.html">메인으로</a> </body> </html> |
'프로그래밍 정리 > JSP, Servlet, DB(oracle)' 카테고리의 다른 글
웹 프로그래밍 16(JSP,DB)-PreparedStatement (0) | 2020.01.05 |
---|---|
웹 프로그래밍 15(JSP,DB)-DAO, DTO (1) | 2020.01.03 |
웹 프로그래밍 14-2(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 |