一、jsp加載項目中資源圖片
如果直接將靜態頁面寫的代碼copy到jsp中,你會發現圖片都無法加載。
獲取代碼:
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
asd
<%=basePath%>/Icons/VerifyCode.png
二、Servlet獲取IP遇到的bug
我是在mac os系統上開發遇到的這個bug,windows上有沒這個問題我不知道。
獲取用戶ip代碼:
String s = request.getHeader("X-Forwarded-For"); if (s == null || s.length() == 0 || "unknown".equalsIgnoreCase(s)) s = request.getHeader("Proxy-Client-IP"); if (s == null || s.length() == 0 || "unknown".equalsIgnoreCase(s)) s = request.getHeader("WL-Proxy-Client-IP"); if (s == null || s.length() == 0 || "unknown".equalsIgnoreCase(s)) s = request.getHeader("HTTP_CLIENT_IP"); if (s == null || s.length() == 0 || "unknown".equalsIgnoreCase(s)) s = request.getHeader("HTTP_X_FORWARDED_FOR"); if (s == null || s.length() == 0 || "unknown".equalsIgnoreCase(s)) s = request.getRemoteAddr(); if ("127.0.0.1".equals(s) || "0:0:0:0:0:0:0:1".equals(s)) try { s = InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException unknownhostexception) { }
當前面的判斷都為空時,進入最後一個if,項目就蹦了,錯誤提示-->
if ("127.0.0.1".equals(s) || "0:0:0:0:0:0:0:1".equals(s)) try { s = InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException unknownhostexception) { }
this web application instance has been stopped already. Could not load [java.net.InetAddress].
解決方法:
打開apache-tomcat文件夾---->打開conf文件夾---->打開server.xml文件--><Host>節點下增加
<Context path="/Servlet" docBase="/Servlet" debug="0" reloadable="true"/>
三、 Ajax請求servlet時報一個錯
報這個錯是接口不支持ajax的請求方式,將get和post都增加到serlvet中,如果只在get中寫代碼,那麼post中就調用get方法,如果只在post中寫代碼,那麼久在get中調用post。
上面的說明我的代碼寫在了doGet中,出於安全考慮,如果稍微zhong yao de jie kou,我們應該選擇只開放post接口,因為get方式的接口,可以直接將請求參數拼接到url上然後通過浏覽器訪問這樣相對來說不安全。