2013年5月22日 星期三

JDBC設定

1.記得裝SQL的Driver在WEB-INF下的lib
download Driver: http://www.mysql.com/products/connector/

2. 取得Data Source的JNDI設定 (與Context的look up有關)
在WEB-INF下的web.xml裡要加上
<resource-ref>
<res-ref-name>jdbc/aaa</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
這樣jdbc/aaa就會對應到javax,sql.DataSource

Context context= new InitialContext();
DataSource ds=context.lookup("java:/comp/env/jdbc/aaa");
才找得到

3.在WEB-INF下的web.xml設定:

  <Resource
    name="jdbc/mysql-aaa-bbb"
    auth="Container"
    type= "javax.sql.DataSource"
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://127.0.0.1:3306/?characterEncoding=UTF-8"
    username="your_username"
    password="your_password"
    maxActive="2"
    maxIdle="2"
    minIdle="1"
    maxWait="10000"
    autoReconnect = "true"
     >
 </Resource>

在Servlet下連線:
private String connectResource= "java:/comp/env/jdbc/mysql-aaa-bbb";
Context ctx=null;
ctx=new InitialContext();
DataSource ds=(DataSource) ctx.lookup(connectResource);
Connection conn=null;
conn = ds.getConnection();
//query
String queryCommand="SELECT column FROM table ";
PreparedStatement ps=null;
ResultSet rs=ps.executeQuery();

JSTL安裝

jstl.jar
1.1: http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/jakarta-taglibs-standard-1.1.2.zip
1.2: http://download.java.net/maven/1/jstl/jars/jstl-1.2.jar


JSP與Servlet間傳參數

JSP to Servlet:

Enumeration paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()) {
     String paramName = (String)paramNames.nextElement();
   
     System.out.println(paramName);
     String[] paramValues = request.getParameterValues(paramName);
     if (paramValues.length == 1) {
       String paramValue = paramValues[0];
       if (paramValue.length() == 0)
         System.out.print("No Value");
       else
         System.out.print(paramValue);
     } else {
       for(int i=0; i<paramValues.length; i++) {
         System.out.println(paramValues[i]);
       }
     }
}

Servlet to JSP: (利用JSTL)
<c:out value="${attributeName.members(values or methods)}"/>
attributeName是request.setAttribute("attributeName", javaObject);

JSP與Servlet溝通

Servlet to JSP:

String jspPath="foldername/jspname.jsp"; (foldername:在Web Content下放jsp檔的folder)
classForPassParam pass=new classForPassParam();
request.setAttribute("passName", pass); (attribute name: passName)
RequestDispatcher dispatcher = request.getRequestDispatcher( jspPath);
dispatcher.forward(request, response);

JSP to Servlet:
(有post or get的物件(html form))
<form id="form_id" action="../targetJSP.view" method="post">

A:<input type="text" name="AAA"><br>
B:<input type="text" name="BBB">
<input type="submit" value="Submit">
</form>

建造Java Web App

Eclipse下
(上方)New->Dynamic Web Project

Java Resources: 放Java code (Servlets, classes)
Web Content下放jsp(可開資料夾)

Web Content下WEB-INF裡的web-xml設定Java resources (servlets, filters, listeners)

IDE設定(Eclipse, Tomcat)

可參考這篇

Eclipse:
安裝好再下載
下載 "Eclipse IDE for Java EE Developers"
開Eclipse-> Help->Install New Software,在work wtih裡加入 
Indigo - http://download.eclipse.org/releases/indigo
安裝即完成

裝Tomcat:
然後安裝
在Eclipse裡: Windows -> Preference->Server -> Runtime Environments->Add->選下載的Tomcat版本->選Tomcat安裝路徑

進Eclipse改編碼:
(上方)Window->Preferences->(左方)General->Workspace->(中下)Text file encoding,  左邊Web也可以改(Html, CSS...)

Eclipse裝library( JSTL...)
在Web Contetnt/WEB-INF/lib下放jar檔