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.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>
<%@ 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>
'JAVA > JSP & Servlet' 카테고리의 다른 글
익스프레션 언어(expression language) 기본[requestScope ,pageScope ] (0) | 2013.03.21 |
---|---|
JSP 변수의 범위 (0) | 2013.03.20 |
JSP 예외처리(Exception) 방법 3가지 (0) | 2013.03.20 |
세션(session) URL 재작성 (0) | 2013.03.20 |
쿠키(cookie) & 세션(session) (1) | 2013.03.20 |