直接撸撸代码也不错:(项目下建立类同上)
package com.pzhu.servlet;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletConfigServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//获取ServletConfig对象
ServletConfig sc=this.getServletConfig();
//获取web.xml中的配置数据
String code=sc.getInitParameter("config");
System.out.println(code);
resp.getWriter().write("servletConfig学习!");
}
}
webxml配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID"
version="2.5">
<servlet>
<servlet-name>ServletConfigServlet</servlet-name>
<servlet-class>com.pzhu.servlet.ServletConfigServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletConfigServlet</servlet-name>
<url-pattern>/scss</url-pattern>
</servlet-mapping>
</web-app>
结果:
就学习到这儿吧,主人···小妲己已经很累了··············