Pet*_*ohn 37 jsp web.xml servlets welcome-file
我在我的web.xml文档中有这个.我想要一个欢迎列表,所以我不需要再输入主页的路径了.但每次单击我的tomcat页面中的应用程序时,它都会显示所请求的资源不可用.
<listener>
<listener-class>web.Init</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>index</servlet-name>
<servlet-class>web.IndexServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>index</servlet-name>
<url-pattern>/index</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
我的jsp页面的servlet
package web;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
public class IndexServlet extends HttpServlet
{
private Logger logger = Logger.getLogger(this.getClass());
private RequestDispatcher jsp;
public void init(ServletConfig config) throws ServletException
{
ServletContext context = config.getServletContext();
jsp = context.getRequestDispatcher("/WEB-INF/jsp/index.jsp");
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
logger.debug("doGet()");
jsp.forward(req, resp);
}
}
Run Code Online (Sandbox Code Playgroud)
为什么它仍然不起作用?我仍然需要在我的URL中键入/ index ...如何正确执行此操作?
Bal*_*usC 57
您需要将JSP文件放入/index.jsp而不是in /WEB-INF/jsp/index.jsp.这样整个servlet都是超级丰富的.
WebContent
|-- META-INF
|-- WEB-INF
| `-- web.xml
`-- index.jsp
Run Code Online (Sandbox Code Playgroud)
如果你非常肯定你需要以这种奇怪的方式调用servlet,那么你应该将它映射到URL模式/index.jsp而不是/index.您只需要更改它以获取请求调度程序request而不是config从中删除整个init()方法.
如果你真的打算有一个"主页servlet"(因此不是一个欢迎文件 - 它有一个完全不同的目的;即在请求文件夹时应该提供的默认文件,因此不是特定的根文件夹),然后你应该在空字符串URL模式上映射servlet.
<servlet-mapping>
<servlet-name>index</servlet-name>
<url-pattern></url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
另请参阅servlet映射url模式中的/和/*之间的差异.
ben*_*y23 23
我想你想要的是你的索引servlet充当欢迎页面,所以改为:
<welcome-file-list>
<welcome-file>index</welcome-file>
</welcome-file-list>
Run Code Online (Sandbox Code Playgroud)
这样就可以使用索引servlet了.注意,你需要一个servlet规范2.4容器才能做到这一点.
另请注意,@ BalusC获得了我的投票,因为您自己的索引servlet是多余的.