Re: 用servlet呈現 piechart 無法顯現在網頁中

看板java作者 (愚人)時間16年前 (2007/11/11 11:46), 編輯推噓2(200)
留言2則, 2人參與, 最新討論串6/6 (看更多)
看到一堆 jsp 就暈了 囧rz --------------------------------------------------------------- String path2 = request.getRealPath("."); try { File image = new File(path2 +"\\MyChart2.png"); ChartUtilities.saveChartAsPNG(image, chart, 640, 480); } catch (Exception exc) { } out.println("<p><img src = 'MyChart2.png'></img></p>"); 這樣寫有潛在的問題 src = 'MyChart2.png' 除非你確定你的 jsp 只會在 context root 不然應該要是 request.getContextPath() + '/' + 'MyChart2.png' 要以 context path 為起始點來算比較保險 如果用 jstl 就會寫成 <img src="<c:url value="/MyChar2.png" />" /> --------------------------------------------------------------- if(drawback.equals("1")){ where_condiction = " and (缺失態樣 like '%1%') "; } 您 equals 的習慣不太好. 只要 drawback 不小心為 null 你的程式就掛在那邊 改成 "1".equals(fooooo) 比較好 --------------------------------------------------------------- if(tartype.equals("0")){ selcase = " where (預算金額 > 0 ) "; temptype = "的所有稽核案件"; temptype2 = "所有稽核採購案件"; } if(tartype.equals("1")){ selcase = " where (採購標的性質 = '工程' ) "; temptype = "的工程稽核案件"; temptype2 = "工程類稽核採購案件"; } if(tartype.equals("2")){ selcase = " where (採購標的性質 = '勞務' ) "; temptype = "的勞務稽核案件"; temptype2 = "勞務類稽核採購案件"; } if(tartype.equals("3")){ selcase = " where (採購標的性質 = '財物' ) "; temptype = "的財物稽核案件"; temptype2 = "財物類稽核採購案件"; } if(tartype.equals("4")){ } IF 太長了, 蠻傷腦筋的 比較好維護的方式大概會寫成 public abstract class SelectType { String selcase; String temptype; String temptype2; public String getSelcase() { return selcase; } public void setSelcase(String selcase) { this.selcase = selcase; } public String getTemptype() { return temptype; } public void setTemptype(String temptype) { this.temptype = temptype; } public String getTemptype2() { return temptype2; } public void setTemptype2(String temptype2) { this.temptype2 = temptype2; } } class AllType extends SelectType { public AllType() { selcase = " where (預算金額 > 0 ) "; temptype = "的所有稽核案件"; temptype2 = "所有稽核採購案件"; } } class EngineeringType extends SelectType { public EngineeringType() { selcase = " where (採購標的性質 = '工程' ) "; temptype = "的工程稽核案件"; temptype2 = "工程類稽核採購案件"; } } class ServiceType extends SelectType { public ServiceType() { selcase = " where (採購標的性質 = '勞務' ) "; temptype = "的勞務稽核案件"; temptype2 = "勞務類稽核採購案件"; } } class EffectsType extends SelectType { public EffectsType() { selcase = " where (採購標的性質 = '財物' ) "; temptype = "的財物稽核案件"; temptype2 = "財物類稽核採購案件"; } } ==================================================================== if(tartype.equals("2")){ selcase = " where (採購標的性質 = '勞務' ) "; temptype = "的勞務稽核案件"; temptype2 = "勞務類稽核採購案件"; } 把要用到的 SelectType 可以先放到 Map 裡 您就不需要 if... 來決定設定的值 map.get("2") 取得一個 ServiceType 的 instance 整串 if 變成一個 map get method 會比較不容易出錯 也方便加新的 type =================================================================== 而後面也有 where_condition 做法也可以模仿, 不過可能要迂迴一點的做法 像是建立一個 Decorator 類別 abstract class WhereConditionDecorator extends SelectType { SelectType selectType; public WhereConditionDecorator(SelectType selectType) { this.selectType = selectType; } public abstract String getSelcase(); } class WhereCondition_anyone extends WhereConditionDecorator { public WhereCondition_anyone(SelectType selectType) { super(selectType); } public String getSelcase() { return selectType.getSelcase() + " " + "and ((缺失態樣 like '%1%') " + "or (缺失態樣 like '%2%') " + "or (缺失態樣 like '%3%') " + "or (缺失態樣 like '%4%') " + "or (缺失態樣 like '%5%') " + "or (缺失態樣 like '%6%') " + "or (缺失態樣 like '%7%') " + "or (缺失態樣 like '%8%') " + "or (缺失態樣 like '%9%')) "; } } 有了他, 你可以利用已經取得的 SelectType 實體再建立一個新的物件 new WhereCondition_anyone(type).getSelcase() 您就算是透過物件的手法幫您產生 sql string where (採購標的性質 = '勞務' ) and ((缺失態樣 like '%1%') or (缺失態樣 like '%2%') or (缺失態樣 like '%3%') or (缺失態樣 like '%4%') or (缺失態樣 like '%5%') or (缺失態樣 like '%6%') or (缺失態樣 like '%7%') or (缺失態樣 like '%8%') or (缺失態樣 like '%9%')) -- 雖然講那麼多 不知對您現在的用處大不大 不過您可以留著以後有閒再來改看也說不定 :) java 也能寫得很有趣 ^^ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.167.103.78

11/11 13:32, , 1F
獲益良多
11/11 13:32, 1F

11/15 00:39, , 2F
大推此文章,此文該M才是
11/15 00:39, 2F
文章代碼(AID): #17DdhuTy (java)
討論串 (同標題文章)
文章代碼(AID): #17DdhuTy (java)