본문 바로가기

JAVA/JSP & Servlet

서블릿(servlet)의 라이프 사이클 (getInitParameter, getContextPath)

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<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" metadata-complete="true" version="3.0">

  <display-name>Welcome to Tomcat</display-name>

  <description>Welcome to Tomcat</description>

  <servlet>

    <servlet-name>dbname</servlet-name>

    <jsp-file>/ex02.jsp</jsp-file>

    <init-param>

      <param-name>tablename</param-name>

      <param-value>board1</param-value>

    </init-param>

  </servlet>

  <servlet-mapping>

    <servlet-name>dbname</servlet-name>

    <url-pattern>/dbname</url-pattern>

  </servlet-mapping>

 

    <servlet>

    <servlet-name>servletex01</servlet-name>

    <servlet-class>servlet.ServletEx01</servlet-class>

    <init-param>

      <param-name>tablename</param-name>

      <param-value>board1</param-value>

    </init-param>

  </servlet>

  <servlet-mapping>

    <servlet-name>servletex01</servlet-name>

    <url-pattern>/ServletEx01</url-pattern>

  </servlet-mapping>

 

  <context-param>

        <param-name>dbname</param-name>

        <param-value>oracle</param-value>

  </context-param>

 

</web-app>

 

 

 

 

 

 

 


ServletEx01.java (서블릿)

 

 

 

 

 

package 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 ServletEx01 extends HttpServlet {

        private static final long serialVersionUID = 1L;

 

    public ServletEx01() {

        super();

    }

 

 

        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

               PrintWriter out = response.getWriter();

              

               out.println("<html>");

               out.println("<head>");

               out.println("</head>");

               out.println("<body>");

               out.println("<b>Hello World</b><br>");

               String tablename = getInitParameter("tablename");

               out.println("<b>" + tablename + "</b>");

               out.println("</body>");

               out.println("</html>");

              

        }

 

 

        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

 

        }

}

 

 

 

 

 

 

 

 

 

 

 

 


init 메서드 작성 예제

<%@page import="java.io.PrintWriter"%>

<%@page import="java.math.BigInteger"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%!

        private BigInteger arr[];

 

        //init메서드!!

        public void jspInit(){

               System.out.println("jspInit() 호출");

               arr = new BigInteger[100];

               arr[0] = new BigInteger("1");

               arr[1] = new BigInteger("1");

               for(int cnt = 2 ; cnt < arr.length ; cnt++)

                       arr[cnt] = arr[cnt-2].add(arr[cnt-1]);

        }

%>

<%

        String str = request.getParameter("num");

        int num = Integer.parseInt(str);

       

        if(num > 100) num = 100;

%>

 

 

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>init 메서드 작성 예제</title>

</head>

<body>

<%

        for(int cnt = 0;cnt<num;cnt++){

               out.println(arr[cnt] + " ");

        }

%>

</body>

</html>

 

 

 

실행

 

http://localhost/LifeCycleEx01/ex01.jsp?num=10

 

 

 

 

 

 

 

 

 


web.xml 이용해서 url 가려주기

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

   

<%

        request.setCharacterEncoding("UTF-8");

 

        String tablename = getInitParameter("tablename");   

%>

 

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>JSP</title>

</head>

<body>

 

<h3>web.xml 이용해서 url 가려주기</h3><br>

tablename : <%=tablename %>

 

</body>

</html>

 

 

 

 

 

 

 

 

 

 

 


getInitParameter

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

 

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>JSP</title>

</head>

<body>

 

<%

        String dbname = application.getInitParameter("dbname");

        out.println(dbname);

%>

 

</body>

</html>

 

 

 

 

 

 

 

 

 

 

 

 

 

 


ex05_1.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

 

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>JSP</title>

</head>

<body>

 

<%

        String name = (String)application.getAttribute("name");

        out.println("name = " + name);

        application.setAttribute("name", "홍길동");

%>

 

</body>

</html>

 

 

ex05_2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

 

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>JSP</title>

</head>

<body>

 

<%

        String name = (String)application.getAttribute("name");

        out.println("name = " + name);

        application.setAttribute("name", "박문수");

%>

 

</body>

</html>

 

 

실행순서

ex05_1.jsp  ->  ex05_2.jsp  ->  ex05_1.jsp

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


getContextPath

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

 

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>JSP</title>

</head>

<body>

 

<%

        String path1 = application.getContextPath();

        out.println("경로1 : " + path1 + "<br>");

       

        String path2 = application.getRealPath("ex05.jsp");

        out.println("경로2 : " + path2 + "<br>");

%>

 

</body>

</html>