找到你要的答案

Q:Set servlet as default home page in web.xml [duplicate]

Q:设置默认主页在web.xml servlet作为[重复]

This question already has an answer here:

I've a servlet registered in web.xml as below.

<servlet>
    <servlet-name>Manager</servlet-name>
    <servlet-class>Manager</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Manager</servlet-name>
    <url-pattern>/RequestManager</url-pattern>
</servlet-mapping>

Basically I want to call this servlet as my default home page when I open http://localhost:8080/appname. So, I tried registering it as welcome file in same web.xml as below:

<welcome-file-list>
    <welcome-file>Manager</welcome-file>
</welcome-file-list>

But, when I run the project, I get an error saying "requested resource not available". However, if I write in the url with my servlet URL pattern, it works fine.

这个问题在这里已经有了答案:

我已经注册在web.xml如下一个servlet。

<servlet>
    <servlet-name>Manager</servlet-name>
    <servlet-class>Manager</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Manager</servlet-name>
    <url-pattern>/RequestManager</url-pattern>
</servlet-mapping>

基本上我想打电话给这个servlet作为我的默认主页时我打开HTTP:/ /本地:8080 /修改。所以,我试着注册在相同的web.xml欢迎文件如下:

<welcome-file-list>
    <welcome-file>Manager</welcome-file>
</welcome-file-list>

但是,当我运行这个项目时,我有一个错误,说“请求的资源不可用”。然而,如果我写我的servlet的URL模式的URL,它工作正常。

answer1: 回答1:

Specify an empty string as servlet's URL pattern.

<servlet>
    <servlet-name>Manager</servlet-name>
    <servlet-class>Manager</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Manager</servlet-name>
    <url-pattern></url-pattern>
</servlet-mapping>

See also:


Unrelated to the concrete problem, the <welcome-file> should represent an URL path, not a servlet name. It'd have worked if you specifed <welcome-file>RequestManager</welcome-file>. But this affects all subfolders. Actually, the <welcome-file> has an entirely different meaning than "home page file" you've had in mind. It represents the default resource which should be served when a folder is been requested.

指定一个空字符串作为servlet的URL模式。

<servlet>
    <servlet-name>Manager</servlet-name>
    <servlet-class>Manager</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Manager</servlet-name>
    <url-pattern></url-pattern>
</servlet-mapping>

See also:


对具体问题无关,<;欢迎文件& gt;应该代表一个URL路径,而不是一个servlet的名字。它会工作,如果你指定<;欢迎文件& gt;requestmanager & lt;/欢迎文件& gt;。但这影响到所有的子文件夹。其实,<;欢迎文件& gt;具有完全不同的含义比“主页文件“你有思想。它代表默认的资源时,应送达文件夹时被要求。

answer2: 回答2:

You can use index.jsp to forward to your servlet.

<jsp:forward page="servlet_context">

and add index.jsp as welcome file in web.xml

你可以使用index.jsp向servlet。

<jsp:f或ward page="servlet_context">

并添加index.jsp文件在web.xml的欢迎

answer3: 回答3:

inside servlet class you can forward Control Using :

request.getRequestDispatcher("forward page URL").forward(req,res);

or else if you are using JSP then use

<% RequestDispatcher rd = servletContext.getRequestDispatcher("/pathToResource");
rd.forward(request, response); %>

or

<jsp:forward page="relative URL" />

在servlet类你可以控制使用:

request.getRequestDispatcher("f或ward page URL").f或ward(req,res);

否则,如果你使用的是JSP,然后使用

<% RequestDispatcher rd = servletContext.getRequestDispatcher("/pathToResource");
rd.f或ward(request, response); %>

<jsp:f或ward page="relative URL" />
servlets  web.xml  welcome-file