当前位置:   article > 正文

Servlet--取得session,application内置对象_servlet实现类可以用什么方法获得applaction对象

servlet实现类可以用什么方法获得applaction对象

在前面的博客里面,使用Servlet取得了request,response,config对象,实际上通过Servlet程序也可以取得session,application等内置对象。


1,通过HttpServletRequest取得HttpSession

  1. <span style="white-space:pre"> </span>//返回当前的session
  2. public abstract HttpSession getSession();
  3. //返回当前的session,如果没有则创建一个新的session对象返回
  4. public abstract HttpSession getSession(boolean paramBoolean);

  1. package linkin;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import javax.servlet.http.HttpSession;
  8. public class LinkinServlet extends HttpServlet
  9. {
  10. private static final long serialVersionUID = 1L;
  11. @Override
  12. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
  13. {
  14. HttpSession session = req.getSession();
  15. System.out.println(session.getId());//4214F4CE399E02730C3219CF0BD83623
  16. session.setAttribute("age", 25);
  17. System.out.println(session.getAttribute("age"));//25
  18. }
  19. @Override
  20. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
  21. {
  22. this.doGet(req, resp);
  23. }
  24. }
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  5. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  6. <servlet>
  7. <servlet-name>LinkinServlet</servlet-name>
  8. <servlet-class>linkin.LinkinServlet</servlet-class>
  9. <init-param>
  10. <param-name>name</param-name>
  11. <param-value>LinkinPark...</param-value>
  12. </init-param>
  13. </servlet>
  14. <servlet-mapping>
  15. <servlet-name>LinkinServlet</servlet-name>
  16. <url-pattern>/LinkinServlet</url-pattern>
  17. </servlet-mapping>
  18. </web-app>


2,利用GenericServlet取得ServletContext实例

  1. public ServletContext getServletContext()
  2. {
  3. return getServletConfig().getServletContext();
  4. }

  1. package linkin;
  2. import java.io.IOException;
  3. import javax.servlet.ServletContext;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. public class LinkinServlet extends HttpServlet
  9. {
  10. private static final long serialVersionUID = 1L;
  11. @Override
  12. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
  13. {
  14. ServletContext context = getServletContext();
  15. //获取“/”,也就是项目实际在服务器上物理位置
  16. System.out.println(context.getRealPath("/"));//E:\Workserver\apache-tomcat-6.0.37\webapps\linkin\
  17. }
  18. @Override
  19. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
  20. {
  21. this.doGet(req, resp);
  22. }
  23. }
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  5. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  6. <servlet>
  7. <servlet-name>LinkinServlet</servlet-name>
  8. <servlet-class>linkin.LinkinServlet</servlet-class>
  9. <init-param>
  10. <param-name>name</param-name>
  11. <param-value>LinkinPark...</param-value>
  12. </init-param>
  13. </servlet>
  14. <servlet-mapping>
  15. <servlet-name>LinkinServlet</servlet-name>
  16. <url-pattern>/LinkinServlet</url-pattern>
  17. </servlet-mapping>
  18. </web-app>


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/149475
推荐阅读
相关标签
  

闽ICP备14008679号