JSP問答

JSP問答,Java中文站,1、如何混合使用Jsp和SSI #include?

JSP問答

Java中文站

1、如何混合使用Jsp和SSI #include?
在JSP中可以使用如下方式包含純HTML:
<!--#include file="data.inc"-->
但是如果data.inc中包含JSP CODE ,我們可以使用:
<%@include file="data.inc"%>

2、如何執行一個執行緒安全的JSP?
只需增加如下指令
<%@ page isThreadSafe="false" %>

3、JSP如何處理HTML FORM中的數據?
通過內置的request對象即可,如下:
<%
String item = request.getParameter("item");
int howMany = new Integer(request.getParameter("units")).intValue();
%>

4、在JSP如何包含一個靜態檔案?
靜態包含如下:<%@ include file="copyright.html" %>
動態包含如下:<jsp:include page="copyright.html" flush="true"/>

5、在JSP中如何使用注釋?
主要有四中方法:
1。<%-- 與 --%>
2。//
3。/**與**/
4。<!--與-->

6、在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);
%>

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中如何設定COOKIE?
COOKIE是作為HTTP HEADER的一部分被傳送的,如下方法即可設定:
<%
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的執行
如下例:
<%
if (request.getParameter("wen") != null) {
// do something
} else {
return;
}
%>

11、在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) %>

12、如果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());
%>

13、在JSP中能傳送EMAIL嗎
可以使用SUN的專用包:sun.net.smtp包。如下腳本使用SmtpClient類傳送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中我能調用一個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();
}
}
}

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

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

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

17、如何中SERVLET或JSP下載一個檔案(如:binary,text,executable)?
現提供兩個解決方案:
A:使用HTTP,
B:在Servlet中,通過設定ContentType和使用java.io包的Stream等類可作到.例如:
response.setContentType("application/x-msword");
然後想輸出緩衝中寫一些東東即可。

18、使用usebean標誌初始化BEAN時如何接受初始化參數
使用如下兩標籤即可:
<jsp:getProperty name="wenBean" property="someProperty"/>
<jsp:setProperty name="wenBean" property="someProperty" value="someValue"/>

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

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

21、當我重編譯我的JSP使用的一個類後,為什麼JVM繼續使用我的老CLASS?

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

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

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

34、如何實現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、如何設定cookie在某一時間後過期?
用Cookie.setMaxAge(int)

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

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

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

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

34、如何轉換JSP 0.9版本的檔案到JSP1.1?
可使用sed/awk即可

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

36、使用JSP連線到資料庫連線緩衝池的最好方法是什麼?
1.使用JDBC2。0中帶有此服務的Driver
2.使用提供有此服務的Application Server
3.自己寫
 

相關詞條

相關搜尋

熱門詞條

聯絡我們