package org.ph.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class BoardServlet
*/
public class BoardServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public BoardServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
*/
// @CheckLogin("login")
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("service run..................");
}
}
package org.ph.web;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet Filter implementation class CookieFilter
*/
public class CookieFilter implements Filter {
/**
* Default constructor.
*/
public CookieFilter() {
// TODO Auto-generated constructor stub
}
/**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
// place your code here
HttpServletRequest req = (HttpServletRequest)request;
HttpServletResponse res = (HttpServletResponse)response;
Cookie[] cookies = req.getCookies();
if(cookies==null||cookies.length==0){
System.out.println("no cookie.....");
res.sendRedirect("noCookie.jsp");
return;
}
for (Cookie cookie : cookies) {
if(cookie.getName().equals("login")){
System.out.println("logined user");
chain.doFilter(request, response);
return;
}
}
res.sendRedirect("noCookie.jsp");
// pass the request along the filter chain
// chain.doFilter(request, response);
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
package org.ph.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class LoginServlet
*/
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
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
// Cookie[] cookies = request.getCookies();
// if(cookies==null||cookies.length==0){
// System.out.println("Login Servlet...........no cookie");
//
// HttpSession session = request.getSession();
// session.setAttribute("login", "peno");
// return;
// }
// for (Cookie cookie : cookies) {
// System.out.println("Login Servlet..."+cookie.getName()+":"+cookie.getValue());
// System.out.println("-------------------------------------");
// }
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Loginservlet Post....");
Cookie ck=new Cookie("login", "PENO LOGIN CK");
ck.setMaxAge(60*5);
response.addCookie(ck);
}
}
'코딩 > WEB' 카테고리의 다른 글
Web : 글 보기 게시판에 댓글 보는 기능 붙여주기 (ajax+JSON) (0) | 2014.11.06 |
---|---|
Spring - Transaction 예제 (0) | 2014.10.17 |
Spring 설정 -- 3 AOP 설정 (0) | 2014.10.15 |
Spring 설정 -- 1 (0) | 2014.10.14 |
Spring 설정 -- 2 Mybatis 연동하기 (0) | 2014.10.14 |