Eclipse Servet:Receive JSP文件在WebContent中不存在

Eclipse Servet:Receive JSP文件在WebContent中不存在,eclipse,jsp,servlets,web.xml,Eclipse,Jsp,Servlets,Web.xml,我正在使用Eclipse编程servlet。现在,我想做一个example.jsp做一些类似servlet的事情(访问ServletConfig的属性或参数,ServletContext,…) 我将example.jsp放在WebContent的顶部,项目名称是ProjectExample 在web.xml中,我是如何声明此servlet的: <servlet> <servlet-name>JSP Example</servlet-name>

我正在使用Eclipse编程servlet。现在,我想做一个
example.jsp
做一些类似servlet的事情(访问ServletConfig的属性或参数,ServletContext,…)

我将example.jsp放在WebContent的顶部,项目名称是ProjectExample

在web.xml中,我是如何声明此servlet的:

<servlet>
    <servlet-name>JSP Example</servlet-name>
    <jsp-file>example.jsp</jsp-file>  
    <init-param>
      <param-name>name</param-name>
      <param-value>hqt</param-value>
    </init-param>
// I meet warning at <jsp-file>: that doesn't found this file 
//although I have change to: `/example.jsp`, `ProjectExample/example.jsp` or `/ProjectExample/example.jsp`
</servlet>

JSP示例
example.jsp
名称
hqt
//我在以下位置遇到警告:未找到此文件
//虽然我已更改为:`/example.jsp`、`ProjectExample/example.jsp`或`/ProjectExample/example.jsp``
因为容器不能识别这个文件,所以当我使用:
getServletConfig().getInitParameter(“name”)
时,我会收到null

请告诉我如何解决这个问题

谢谢:)


@:如果在代码中键入错误,那不是问题,因为这只是输入错误。我不知道为什么StackOverFlow不再允许复制/粘贴功能。

我认为主要问题不在于您的配置,而在于jsp页面的配置方式

更改您的
/example.jsp
,并将其添加到jsp:

Who am I? -> <%= getServletName() %>
这是因为所有JSP共享名为“JSP”的相同servlet配置。它配置在$CATALINE_HOME/conf/web.xml(如果您使用的是Tomcat)。对于我的Tomcat 7,该配置如下所示:

<servlet>
    <servlet-name>jsp</servlet-name>
    <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
    <init-param>
        <param-name>fork</param-name>
        <param-value>false</param-value>
    </init-param>
    <init-param>
        <param-name>xpoweredBy</param-name>
        <param-value>false</param-value>
    </init-param>
    <load-on-startup>3</load-on-startup>
</servlet>

jsp
org.apache.jasper.servlet.JspServlet
叉
假的
xpoweredBy
假的
3.

您的servlet应该具有
init
方法,您可以在那里读取所需的参数:

public class SimpleServlet extends GenericServlet {
    protected String myParam = null;

    public void init(ServletConfig servletConfig) throws ServletException{
        this.myParam = servletConfig.getInitParameter("name");
      }

    //your servlet code...
}

此示例取自

注意:这是tomcat特有的,其他服务器可能工作不一样注意:Eclipse将把$CATALINE_HOME/conf/web.xml复制到您的工作区(如果您决定删除jsp servlet并检查会发生什么情况)
public class SimpleServlet extends GenericServlet {
    protected String myParam = null;

    public void init(ServletConfig servletConfig) throws ServletException{
        this.myParam = servletConfig.getInitParameter("name");
      }

    //your servlet code...
}