• ServletContext全局变量初始化


    Java部分

    package com.servlet;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class ServletContext extends HttpServlet {
    
    	private static final long serialVersionUID = 1L;
    	public void doGet(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		response.setCharacterEncoding("utf-8");
    		response.setContentType("text/html");
    		
    		javax.servlet.ServletContext servletContext=getServletConfig().getServletContext();
    		String uploadFolder=servletContext.getInitParameter("upload folder");
    		String allowedFileType=servletContext.getInitParameter("allowed file type");
    		
    		PrintWriter out = response.getWriter();
    		out.println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">");
    		out.println("<HTML>");
    		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
    		out.println("  <BODY>");
    		
    		out.println("上传文件夹××××××××××××"+uploadFolder);
    		out.println("<br/>上传文件类型××××××××××××"+allowedFileType);
    		
    		out.println("  </BODY>");
    		out.println("</HTML>");
    		out.flush();
    		out.close();
    	}
    }
    

      web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" 
        xmlns="http://java.sun.com/xml/ns/javaee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
      <display-name></display-name>
      
      <context-param>
          <param-name>upload folder</param-name>
          <param-value>attachment</param-value>
      </context-param>
      <context-param>
          <param-name>allowed file type</param-name>
          <param-value>.gif,.jpg,.bmp</param-value>
      </context-param>
      
      <servlet>
        <servlet-name>ServletContext</servlet-name>
        <servlet-class>com.servlet.ServletContext</servlet-class>
      </servlet>
    
      <servlet-mapping>
        <servlet-name>ServletContext</servlet-name>
        <url-pattern>/servlet/ServletContext</url-pattern>
      </servlet-mapping>    
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>
  • 相关阅读:
    怎样为flash配置Alcon调试工具
    8.9Go简介
    8.14GO之条件语句
    8.10Go之基础语法
    8.11Java之数组知识回顾
    8.13Go之常量
    8.10Go执行流、编译、特点、转义字符
    8.14Go之运算符(Operator)
    8.14Java之使用HttpClient类通过POST方式上传文件
    8.10Go之语言数据类型
  • 原文地址:https://www.cnblogs.com/DeepBlues/p/3477543.html
Copyright © 2020-2023  润新知