JSP內部對象詳解

來源:互聯網
上載者:User
1.1 application
*在jsp中獲得application對象
如:getServletContext().setAttribute("counter",new mycount.Counter());
如:<jsp:useBean scope="application" id="counter" class="mycounter.Counter"/>
*在jsp中處理On Application Start和On Session Start事件的方法
使用HttpSessionBindingListener類.
添加session:
session.putValue("bingdings.listener",new MyListener(getServletContext());
定義MyListener類:
import javax.servlet.http.*;
import javax.servlet.*;
public class MyListener implements HttpSessionBindingListener{
ServletContext context;
public MyListener(ServletContext context){
this.context=context;
}
public void valueBound(HttpSessionBindingEvent event){
System.out.println("valuebound:someone just bound my listener to a session!");
}
public void valueUnbound(HttpSessionBindingEvent event){
System.out.println("valueunbound:someone just unbound my listener!");
}
}

1.2 request
*擷取一個正在運行時的jsp/servlet檔案的絕對url地址
Stringf file=request.getRequestURL();
if(requet.getQueryString()!=null{
file+='?'+request.getqueryString();
}
URL reconstructedURL=new URL(request.getScheme(),request.getServerName(),request.getServerPort(),file);
out.println(reconstructedURL.toString());
*擷取用戶端通過哪一個url訪問本頁面
String callPage=request.getHeader("Referer");
*擷取當前指令碼在當疥檔案系統中的真實路徑
request.getRealPath(request.getServletPath());
*判斷多個submit中的一個
<input type=submit name="sub" value="up">
<input type=submit name="sub" value="down">
在jsp中使用request.getParameter("sub");就可分辨

1.3 response
*網頁重新導向之三方法
(1)response.sendRedirect(url);
(2)<%response.setStatus(HttpServletResponse.SC_MOVED_PREMANENTLY);
String nowloc="/newpath/index.htm";
response.setHeader("Location",newloc);%>
(3)<jsp:forward page="/newpage.jsp"/>
注意上法只能在任何輸出還沒有發送到用戶端之前使用這種方法
*禁用緩衝
<%response.setHeader("Cache-Control","no-store");
response.setDateHeader("Expires",0);%>

1.4 session
*存活時間
<%session.setMaxInactiveInterval(300);%>
*登出
session.invalidate();

1.5 exception
*在jsp頁面中處理Servlet的錯誤
protected void sendErrorRedirect(HttpServletRequest request,
HttpServletResponse response,String errorPageURL,Throwable e)
throws ServletException,IOException{
request.setAttibute("javax.servlet.jsp.jspException",e);
getServletConfig().getServletContext();
getRequestDispatcher(errorPageURL).forward(request,response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response){
try{
//
}
catch(Exception e){try{
sendErrorRedirect(request,response,"/jsp/ErrPage.jsp",e);
}catch(Exception e){e.printStackTrace();}
}
}
*在jsp頁面中輸出錯誤的stacktrace
(1)
<%@ page isErrorPage="true%>
<%
out.println("<pre>");
printWriter pw=response.getWriter();
exception.printStackTrace(pw);
out.println("</pre>");
%>
(2)
<%@ page isErrorPage="true%>
<pre>
<%
exception.printStackTrace(new PrintWriter(out));
%>
</pre>

1.6 Cookie
*設定cookie
<%
Cookie mycookie=new Cookie("aName","aValue");
response.addCookie(mycookie);
//mycookie.setMaxAge(time);
%>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.