JSP常用問答

來源:互聯網
上載者:User

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="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("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.自己寫

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.