import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//ServletContext를 이용한 데이터 공유
// 여러개의 Servlet에서 데이터를 공유해야 할 경우에 context parameter를 사용한다.
// web.xml파일에 데이터를 작성하면, Servlet에서 공유할 수 있다.
// 순서
// Servlet클래스 제작
// web.xml파일에 context parameter 기술
// ServletContext메소드를 이용해서 데이터를 사용한다.
// getServletContext() : ServletContext를 얻어오는 메소드
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>servlet01</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>id</param-name>
<param-value>test</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>1234</param-value>
</context-param>
<context-param>
<param-name>local</param-name>
<param-value>buchaen</param-value>
</context-param>
</web-app>
---------------------------------------------------------------------------------------------------------------
ServletContext_Ex.java
package com.test.ex;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//ServletContext를 이용한 데이터 공유
// 여러개의 Servlet에서 데이터를 공유해야 할 경우에 context parameter를 사용한다.
// web.xml파일에 데이터를 작성하면, Servlet에서 공유할 수 있다.
// 순서
// Servlet클래스 제작
// web.xml파일에 context parameter 기술
// ServletContext메소드를 이용해서 데이터를 사용한다.
// getServletContext() : ServletContext를 얻어오는 메소드
/**
* Servlet implementation class ServletContext_Ex
*/
@WebServlet(name = "SCEx", urlPatterns = { "/SCEx" })
public class ServletContext_Ex extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ServletContext_Ex() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = getServletContext().getInitParameter("id");
String pw = getServletContext().getInitParameter("password");
String local = getServletContext().getInitParameter("local");
// response.setContentType("text/html; charset=euc-kr");
// PrintWriter out = response.getWriter();
System.out.println("id : "+id);
System.out.println("pw :"+pw);
System.out.println("local : "+local);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
'기존카테고리 > JSP' 카테고리의 다른 글
[회원가입1]join.html, joinOK.java (0) | 2017.06.06 |
---|---|
웹어플리케이션을 감시하는 리스너 ServletContextListener(contextInitialized(), contextDestroy()) (0) | 2017.06.06 |
context path, web.xml을 이용한 서블릿 초기화 (0) | 2017.06.06 |
서블릿 한글처리 (0) | 2017.06.06 |
서블릿 form 전송과 결과값 출력 (0) | 2017.06.06 |