일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 이클립스
- nodejs
- Android
- Java
- 컬렉션프레임워크
- JDBC
- python
- API
- 단축키
- 대덕인재개발원
- Oracle
- ddit
- 맥
- html
- 자바문제
- Mac
- 생활코딩
- ibatis
- 배열
- FastAPI
- Homebrew
- spring
- crud
- pyqt
- Error
- jsp
- servlet
- 자바
- 객체지향
- 반복문
Archives
- Today
- Total
romworld
Cookie 2 ( login) 본문
cookieMain.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>Cookie 연습용 Main페이지 입니다.</h2>
<a href="<%=request.getContextPath()%>/basic/cookie/cookieLogin.jsp">Login창으로 이동</a>
</body>
</html>
cookieLogin.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login 창</title>
<style>
table {
border : 1px solid black;
padding : 15px;
margin : 10px auto;
}
</style>
<%
// 서블릿에서 저장한 쿠키값을 가져와 처리하기
String cookieUserId = ""; // 쿠키에 저장된 userid값이 저장될 변수
String checkOK = "";
Cookie[] cookies = request.getCookies();
if(cookies!=null){
for(Cookie cookie : cookies){
if("USERID".equals(cookie.getName())){ // 해당 쿠키가 있을 때...
cookieUserId = cookie.getValue(); // 쿠키값 구하기 (userid값 구하기)
checkOK = "checked";
}
}
}
%>
</head>
<body>
<form action="<%=request.getContextPath() %>/cookieLoginServlet.do" method="post">
<table>
<tr>
<td>ID : </td>
<td><input type="text" name="userid" value="<%=cookieUserId %>" placeholder="ID를 입력하세요"></td>
</tr>
<tr>
<td>PASS : </td>
<td><input type="password" name="pass" placeholder="Password를 입력하세요."></td>
</tr>
<tr>
<td colspan="2">
<input type="checkbox" name="chkid" value="check" <%=checkOK %>>id 기억하기
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center">
<input type="submit" value="Login">
</td>
</tr>
</table>
</form>
</body>
</html>
CookieLoginServlet
package kr.or.ddit.basic.cookie;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/cookieLoginServlet.do")
public class CookieLoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
// userid, pass, chkid 정보 받아오기
String userId = request.getParameter("userid"); // id
String pass = request.getParameter("pass"); // password
String chkId = request.getParameter("chkid"); // checkbox
// Cookie객체 생성 ==> 쿠키이름: USERID
Cookie idCookie = new Cookie("USERID", userId);
// 체크박스의 체크 여부에 따라 쿠키를 저장하거나 삭제한다.
// if(chkId != null) { // 체크박스가 체크되었을 때...
// // 쿠키 저장하기
// response.addCookie(idCookie);
// }else {
// // 쿠키 삭제하기
// idCookie.setMaxAge(0); // 유지시간을 0으로
// response.addCookie(idCookie);
// }
if(chkId==null) {
idCookie.setMaxAge(0);
}
response.addCookie(idCookie);
// 로그인 성공 여부 검사
if("test".equals(userId) && "1234".equals(pass)) { // 로그인 성공
response.sendRedirect(request.getContextPath() + "/basic/cookie/cookieMain.jsp");
}else { // 로그인 실패
response.sendRedirect(request.getContextPath() + "/basic/cookie/cookieLogin.jsp");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
'JAVA > 개념정리' 카테고리의 다른 글
Ajax ( JSON) (1) | 2022.12.06 |
---|---|
Session (0) | 2022.12.06 |
Cookie (0) | 2022.12.06 |
Servlet 3 ( request, response, forward,redirect) (1) | 2022.12.06 |
Servlet 2 (0) | 2022.12.06 |
Comments