我的文件WebContent/jsps夹中的JSP文件中有一个HTML表单.我servlet.java在src文件夹中的默认包中有一个servlet类.在我web.xml的映射为/servlet.
我action在HTML表单的属性中尝试了几个URL :
<form action="/servlet">
Run Code Online (Sandbox Code Playgroud)
<form action="/servlet.java">
Run Code Online (Sandbox Code Playgroud)
<form action="/src/servlet.java">
Run Code Online (Sandbox Code Playgroud)
<form action="../servlet.java">
Run Code Online (Sandbox Code Playgroud)
但这些都不起作用.他们都在Tomcat 6/7/8中继续返回如下所示的HTTP 404错误:
HTTP状态404 - /servlet
描述:请求的资源(/ servlet)不可用.
或者如下面的Tomcat 8.5/9:
HTTP状态404 - 未找到
消息:/ servlet
描述:源服务器没有找到目标资源的当前表示,或者不愿意透露存在该资源
为什么不起作用?
我在我的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) 我有一个小maven(间接通过Netbeans 8.1和tomcat设置)
每当我运行项目时,它会在根目录上打开带有HelloWord的浏览器:
即页面http://localhost:8084/是:
<html>
<head>
<title>Start Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我试图创建一个servlet来替换它:
@WebServlet(name = "HomeServlet", urlPatterns = {"/"}) 但是,它没有按预期工作.
即它仍然表现出同样的问候世界: http://localhost:8084
但它确实搞乱了根目录上的所有文件,即http://localhost:8084/foo.css由这个servlet处理并获得响应.
所以,我的问题是(实际上是两个):
如何将此页面的内容更改为其他内容?
或者,至少(如果前者不可能):我可以在根路径上使用永久重定向来避免用户看到此页面吗?
(即http代码301)将用户移动到 http://localhost:8084/home
当我使用Eclipse中的Tomcat 8.0启动Java EE项目时,我无法设置在浏览器中加载的默认页面.我正在尝试学习JSF,所以我遵循了本教程
一切正常,但是当我右键单击login.xhtml或welcome.xhtml文件并选择"在服务器上运行/运行" 时,我只能看到创建的页面.
到目前为止,我创建的所有其他Web应用程序在我启动整个项目时都加载了默认页面.默认行为是加载index.html页面(如果有的话,可能是index.jsp).所以我添加index.html和index.xhtml页面到我的WEB-INF项目文件夹,希望其中至少有一个将被显示.然而,没有任何反应.浏览器始终只显示localhost:8080/JSFFaceletsTutorial/URL 上的页面,但页面是白色的,甚至不是错误消息.我想我一直在解决这个问题的过程中遇到错误404,但是,我不再能够重现这个错误,我不记得是什么导致了它.
我发现可以更改默认的起始页面
但是,它对我也不起作用.无论我是否编辑web.xml文件,我都得到相同的结果.
更令人费解的是,当我尝试更改网络浏览器时:"窗口/网络浏览器/ ..."它在外部网络浏览器中的作用时间与内部Eclipse网络浏览器不同.内部总是空白页面 - 但外部网络浏览器曾经设法显示index.html页面 - 但它是一些过时的版本.尽管我完全确定我编辑了它,保存了更改,重新启动了服务器......但它仍然显示了页面的过时版本.即使在这种情况下,它仍然忽略了web.xml文件中所做的更改.但是当我现在尝试它时,它再次在所有浏览器中显示空白页面.除了编辑web.xml文件外,我不知道我做了什么改变......
我的猜测是问题在于我还没有完全掌握的JSF技术.这是因为当我选择使用右键单击"运行方式/在服务器上运行" 来运行login.xhtml和welcome.xhtml页面时,这些页面的URL将显示localhost:8080:路径/JSFFaceletsTutorial/faces/login.xhtml和/JSFFaceletsTutorial/faces/welcome.xhtml.这很奇怪,因为我的项目中没有任何目录"faces".键入所有可能的排列:
<welcome-file-list>
<welcome-file>faces/index.html</welcome-file>
<welcome-file>faces/index.xhtm</welcome-file>
</welcome-file-list>
Run Code Online (Sandbox Code Playgroud)
在这web.xml也没有帮助.当我在那里输入完整地址时,它没有帮助.
以下是我在控制台中收到的警告(我跳过了INFO日志条目):
"2014年12月19日上午9:39:55 org.apache.tomcat.util.digester.SetPropertiesRule开始警告:[SetPropertiesRule] {Server/Service/Engine/Host/Context}将属性'source'设置为'org.eclipse. jst.jee.server:JSFFaceletsTutorial'找不到匹配的属性....警告:JSF1074:已经注册了名为'loginBean'的托管bean.用com.tutorial.LoginBean替换现有的托管bean类com.tutorial.LoginBean .2014年12月19日上午9:39:57 org.apache.coyote.AbstractProtocol start"
我不确定这是否有用.我现在没有想法了.
servlets ×3
homescreen ×2
jsp ×2
welcome-file ×2
eclipse ×1
forms ×1
html ×1
jsf ×1
tomcat ×1
web.xml ×1