https://docs.oracle.com/cd//F23552_01/weblogic-server/12.2.1.4/jdbcp/programming.html#GUID-14A525A0-1340-497C-98D7-A323AB724718
https://www.javadrive.jp/servlet/database/index6.html
OS: CentOS7
前提:
Oracle WebLogic Server 14c (14.1.1.0)インストール済
データソース作成済
-- 1. テストテーブル作成
接続先DBで実行
drop table tab1 purge;
create table tab1(col1 int,col2 varchar2(10),col3 timestamp);
insert into tab1 values(1,'A',sysdate);
insert into tab1 values(2,'B',sysdate);
insert into tab1 values(3,'C',sysdate);
insert into tab1 values(4,'D',sysdate);
insert into tab1 values(5,'あ',sysdate);
commit;
select * from tab1 order by col1;
-- 2. Weblogic サービス起動
以下、すべてweblogicユーザで作業
cd /home/weblogic/Oracle/Middleware/Oracle_Home/user_projects/domains/base_domain
./startWebLogic.sh
-- 3. 管理コンソールにログイン
http://192.168.137.128:7001/console
-- 4. Servletのライブラリダウンロード
wget -O servlet-api.jar "https://ja.osdn.net/frs/g_redir.php?m=kent&f=bmfproject/lib/servlet-api.jar"
-- 5. コーディング
cd
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.util.*;
import javax.naming.*;
public class DbTestServlet extends HttpServlet {
public void doGet (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
Context ctx = null;
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL, "t3://mmm128:7001");
ht.put(Context.SECURITY_PRINCIPAL, "weblogic");
ht.put(Context.SECURITY_CREDENTIALS, "password");
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
ctx = new InitialContext(ht);
javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup ("test");
conn = ds.getConnection();
stmt = conn.createStatement();
stmt.execute("select * from tab1 order by col1");
rs = stmt.getResultSet();
PrintWriter out;
res.setContentType("text/html; charset=UTF-8");
out = res.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>DbTest</title>");
out.println("</head>");
out.println("<body>");
while(rs.next()){
int col1 = rs.getInt("col1");
String col2 = rs.getString("col2");
out.println("<p>");
out.println("col1: " + col1 + ", col2: " + col2);
out.println("</p>");
}
out.println("</body>");
out.println("</html>");
rs.close();
stmt.close();
stmt=null;
conn.close();
conn=null;
} catch (Exception e) {
PrintWriter out;
res.setContentType("text/html; charset=UTF-8");
out = res.getWriter();
out.println("<html><body>");
out.println("<h1>Error1</h1>");
out.println("</body></html>");
}
finally {
try {
ctx.close();
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (Exception e) {
PrintWriter out;
res.setContentType("text/html; charset=UTF-8");
out = res.getWriter();
out.println("<html><body>");
out.println("<h1>Error</h1>");
out.println("</body></html>");
}
}
}
}
mkdir -p 20220307/app03/WEB-INF/{classes,lib}
vim 20220307/app03/WEB-INF/web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<servlet>
<servlet-name>dbtest</servlet-name>
<servlet-class>DbTestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dbtest</servlet-name>
<url-pattern>/hello/</url-pattern>
</servlet-mapping>
</web-app>
vim 20220307/app03/WEB-INF/weblogic.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE weblogic-web-app
http://xmlns.oracle.com/weblogic/weblogic-web-app/1.0/weblogic-web-app.xsd>
<weblogic-web-app>
</weblogic-web-app>
-- 6. ビルド
cd
cp servlet-api.jar 20220307/app03/WEB-INF/lib/
javac DbTestServlet.java -Xlint:unchecked -classpath ./20220307/app03/WEB-INF/lib/servlet-api.jar -d ./20220307/app03/WEB-INF/classes
cd 20220307/app03
jar cvf ../app03.war .
ls -l ../
-- 7. デプロイ
デプロイメント→インストール
app03.war を選択
デフォルトの設定のまま「次」をクリック
「追加構成」のメニューまできたら「いいえ、後で構成を確認します。」を選択し、「終了」をクリック
http://192.168.137.128:7001/app03/hello