Data Source Apache Tomcat Data Source METAINFcontext xml

  • Slides: 8
Download presentation

Настройка Data. Source в Apache Tomcat Настройка Data. Source: Файл /META-INF/context. xml <? xml

Настройка Data. Source в Apache Tomcat Настройка Data. Source: Файл /META-INF/context. xml <? xml version="1. 0" encoding="UTF-8" ? > <!-- контекст web-приложения - префикс всех URL для данного приложения --> <Context path="/websample"> <!-- Объявление ресурса-источника данных --> <Resource auth="Container" type="javax. sql. Data. Source" driver. Class. Name="oracle. jdbc. driver. Oracle. Driver max. Active="20" max. Idle="10" max. Wait="-1" name="jdbc/sample" url="jdbc: oracle: thin: @: 1521: spm" username="o 50" password="o 50" /> </Context> Использование Data. Source: // Создаем начальный контекст JNDI (Java Naming Directory) Initial. Context ctx = new Initial. Context(); // Достаем из контекста источник данных Data. Source ds = (Data. Source)ctx. lookup("java: comp/env/jdbc/sample"); // Получаем соединение с БД из источника данных return ds. get. Connection();

J 2 EE: Сервлеты Пример: public class My. Servlet extends javax. servlet. http. Http.

J 2 EE: Сервлеты Пример: public class My. Servlet extends javax. servlet. http. Http. Servlet { protected void service(Http. Servlet. Request request, Http. Servlet. Response response) throws Servlet. Exception, IOException {} public void destroy() { this. log("Servlet destroyed"); } public void init(Servlet. Config cfg) throws Servlet. Exception { this. log("Servlet inited"); } }

J 2 EE: Java Server Pages (JSP) <%@ page language="java" content. Type="text/html; charset=UTF-8" page.

J 2 EE: Java Server Pages (JSP) <%@ page language="java" content. Type="text/html; charset=UTF-8" page. Encoding="UTF-8" %> <%@ page import="java. util. *" %> <%! // Объявляется поле в классе страницы int my_integer_field = 777; // Объявляется метод в классе страницы private String make_greeting(String name) { return "Hello, "+name + "!"; } %> <html> <head> <title>Sample Hello world page</title> </head> <body> <% // Скриптлет 1 for (int i=0; i<10; i++) { %> <h 1> <%= /* Вывод в поток out */ make_greeting("World "+i) %> </h 1> <% // Скриптлет 2 } // Конец цикла %> </body> </html> import java. util. *; public final class hello_jsp extends org. apache. jasper. runtime. Http. Jsp. Base { // Объявляется поле в классе страницы int my_integer_field = 777; // Объявляется метод в классе страницы private String make_greeting(String name) { return "Hello, "+name + "!"; } public void _jsp. Service(Http. Servlet. Request request, Http. Servlet. Response response) throws java. io. IOException, Servlet. Exception { Page. Context page. Context = null; Http. Session session = null; Jsp. Writer out = null; Object page = this; response. set. Content. Type("text/html; charset=UTF-8"); session = page. Context. get. Session(); out = page. Context. get. Out(); out. write("<html>n"); out. write("<head>n"); out. write("t<title>Sample Hello world page</title>n"); out. write("</head>n"); out. write("<body>rn"); // Скриптлет 1 for (int i=0; i<10; i++) { out. write("rn"); out. write("<h 1> "); out. print( /* Вывод в поток out */ make_greeting("World "+i) ); out. write("</h 1>rn"); // Скриптлет 2 } // Конец цикла out. write("n"); out. write("</body>n"); out. write("</html>"); } }