Aşağıda basit bir JSP örneği gözükmektedir :
<html> <body> <% int i=10; %> i : <%=i%> </body> </html>
<% ile %> arasına Java kodları girilmektedir. Örnekte i sayısına 10 atanmakta ve ardırdan i değeri sayfa üzerinde gösterilmektedir.
Aşağıdaki fonksiyon bir java web projesinde , bir url'i query string'lerle (query parametreleri) birlikte vermektedir :
public static String getFullURL(HttpServletRequest request) { StringBuffer requestURL = request.getRequestURL(); String queryString = request.getQueryString(); if (queryString == null) { return requestURL.toString(); } else { return requestURL.append('?').append(queryString).toString(); } }
getRequestURL isteği yapılan sayfanın url'ni parametreler (?'dan sonra girilen) olmadan verir. getQueryString ile ?'dan sonraki string alınır ve birleştirilir
package com.xxx; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class ClientResourceFilter implements Filter { public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (!(request instanceof HttpServletRequest)) { chain.doFilter(request, response); return; } HttpServletResponse resp=(HttpServletResponse)response; resp.setHeader("Cache-Control", "max-age=604800"); chain.doFilter(request, response); } public void init(FilterConfig fConfig) throws ServletException {} public void destroy() {} }Yukarıdaki filter web.xml'e aşağıdaki gibi eklenir :
<filter> <filter-name>ClientResourceFilter</filter-name> -class>com.xxx.ClientResourceFilter</filter-class> </filter>filter-mapping ise aşağıdaki gibi tanımlanır.
<filter-mapping> <filter-name>ClientResourceFilter</filter-name> <url-pattern>/Client/*</url-pattern> </filter-mapping>
Bir web sitesine istek yapan client'ın mobil cihaz mı değil mi olduğunu aşağıdaki fonksiyonu kullanarak anlayabilirsiniz :
public boolean isMobile(HttpServletRequest request) throws Exception { String user_agent = request.getHeader("user-agent"); String accept = request.getHeader("accept"); String x_wap_profile = request.getHeader("x-wap-profile"); String profile = request.getHeader("profile"); String opera = request.getHeader("X-OperaMini-Phone"); String ua_pixels = request.getHeader("ua-pixels"); if (user_agent != null) { if (user_agent.contains("windows") &&! user_agent.contains("windows ce")){ return false; } Pattern pattern = Pattern.compile("up.browser|up.link|windows ce|iphone| iemobile|mini|mmp|symbian|midp|wap|phone|pocket|mobile|pda|psp|android|nokia| blackberry|rim|opera mini|opera mobi|windows ce|windows phone|netfront|palm- |palm os|pre|palmsource|avantogo|webos|hiptop|iris|kddi|kindle|lg-|lge|mot-|motorola| nintendo ds|nitro|playstation portable|samsung|sanyo|sprint|sonyericsson| alcatel|audiovox|bird|coral|cricket|docomo|edl|huawei|htc|gt-|lava|ct|lg|lynx|mobile| lenovo|maui|micromax|mot|myphone|nec|nexian|nook|pantech|pg|polaris|ppc|sch |sec|spice|tianyu|ustarcom|utstarcom|" + "videocon|vodafone|winwap|zte", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(user_agent); if (matcher.find()){ return true; } String[] ua = {"acs-","alav","alca","amoi","audi","aste","avan", "benq","bird","blac","blaz", "brew","cell","cldc","cmd-","dang","doco","eric","hipt","inno", "ipaq","java","jigs", "kddi","keji","leno","lg-c","lg-d","lg-g","lge-","maui","maxo", "midp","mits","mmef", "mobi","mot-","moto","mwbp","nec-","newt","noki","opwv", "palm","pana","pant","pdxg", "phil","play","pluc","port","prox","qtek","qwap", "sage","sams","sany","sch-","sec-","send","seri","sgh-","shar","sie-","siem", "smal","smar","sony","sph-","symb","t-mo", "teli","tim-","tosh","tsm-","upg1","upsi", "vk-v","voda","w3c ","wap-","wapa","wapi", "wapp","wapr","webc", "winw","winw","xda","xda-"}; for(int i = 0; i < ua.length; i++ ) { if (ua[i] == user_agent.substring(0,3)) { return true; } } } if (accept != null) { if (accept.contains("text/vnd.wap.wml") || accept.contains("application/vnd.wap.xhtml+xml")) { return true; } } if (x_wap_profile != null || profile != null || opera != null || ua_pixels != null) { return true; } return false; }
Not : http://wiki.drupalschool.net/index.php/Use_Java_to_Detect_Mobile_Devices kaynağından yararlanılmıştır.
Bir hata oluştuğunda (bir exception veya 404, 500 durumu gönderen bir hata) bir sayfaya veya bir servlet'e yönlendirilmesini sağlayabiliriz. Örneğin web.xml dosyasına aşağıdaki gibi bir bölüm eklenebilir :
java.lang.Throwable /hata.jsf 404 /404.jsf
Yukarıdaki eklendğinde herhangi bir Exception olutuğundan hata.jsf, 404 hatası (sayfa bulunamadıysa) 404.jsf sayfası kullanıcıya gösterilir
@font-face { font-family: "XFont"; src: url('fonts/XFontBol.ttf'); src: url('fonts/XFontBolIta.ttf'); src: url('fonts/XFontDem.ttf'); src: url('fonts/XFontDemIta.ttf'); src: url('fonts/XFontMed.ttf'); src: url('fonts/XFontMedIta.ttf'); src: url('fonts/XFontReg.ttf'); }ttf dosyaları fonts klasörü içinde olduğu varsayıyoruz. Klasör yapısı aşağıdaki gibidir :
index.html (style'ın bulunduğu sayfa) fonts/XFontBol.ttf fonts/XFontBolIta.ttf ... ...
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties>Bir Java web projesi haline getirmek için
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency>bağımlılığı eklenir.