Servlets 为什么同一项目的内部和外部调用的ServletContext值不同?
Servlets 为什么同一项目的内部和外部调用的ServletContext值不同?,servlets,attributes,Servlets,Attributes,为什么它们不同?如果它们不同,我们如何获得一个ServletContext属性,该属性应该是应用程序智能的?由于这两个访问来自不同的URL,servlet上下文不应该是相同的。如果您遇到类似问题,请在访问项目和项目源中使用相同的URL。这也将避免Ajax调用的安全问题。您可以发布您的web.xml吗?它们不一定不同,它们被一个门面隐藏。IMHO这一切都是关于servlet引擎的内部设计。见例。 public class Servlet1 extends HttpServlet { @Ov
为什么它们不同?如果它们不同,我们如何获得一个ServletContext属性,该属性应该是应用程序智能的?由于这两个访问来自不同的URL,servlet上下文不应该是相同的。如果您遇到类似问题,请在访问项目和项目源中使用相同的URL。这也将避免Ajax调用的安全问题。您可以发布您的web.xml吗?它们不一定不同,它们被一个门面隐藏。IMHO这一切都是关于servlet引擎的内部设计。见例。
public class Servlet1 extends HttpServlet {
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
ServletContext sc = (ServletContext) getServletConfig().getServletContext();
out.println("sc = " + sc);
}
> The result for internal call (http://local-server-ip:8080/prj/Servlet1) :
sc = org.apache.catalina.core.ApplicationContextFacade@1d57f3c
> The result for external call (http://example.com/prj/Servlet1) :
sc = org.apache.catalina.core.ApplicationContextFacade@135542b