[JSP/Servlet] Annotation
- BACKEND/Servlet&JSP
- 2017. 5. 30. 09:40
package hello;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
* <servlet> : @WebServlet - 서블릿 클래스에 선언
* <servlet-name> - name속성
* <servlet-class> - 따로 설정 필요 없다
* <servlet-mappint>
* <servlet-name>
* <url-pattern> - value 속성 또는 urlPatterns 속성 : String배열
*
* *Annotation 속성의 값의 타입이 배열일때 - 배열 값 : {값, 값, 값}, 값을 한개만 설정할 경우 리터럴로 표시
*/
//@WebServlet(name="hello", value="/hello") <- name 생략 가능
//@WebServlet(value="/hello") <- value속성만 있을때 생략 가능
//@WebServlet("/hello")
@WebServlet(value="/hello", loadOnStartup=1, initParams=@WebInitParam(name="email",value="a@a.com"))
public class HelloServlet extends HttpServlet{
public void init(){
System.out.println("HelloServlet.init()실행");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("HelloServlet");
String email = getServletConfig().getInitParameter("email");
System.out.println("초기파라미터:"+email);
req.getRequestDispatcher("/index.jsp").forward(req, resp);
}
}
<%@ page contentType="text/html;charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="/Servlet3/hello">hello 서블릿</a><br>
<a href="/Servlet3/hello2">hello 서블릿</a><br>
<a href="/Servlet3/HelloServlet2">hello 서블릿</a>
</body>
package hello;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class HelloServlet2
*/
@WebServlet({ "/HelloServlet2", "/hello2" })
public class HelloServlet2 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("HelloServlet2");
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
}
package filter;
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.annotation.WebFilter;
/**
* Servlet Filter implementation class EncodingFilter
*/
@WebFilter("/*")
public class EncodingFilter implements Filter {
/**
* Default constructor.
*/
public EncodingFilter() {
// 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 {
request.setCharacterEncoding("UTF-8");
chain.doFilter(request, response);
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
package listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
/**
* Application Lifecycle Listener implementation class MyListener
*
*/
@WebListener
public class MyListener implements ServletContextListener {
/**
* Default constructor.
*/
public MyListener() {
// TODO Auto-generated constructor stub
}
/**
* @see ServletContextListener#contextDestroyed(ServletContextEvent)
*/
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}
/**
* @see ServletContextListener#contextInitialized(ServletContextEvent)
*/
public void contextInitialized(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}
}
'BACKEND > Servlet&JSP' 카테고리의 다른 글
[JSP/Servlet] 서블릿 정리 (0) | 2017.12.15 |
---|---|
[JSP/Servlet] FileUpload 개념 파일 (0) | 2017.06.16 |
[JSP/Servlet] Fileter (0) | 2017.05.19 |
[JSP/Servlet] 초기파라미터를 제공하는 EL 예제 (0) | 2017.05.17 |
[JSP/Servlet] JSTL (0) | 2017.05.17 |