JAVA SERVLET 属性范围样例

简介: package com.jeelearning.servlet; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.
package com.jeelearning.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class SetAttributeServlet
 */
@WebServlet("/setAttribute")
public class SetAttributeServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SetAttributeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        ServletContext servletctx = getServletContext();
        HttpSession session = request.getSession();
        
        servletctx.setAttribute("applicationAttribute", "应用范围属性");
        session.setAttribute("sessionAttribute", "会话范围属性");
        request.setAttribute("requestAttribute", "请求范围属性");
        
        RequestDispatcher view = request.getRequestDispatcher("getAttribute");
        view.forward(request, response);
        
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
package com.jeelearning.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class GetAttributeServlet
 */
@WebServlet("/getAttribute")
public class GetAttributeServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetAttributeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        ServletContext servletctx = getServletContext();
        HttpSession session = request.getSession();
        PrintWriter out = response.getWriter();
        
        out.println("应用范围属性: ");
        out.println(servletctx.getAttribute("applicationAttribute"));
        out.println("<br/>");
        out.println("会话范围属性: ");
        out.println(session.getAttribute("sessionAttribute"));
        out.println("<br/>");
        out.println("请求范围属性: ");
        out.println(request.getAttribute("requestAttribute"));
        out.println("<br/>");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

目录
相关文章
|
27天前
|
Java 编译器 测试技术
|
2月前
|
缓存 安全 Java
Java服务器端技术:Servlet与JSP的集成与扩展
Java服务器端技术:Servlet与JSP的集成与扩展
23 3
|
2月前
|
存储 缓存 前端开发
Servlet与JSP在Java Web应用中的性能调优策略
Servlet与JSP在Java Web应用中的性能调优策略
26 1
|
2月前
Caused by: java.lang.ClassNotFoundException: javax.servlet.Filter
Caused by: java.lang.ClassNotFoundException: javax.servlet.Filter
34 3
|
2月前
|
存储 Java 关系型数据库
基于Servlet和JSP的Java Web应用开发指南
基于Servlet和JSP的Java Web应用开发指南
19 0
|
2月前
|
前端开发 安全 Java
在Java服务器端开发的浩瀚宇宙中,Servlet与JSP犹如两颗璀璨的明星,它们联袂登场,共同编织出动态网站的绚丽篇章。
在Java服务器端开发的浩瀚宇宙中,Servlet与JSP犹如两颗璀璨的明星,它们联袂登场,共同编织出动态网站的绚丽篇章。
18 0
|
3月前
|
安全 Java API
Java中的Servlet编程详解
Java中的Servlet编程详解
|
3月前
|
Java 数据库连接 开发者
Java中的Servlet生命周期详解
Java中的Servlet生命周期详解
|
3月前
|
安全 Java API
Java中的Servlet编程详解
Java中的Servlet编程详解
|
4月前
|
存储 缓存 Java
探索 Java 中 `Class.java` 的属性与方法
探索 Java 中 `Class.java` 的属性与方法
下一篇
无影云桌面