서블릿 컨텍스트 영역에 값 넣기 예제

 << 서블릿의 영역(scope) ** >>

  1. ServletContext영역

  - 모든 서블릿이 공유하는 영역

  - 하나의 웹어플리케이션당 하나가 만들어짐.


  2. ServletConfig영역

  - 서블릿 하나당 할당되는 영역



1. web.xml

2. main.html

3. MyServlet.java

4. ServletScope.java

5. ServletScope1.java

6. TestServlet.java




 web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

  <display-name>day02_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>

  <servlet>

  <servlet-name>myservlet</servlet-name>

  <servlet-class>test.servlet.MyServlet</servlet-class>

  </servlet>

  <servlet-mapping>

  <servlet-name>myservlet</servlet-name>

  <url-pattern>/my.do</url-pattern>

  </servlet-mapping>

  <!-- 서블릿 컨텍스트 영역에 값 넣기 -->

  <context-param>

  <param-name>user</param-name>

  <param-value>scott</param-value>

  </context-param>

  

  <servlet>

  <servlet-name>scope1</servlet-name>

  <servlet-class>test.servlet.ServletScope1</servlet-class>

  <!-- 초기화 파라미터 설정하기(서블릿컨피그를 통해 사용가능함.!!) -->

  <init-param>

  <param-name>id</param-name>

  <param-value>song</param-value>

  </init-param>

  </servlet>

  <servlet-mapping>

  <servlet-name>scope1</servlet-name>

  <url-pattern>/scope1.do</url-pattern>

  </servlet-mapping>

</web-app>



2. main.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">

<title>Insert title here</title>

</head>

<body>

<a href="my.do">myservlet</a><br/>

<a href="scope.do">scope</a><br/>

<a href="test.do">test</a><br/>

</body>

</html>








3. MyServlet.java

; myservlet 클릭하면

package test.servlet;


import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


public class MyServlet extends HttpServlet{

//서블릿이 최초로 요청될때 딱한번만 호출됨.(초기화작업)

@Override

public void init() throws ServletException {

System.out.println("init메소드가 호출됨!!");

}

//서블릿이 종료될때 ( 메모리에서 제거될때 ) 호출됨.(자원해제등, .. )

//일정시간 동안 서블릿이 호출되지 않거나 웹서버가 종료되면 서블릿은 제거됨.

@Override

public void destroy() {

System.out.println("destroy");

}

// 서블릿을 요청할때마다 호출됨.

@Override

protected void service(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, IOException {

resp.setContentType("text/html;charset=euc-kr");

PrintWriter pw = resp.getWriter();

pw.println("<h2>service메소드 호출!</h2>");

pw.close();

}

}



4. ServletScope.java

package test.servlet;


import java.io.IOException;

import java.io.PrintWriter;


import javax.servlet.ServletContext;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


@WebServlet("/scope.do")

public class ServletScope extends HttpServlet{

@Override

protected void service(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, IOException {


//서블릿컨텍스트 객체 얻어오기

ServletContext sc = getServletContext();


//서블릿컨텍스트 영역에 값넣기

sc.setAttribute("url", "localhost");


//서블릿컨텍스트 영역에서 값 꺼내오기

String url=(String)sc.getAttribute("url");


//web.xml에 설정된 초기화파라미터 읽어오기.

String user = sc.getInitParameter("user");

resp.setContentType("text/html;charset=euc-kr");

PrintWriter pw = resp.getWriter();

pw.println("url:" + url);

pw.println("user:" + user);

pw.println("<a href='test.do'>testservlet</a><br/>");

pw.close();

}

}



; user에 scott를 보여준다.(xml에서 읽기)



5. ServletScope1.java

package test.servlet;


import java.io.IOException;

import java.io.PrintWriter;


import javax.servlet.ServletConfig;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


public class ServletScope1 extends HttpServlet{

@Override

protected void service(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, IOException {


//서블릿컨피그 객체 얻어오기.

ServletConfig sc = getServletConfig();

String id = sc.getInitParameter("id");


resp.setContentType("text/html;charset=euc-kr");

PrintWriter pw = resp.getWriter();

pw.println("id:" + id);

pw.close();

}

}







6. TestServlet.java

package test.servlet;


import java.io.IOException;

import java.io.PrintWriter;


import javax.servlet.ServletContext;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


@WebServlet("/test.do")

public class TestServlet extends HttpServlet {

@Override

protected void service(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, IOException {


ServletContext sc=getServletContext();

String url=(String)sc.getAttribute("url");


resp.setContentType("text/html;charset=euc-kr");

PrintWriter pw = resp.getWriter();

pw.println("서블릿컨텍스트에 담긴 url:" + url);

pw.close();

}

}






'WEB > JSP' 카테고리의 다른 글

9. JSP - Servlet 파라미터 받기  (0) 2013.04.18
8. 한글 처리  (0) 2013.04.18
6. Servlet - connection pool  (0) 2013.04.18
5. Servlet예제 - doGet, doSet  (0) 2013.04.18
4. JSP - Servlet 예제  (0) 2013.04.18

+ Recent posts