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>