jsp常見問題

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(
"I''m 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"/>

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章