日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Sission与aplication 在servlet中用法汇总

發布時間:2024/3/26 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Sission与aplication 在servlet中用法汇总 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

application用法:

在Servlet中,取得application有兩種方法:?
1.通過無參初始化方法,直接取得;?
2.通過有參初始化方法,必須使用config對象取得。?

實例:?
1.無參初始化方法:?
Application.java:?
view plaincopy to clipboardprint??

?? 1. package mgc.servlet.test;???
?? 2.???
?? 3. import java.io.*;???
?? 4. import javax.servlet.*;???
?? 5. import javax.servlet.http.*;???
?? 6.???
?? 7. public class Application extends HttpServlet {???
?? 8.????????
?? 9.???? public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {???
? 10.???????? this.doPost(request,response);???
? 11.???? }???
? 12.????????
? 13.???? public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {???
? 14.???????? //取得Application對象???
? 15.???????? ServletContext application=this.getServletContext();???
? 16.???????? //設置Application屬性???
? 17.???????? application.setAttribute("name", "Magci");???
? 18.???????? //跳轉到接收頁面???
? 19.???????? response.sendRedirect("application.jsp");???
? 20.???? }???
? 21. }??

package mgc.servlet.test;?
import java.io.*;?
import javax.servlet.*;?
import javax.servlet.http.*;?
public class Application extends HttpServlet {?
????
??? public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {?
??????? this.doPost(request,response);?
??? }?
????
??? public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {?
??????? //取得Application對象?
??????? ServletContext application=this.getServletContext();?
??????? //設置Application屬性?
??????? application.setAttribute("name", "Magci");?
??????? //跳轉到接收頁面?
??????? response.sendRedirect("application.jsp");?
??? }?
}?



web.xml:?
view plaincopy to clipboardprint??

?? 1.?? <servlet>??
?? 2.???? <servlet-name>Application</servlet-name>??
?? 3.???? <servlet-class>mgc.servlet.test.Application</servlet-class>??
?? 4.?? </servlet>??
?? 5.???
?? 6.?? <servlet-mapping>??
?? 7.???? <servlet-name>Application</servlet-name>??
?? 8.???? <url-pattern>/servlet/application</url-pattern>??
?? 9.?? </servlet-mapping>??

? <servlet>?
??? <servlet-name>Application</servlet-name>?
??? <servlet-class>mgc.servlet.test.Application</servlet-class>?
? </servlet>?
? <servlet-mapping>?
??? <servlet-name>Application</servlet-name>?
??? <url-pattern>/servlet/application</url-pattern>?
? </servlet-mapping>?



application.jsp:?
view plaincopy to clipboardprint??

?? 1. <%@page contentType="text/html;charset=GB2312" %>??
?? 2. <html>??
?? 3.?? <head>??
?? 4.???? <title>application</title>??
?? 5.?? </head>??
?? 6.??????
?? 7.?? <body>??
?? 8.?????? <h1><%=getServletContext().getAttribute("name") %></h1>??
?? 9.?? </body>??
? 10. </html>??

<%@page contentType="text/html;charset=GB2312" %>?
<html>?
? <head>?
??? <title>application</title>?
? </head>?
??
? <body>?
????? <h1><%=getServletContext().getAttribute("name") %></h1>?
? </body>?
</html>?




2.有參初始化方法:?

Config.java:?
view plaincopy to clipboardprint??

?? 1. package mgc.servlet.test;???
?? 2.???
?? 3. import java.io.*;???
?? 4. import javax.servlet.*;???
?? 5. import javax.servlet.http.*;???
?? 6.???
?? 7. public class Config extends HttpServlet {???
?? 8.????????
?? 9.???? private ServletConfig conf=null;???
? 10.????????
? 11.???? public void init(ServletConfig conf) throws ServletException {???
? 12.???????? //實例化config對象???
? 13.???????? this.conf=conf;???
? 14.???? }???
? 15.????????
? 16.???? public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {???
? 17.???????? this.doPost(request,response);???
? 18.???? }???
? 19.????????
? 20.???? public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {???
? 21.???????? //通過config對象取得Application對象???
? 22.???????? ServletContext application=this.conf.getServletContext();???
? 23.???????? //設置Application屬性???
? 24.???????? application.setAttribute("name", "Magci");???
? 25.???????? //跳轉到接收頁面???
? 26.???????? response.sendRedirect("config.jsp");???
? 27.???? }???
? 28. }??

package mgc.servlet.test;?
import java.io.*;?
import javax.servlet.*;?
import javax.servlet.http.*;?
public class Config extends HttpServlet {?
????
??? private ServletConfig conf=null;?
????
??? public void init(ServletConfig conf) throws ServletException {?
??????? //實例化config對象?
??????? this.conf=conf;?
??? }?
????
??? public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {?
??????? this.doPost(request,response);?
??? }?
????
??? public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {?
??????? //通過config對象取得Application對象?
??????? ServletContext application=this.conf.getServletContext();?
??????? //設置Application屬性?
??????? application.setAttribute("name", "Magci");?
??????? //跳轉到接收頁面?
??????? response.sendRedirect("config.jsp");?
??? }?
}?



web.xml:?
view plaincopy to clipboardprint??

?? 1.?? <servlet>??
?? 2.???? <servlet-name>Config</servlet-name>??
?? 3.???? <servlet-class>mgc.servlet.test.Config</servlet-class>??
?? 4.?? </servlet>??

? <servlet>?
??? <servlet-name>Config</servlet-name>?
??? <servlet-class>mgc.servlet.test.Config</servlet-class>?
? </servlet>?



config.jsp:?
view plaincopy to clipboardprint??

?? 1. <%@page contentType="text/html;charset=GB2312" %>??
?? 2. <html>??
?? 3.?? <head>??
?? 4.???? <title>config</title>??
?? 5.?? </head>??
?? 6.??????
?? 7.?? <body>??
?? 8.?????? <h1><%=getServletContext().getAttribute("name") %></h1>??
?? 9.?? </body>??
? 10. </html>?

?

Sission用法:

眾所周知,在JSP頁面上可直接通過session.setAttribute(name,object)設置session,可如果想在servlet使用session 的話,就和在JSP有點區別了。在servlet中通過HttpSession session=request.getSession(boolean create);得到一個session對象(準確來說,得到的應該是一個HttpSession 對象),然后,就可以像在JSP頁面中直接使用它了。

參數Boolean create說明:如果當前請求不屬于任何會話,而且create參數為true,則創建一個會話,否則返回null.當然如果為false的時候,那就和不帶參數的HttpSession session=request.getSession();等價了。

其實,jsp中的session和servlet中的HttpSession其實根本沒有區別,JSP頁面在編譯時會通過Jsp ? container將session對象變換為javax.servlet.http.HttpSession對象。

捎帶介紹下HttpSession的方法吧。

public void setAttribute(String name,Object value)

  將value對象以name名稱綁定到會話

  public object getAttribute(String name)

  取得name的屬性值,如果屬性不存在則返回null

  public void removeAttribute(String name)

  從會話中刪除name屬性,如果不存在不會執行,也不會拋處錯誤.

  public Enumeration getAttributeNames()

  返回和會話有關的枚舉值

  public void invalidate()

  使會話失效,同時刪除屬性對象

  public Boolean isNew()

  用于檢測當前客戶是否為新的會話

  public long getCreationTime()

  返回會話創建時間

  public long getLastAccessedTime()

  返回在會話時間內web容器接收到客戶最后發出的請求的時間

  public int getMaxInactiveInterval()

  返回在會話期間內客戶請求的最長時間.秒

  public void setMasInactiveInterval(int seconds)

  允許客戶客戶請求的最長時間

  ServletContext getServletContext()

  返回當前會話的上下文環境,ServletContext對象可以使Servlet與web容器進行通信

  public String getId()

  返回會話期間的識別號

?

總結

以上是生活随笔為你收集整理的Sission与aplication 在servlet中用法汇总的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。