jsp 용기 초기화 클래스

1446 단어 jspWebservlet
	web        
		in jsp:  	this.getServletContext().getAttribute("user");
			1.   servlet         servletcontext    web     
				public class InitServlet extends HttpServlet {

					@Override
					public void init() throws ServletException {
						System.out.println("initservlet");
						User user=new User();
						this.getServletContext().setAttribute("user",user);
					}
					
				}

				 <servlet>
					<servlet-name>InitServlet</servlet-name>
					<servlet-class>com.bjsxt.drp.servlet.InitServlet</servlet-class>
					<load-on-startup>0</load-on-startup>
				  </servlet>

								

			2.ServletContextListener             ,
				public class ServletInitListener implements ServletContextListener {

					public void contextDestroyed(ServletContextEvent arg0) {
						// TODO Auto-generated method stub

					}

					public void contextInitialized(ServletContextEvent sce) {
						User user=new User();
						sce.getServletContext().setAttribute("user",user);

					}

				}

				<listener>
					<listener-class>com.bjsxt.drp.listener.ServletInitListener</listener-class>
				  </listener>

좋은 웹페이지 즐겨찾기