package cn.itcast.servletconfig;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ConfigServletDemo1
*/
@WebServlet("/ConfigServletDemo1")
@WebInitParam(name = "xxx", value = "yyy")
public class ConfigServletDemo1 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
@Override
public void init(ServletConfig config) throws ServletException {
String value=config.getInitParameter("xxx");
System.out.println(value);
}
}
我在浏览器中输入后,Console里出现的是null。可是我看视频里应该是出现yyy的!为什么会这样!!我找了好久了!刚入门!求大神教~~~!
------解决思路----------------------
你的注解方式有问题
注解方式修改为
@WebServlet(
urlPatterns = { "/ConfigServletDemo1" },
initParams = {
@WebInitParam(name = "xxx", value = "yy")
})