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()
返回会话期间的识别号