top
Loading...
Jsp問答集一
如何混合使用Jsp和SSI #include?
在JSP中可以使用如下方式包含純HTML:

在JSP中如何執行瀏覽重定向?
使用如下方式即可:response.sendRedirect("http://ybwen.home.chinaren.com/index.html");
也能物理地改變HTTP HEADER屬性,如下:
<%
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
String newLocn="/newpath/index.html";
response.setHeader("Location",newLocn);
%>


如何防止在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
%>


在JSP中如何設置COOKIE?
COOKIE是作為HTTP HEADER的一部分被發送的,如下方法即可設置:
<%
Cookie mycookie = new Cookie("aName","aValue");
response.addCookie(mycookie);
%>


在JSP中如何刪除一個COOKIE?
<%
Cookie killMyCookie = new Cookie("mycookie", null);
killMyCookie.setMaxAge(0);
killMyCookie.setPath("/");
response.addCookie(killMyCookie);
%>


在一個JSP的請求處理中如何停止JSP的執行
如下例:
<%
if (request.getParameter("wen") != null) {
// do something
} else {
return;
}
%>


在JSP中如何定義方法
你可以定義方法,但是你不能直接訪問JSP的內置對象,而是通過參數的方法傳遞。如下:
<%!
public String howBadFrom(HttpServletRequest req) {
HttpSession ses = req.getSession();
...
return req.getRemoteHost();
}
%>
<%
out.print("in general,lao lee is not baddie ");
%>
<%= howBadFrom(request) %>


如果BROWSER已關閉了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());
%>


在JSP中能發送EMAIL嗎
可以使用SUN的專用包:sun.net.smtp包。如下腳本使用SmtpClient類發送EMAIL。
<%@ page import="sun.net.smtp.SmtpClient, java.io.*" %>
<%
String from="ybwen@sina.com";
String to="hewenjun@yeah.net, lei@who.com.cn";
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("I'm in genius.com");
message.println();
client.closeServer();
}
catch (IOException e){
System.out.println("ERROR SENDING EMAIL:"+e);
}
%>


在SERVLET中我能調用一個JSP錯誤頁嗎
當然沒問題,如下展示了如何在一個SERVLET控制邏輯單元內調用一個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();
}
}
}


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();
//調用遠程方法
acct.doWhatever(...);
// 如此等等
%>


當我使用一個結果集時,如何防止字段為"null"的字域顯示在我的HTML輸入文本域中?
可以定義一個簡單的函數來達到目的,如下:
<%!
String blanknull(String s) {
return (s == null) ? "" : s;
}
%>

然后在JSP的FORM中,可以這樣使用
<input type="text" name="shoesize" value="<%=blanknull(shoesize)%>">


如何中SERVLET或JSP下載一個文件(如:binary,text,executable)?
現提供兩個解決方案:
A:使用HTTP,如
點擊下載網絡恐龍圖片(這個地址是假的)

B:在Servlet中,通過設置ContentType和使用java.io包的Stream等類可作到.例如:
response.setContentType("application/x-msword");
然后想輸出緩沖中寫一些東東即可。

使用useBean標志初始化BEAN時如何接受初始化參數
使用如下兩標簽即可:
<jsp:getProperty name="wenBean" property="someProperty"/>
<jsp:setProperty name="wenBean" property="someProperty" value="someValue"/>
JSP問答集二

作者:ybwen

使用JSP如何獲得客戶瀏覽器的信息?
使用request.getHeader(String)即可


能象調用子程序一樣調用JSP嗎?
當然可以,用<jsp:include page="relativeURL" flush="true"/>


當我重編譯我的JSP使用的一個類后,為什么JVM繼續使用我的老CLASS?


<%@include file="abc.jsp"%>與<jsp:include page="abc.jsp"/>之間的差別?
前一個為靜態包含,而后一個為動態包含


JSP的缺點?
1。對JAVA程序進行調試沒有好東東
2。因大多數的servlet引擎不支持connection pooling
3。Servlet引擎沒有標準
4。JSP與其它腳本語言的交互


JSP能進行遞歸調用嗎?
當然可以,如對form的提交給本頁


如何實現JSP的國際化?
為各種版本提供resource bundles屬性文件即可

在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());
}
%>


如何在JSP中包括絕對路徑文件?
使用URLConnection即可。


在servlets和JSP之間能共享session對象嗎?
當然可以,
HttpSession session = request.getSession(true);
session.putValue("variable","value");


JavaScript的變量能復制到JSP的SESSION中嗎?


如何設置cookie在某一時間后過期?
用Cookie.setMaxAge(int)


如何獲得當前的sessions數?
可以使用HttpSessionBindingListeners來跟蹤


能設置一些代碼在我所有的JSP文件之上運行?如果可以,能共享嗎?
當然可以,可以為你的JSP文件定義一個別名:/jsp/=ybwen.genius.myPreprocessingServlet,而以/jsp/為前綴的文件可以使用


對一個JSP頁,如果多個客戶端同時請求它,同步可能嗎?
在jsp:useBean語法中使用beanName有何好處?
beanName使用Beans.instantiate()初始化Bean


當我使用<jsp:forward>時,在瀏覽器的地址欄沒有改變?
使用response.sendRedirect("newURL")


如何轉換JSP 0.9版本的文件到JSP1.1?
可使用sed/awk即可


使用JSP能設置HTML FORM中輸入域的焦點,不用JavaScript?
沒辦法


使用JSP連接到數據庫連接緩沖池的最好方法是什么?
1。使用JDBC2。0中帶有此服務的Driver
2.使用提供有此服務的Application server
3.自己寫

北斗有巢氏 有巢氏北斗