http://yidaba./ 一大把長見識的小故事1,、如何混合使用Jsp和SSI #include? 在JSP中可以使用如下方式包含純HTML: <!--#include file="data.inc"--> 但是如果data.inc中包含JSP CODE ,,我們可以使用: <%@include file="data.inc"%>
2、如何執(zhí)行一個線程安全的JSP? 只需增加如下指令 <%@ page isThreadSafe="false" %>
3,、JSP如何處理HTML FORM中的數(shù)據(jù)? 通過內(nèi)置的request對象即可,,如下: <% String item = request.getParameter("item"); int howMany = new Integer(request.getParameter("units")).intValue(); %>
4、在JSP如何包含一個靜態(tài)文件? 靜態(tài)包含如下:<%@ include file="copyright.html" %> 動態(tài)包含如下:<jsp:include page="copyright.html" flush="true"/>
5,、在JSP中如何使用注釋? 主要有四中方法: 1,。<%-- 與 --%> 2。// 3。/**與**/ 4,。<!--與-->
6,、在JSP中如何執(zhí)行瀏覽重定向? 使用如下方式即可:response.sendRedirect("http://ybwen.home./index.html"); 也能物理地改變HTTP HEADER屬性,如下: <% response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); String newLocn="/newpath/index.html"; response.setHeader("Location",newLocn); %>
7,、如何防止在JSP或SERVLET中的輸出不被BROWSER保存在CACHE中? 把如下腳本加入到JSP文件的開始即可: <% response.setHeader("Cache-Control","no-store"); //HTTP 1.1 response.setHeader("Pragma","no-cache"); //HTTP 1.0 response.setDateHeader ("Expires", 0); //prevents caching at the proxy server %>
8,、在JSP中如何設(shè)置COOKIE? COOKIE是作為HTTP HEADER的一部分被發(fā)送的,如下方法即可設(shè)置: <% Cookie mycookie = new Cookie("aName","aValue"); response.addCookie(mycookie); %>
9,、在JSP中如何刪除一個COOKIE? <% Cookie killMyCookie = new Cookie("mycookie", null); killMyCookie.setMaxAge(0); killMyCookie.setPath("/"); response.addCookie(killMyCookie); %>
10,、在一個JSP的請求處理中如何停止JSP的執(zhí)行 如下例: <% if (request.getParameter("wen") != null) { // do something } else { return; } %>
11、在JSP中如何定義方法 你可以定義方法,,但是你不能直接訪問JSP的內(nèi)置對象,,而是通過參數(shù)的方法傳遞。如下: <%! public String howBadFrom(HttpServletRequest req) { HttpSession ses = req.getSession(); ... return req.getRemoteHost(); } %> <% out.print("in general,lao lee is not baddie "); %> <%= howBadFrom(request) %>
12,、如果BROWSER已關(guān)閉了COOKIES,,在JSP中我如何打開SESSION來跟蹤 使用URL重寫即可,如下: hello1.jsp <%@ page session="true" %> <% Integer num = new Integer(100); session.putValue("num",num); String url =response.encodeURL("hello2.jsp"); %> <a href=<%=url%>>hello2.jsp</a>
hello2.jsp <%@ page session="true" %> <% Integer i= (Integer )session.getValue("num"); out.println("Num value in session is "+i.intValue()); %>
13,、在JSP中能發(fā)送EMAIL嗎 可以使用SUN的專用包:sun.net.smtp包,。如下腳本使用SmtpClient類發(fā)送EMAIL。 <%@ page import="sun.net.smtp.SmtpClient, java.io.*" %> <% String from="[email protected]"; String to="[email protected], [email protected]"; try{ SmtpClient client = new SmtpClient("mail.xxxxx.xxx"); client.from(from); client.to(to); PrintStream message = client.startMessage(); message.println("To: " + to); message.println("Subject: Sending email from JSP!"); message.println("This was sent from a JSP page!"); message.println(); message.println("Cool! :-)"); message.println(); message.println("Good Boy"); message.println("Im in genius.com"); message.println(); client.closeServer(); } catch (IOException e){ System.out.println("ERROR SENDING EMAIL:"+e); } %>
14,、在SERVLET中我能調(diào)用一個JSP錯誤頁嗎 當然沒問題,,如下展示了如何在一個SERVLET控制邏輯單元內(nèi)調(diào)用一個JSP錯誤頁面。 protected void sendErrorRedirect(HttpServletRequest request, HttpServletResponse response, String errorPageURL, Throwable e) throws ServletException, IOException { request.setAttribute ("javax.servlet.jsp.jspException", e); getServletConfig().getServletContext(). getRequestDispatcher(errorPageURL).forward(request, response); }
public void doPost(HttpServletRequest request,HttpServletResponse response) { try { // do something } catch (Exception ex) { try { sendErrorRedirect(request,response,"/jsp/MyErrorPage.jsp",ex); } catch (Exception e) { e.printStackTrace(); } } }
15,、JSP和APPLET如何通訊 JSP如何與EJB SessionBean通訊 下面的代碼段作了很好的示范 <%@ page import="javax.naming.*, javax.rmi.PortableRemoteObject, foo.AccountHome, foo.Account" %> <%! //定義一個對SessionBeanHome接口實例的全局引用 AccountHome accHome=null;
public void jspInit() { //獲得Home接口實例 InitialContext cntxt = new InitialContext( ); Object ref= cntxt.lookup("java:comp/env/ejb/AccountEJB"); accHome = (AccountHome)PortableRemoteObject.narrow(ref,AccountHome.class); } %> <% //實例化SessionBean Account acct = accHome.create(); //調(diào)用遠程方法 acct.doWhatever(...); // 如此等等 %>
16,、當我使用一個結(jié)果集時,如何防止字段為"null"的字域顯示在我的HTML輸入文本域中? 可以定義一個簡單的函數(shù)來達到目的,,如下: <%! String blanknull(String s) { return (s == null) ? "" : s; } %>
然后在JSP的FORM中,,可以這樣使用 <input type="text" name="shoesize" value="<%=blanknull(shoesize)%>">
17、如何中SERVLET或JSP下載一個文件(如:binary,text,executable),? 現(xiàn)提供兩個解決方案: A:使用HTTP,, B:在Servlet中,通過設(shè)置ContentType和使用java.io包的Stream等類可作到.例如: response.setContentType("application/x-msword"); 然后想輸出緩沖中寫一些東東即可,。
18,、使用useBean標志初始化BEAN時如何接受初始化參數(shù) 使用如下兩標簽即可: <jsp:getProperty name="wenBean" property="someProperty"/> <jsp:setProperty name="wenBean" property="someProperty" value="someValue"/>
19、使用JSP如何獲得客戶瀏覽器的信息,? 使用request.getHeader(String)即可
20,、能象調(diào)用子程序一樣調(diào)用JSP嗎? 當然可以,,用<jsp:include page="relativeURL" flush="true"/>
21、當我重編譯我的JSP使用的一個類后,為什么JVM繼續(xù)使用我的老CLASS,?
<%@include file="abc.jsp"%>與<jsp:include page="abc.jsp"/>之間的差別,? 前一個為靜態(tài)包含,而后一個為動態(tài)包含
22,、JSP的缺點,? 1。對JAVA程序進行調(diào)試沒有好東東 2,。因大多數(shù)的servlet引擎不支持connection pooling 3,。Servlet引擎沒有標準 4。JSP與其它腳本語言的交互
23,、JSP能進行遞歸調(diào)用嗎,? 當然可以,如對form的提交給本頁
34、如何實現(xiàn)JSP的國際化,? 為各種版本提供resource bundles屬性文件即可
25,、在JSP中如何寫文本文件? 使用PrintWriter對象,,如: <%@ page import="java.io.*" %> <% String str = "print me"; String nameOfTextFile = "/usr/anil/imp.txt"; try { PrintWriter pw = new PrintWriter(new FileOutputStream(nameOfTextFile)); pw.println(str); pw.close(); } catch(IOException e) { out.println(e.getMessage()); } %>
26,、如何在JSP中包括絕對路徑文件? 使用URLConnection即可,。
27,、在servlets和JSP之間能共享session對象嗎? 當然可以,, HttpSession session = request.getSession(true); session.putValue("variable","value");
28,、JavaScript的變量能復制到JSP的SESSION中嗎?
29,、如何設(shè)置cookie在某一時間后過期,? 用Cookie.setMaxAge(int)
30、如何獲得當前的sessions數(shù),? 可以使用HttpSessionBindingListeners來跟蹤
31,、能設(shè)置一些代碼在我所有的JSP文件之上運行?如果可以,,能共享嗎,? 當然可以,可以為你的JSP文件定義一個別名:/jsp/=ybwen.genius.myPreprocessingServlet,而以/jsp/為前綴的文件可以使用
32、對一個JSP頁,,如果多個客戶端同時請求它,,同步可能嗎? 在jsp:useBean語法中使用beanName有何好處,? beanName使用Beans.instantiate()初始化Bean
33,、當我使用<jsp:forward>時,在瀏覽器的地址欄沒有改變? 使用response.sendRedirect("newURL")
34,、如何轉(zhuǎn)換JSP 0.9版本的文件到JSP1.1? 可使用sed/awk即可
35,、使用JSP能設(shè)置HTML FORM中輸入域的焦點,不用JavaScript? 沒辦法
36,、使用JSP連接到數(shù)據(jù)庫連接緩沖池的最好方法是什么,? 1.使用JDBC2。0中帶有此服務的Driver 2.使用提供有此服務的Application server 3.自己寫
|