最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網 會員登陸 & 注冊

ServletContext的代碼

2020-05-09 15:31 作者:小垃圾kiki  | 我要投稿
package cn.itcast.web.servletcontext;

import java.io.File;
import java.io.IOException;

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;

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

?? ?/**
?? ? * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
?? ? */
?? ?protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
?? ??? ?
?? ??? ?//2.通過HttpServlet獲取
?? ??? ?ServletContext context = this.getServletContext();
?? ??? ?//獲取文件的服務器路徑
?? ??? ?String realPath = context.getRealPath("/b.txt");//訪問web目錄下的資源路徑
?? ??? ?System.out.println(realPath);
?? ??? ?File file=new File(realPath);
?? ??? ?String c = context.getRealPath("/WEB-INF/c.txt");//訪問WEB-INF目錄下的資源路徑
?? ??? ?System.out.println(c);
?? ??? ?String a = context.getRealPath("/WEB-INF/classes/a.txt");//訪問src目錄下的資源路徑
?? ??? ?System.out.println(a);
?? ?}

?? ?/**
?? ? * @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 cn.itcast.web.servletcontext;

import java.io.IOException;

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;

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

?? ?/**
?? ? * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
?? ? */
?? ?protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
?? ??? ?
?? ??? ?//2.通過HttpServlet獲取
?? ??? ?ServletContext context = this.getServletContext();
?? ??? ?//獲取數(shù)據(jù)
?? ??? ?Object msg = context.getAttribute("msg");
?? ??? ?System.out.println(msg);
?? ??? ?
?? ?}

?? ?/**
?? ? * @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 cn.itcast.web.servletcontext;

import java.io.IOException;

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;

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

?? ?/**
?? ? * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
?? ? */
?? ?protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
?? ??? ?
?? ??? ?//2.通過HttpServlet獲取
?? ??? ?ServletContext context = this.getServletContext();
?? ??? ?//設置數(shù)據(jù)
?? ??? ?context.setAttribute("msg", "haha");
?? ??? ?
?? ?}

?? ?/**
?? ? * @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);
?? ?}

}

ServletContext的代碼的評論 (共 條)

分享到微博請遵守國家法律
博白县| 方正县| 莎车县| 乌鲁木齐县| 睢宁县| 女性| 丰城市| 开化县| 镇坪县| 阳西县| 商河县| 密山市| 成武县| 甘谷县| 梓潼县| 台中市| 苏尼特右旗| 洞头县| 镇康县| 阿拉善右旗| 德令哈市| 夏邑县| 乡城县| 遵化市| 沅江市| 寿宁县| 吉木萨尔县| 九江市| 梨树县| 沾化县| 柳州市| 隆昌县| 万山特区| 苍山县| 白河县| 九龙城区| 东方市| 登封市| 渭源县| 沂南县| 思南县|