공부하는 블로그입니다!
궁금한 점은 댓글을 달아주세요!
(JSP) 3.7일 공부한 내용 / request, forward, cookie
###jsp request
request 관련 객체 메소드
getContextPath() : 웹어플리케이션의 컨텍스트 패스를 얻습니다.
getMethod() : get방식과 post방식을 구분할 수 있습니다.
getSession() : 세션 객체를 얻습니다.
getProtocol() : 해당 프로토콜을 얻습니다.
getRequestURL() : 요청 URL를 얻습니다.
getRequestURI() : 요청 URI를 얻습니다.
getQueryString() : 쿼리스트링을 얻습니다.
parameter관련 메소드
getParameter(String name) : name에 해당하는 파라미터 값을 구함.
getParameterNames() : 모든 파라미터 이름을 구함.
getParameterValues(String name) : name에 해당하는 파라미터값들을 구함.
response 객체 관련 메소드
getCharacterEncoding() : 응답할때 문자의 인코딩 형태를 구합니다.
addCookie(Cookie) : 쿠키를 지정 합니다.
sendRedirect(URL) : 지정한 URL로 이동합니다.
액션태그
forward, include, param 태그 살펴보기
jsp01.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>jsp01</title>
</head>
<body>
//jsp02.jsp로 포워딩
<jsp:forward page="jsp02.jsp">
<jsp:param name="id" value="abcd"></jsp:param>
<jsp:param name="pw" value="1111"></jsp:param>
</jsp:forward>
</body>
</html>
jsp02.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>jsp2</title>
</head>
<body>
<%!
String id,pw;
%>
<% //포워딩하면서 param으로 받은 value를 request로 받을 수 있다.
id = request.getParameter("id");
pw = request.getParameter("pw");
%>
id = <%=id%> </br>
pw = <%=pw%>
</body>
</html>
Cookie
관련메소드
setMaxAge() : 쿠키 유효기간을 설정 합니다.
setpath() : 쿠키사용의 유효 디렉토리를 설정 합니다.
setValue() : 쿠키의 값을 설정 합니다.
setVersion() : 쿠키 버전을 설정 합니다.
getMaxAge() : 쿠키 유효기간 정보를 얻습니다.
getName() : 쿠키 이름을 얻습니다.
getPath() : 쿠키사용의 유효 디렉토리 정보를 얻습니다.
getValue() : 쿠키의 값을 얻습니다.
getVersion() : 쿠키 버전을 얻습니다.
코드
jsp_create.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>cookie</title>
</head>
<body>
<%
String id = request.getParameter("id");
String pw = request.getParameter("pw");
if (id.equals("abcd")) {
Cookie c = new Cookie("id", id);
c.setMaxAge(60);
response.addCookie(c);
response.sendRedirect("cookie_show.jsp");
}
%>
</body>
</html>
jsp_show.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
Cookie[] c = request.getCookies();
for (int i=0;i<c.length;i++){
out.println("name = "+ c[i].getValue());
}
%>
<<a href="cookie_del.jsp">ㄱㄱㄹ</a>
</body>
</html>
jsp_del.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
Cookie[] c = request.getCookies();
for(int i=0;i<c.length;i++){
if(c[i].getValue().equals("abcdjs")){
c[i].setMaxAge(0);
response.addCookie(c[i]);
}
}
%>
</body>
</html>
cookie.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="cookie_create.jsp">
<input type="text" name="id">
<input type="text" name="pw">
<input type="submit" value="확인">
</form>
</body>
</html>