1

こんにちは、Web アプリを実行しています。「サーバーで実行」を選択すると、ブラウザーに次のエラーが表示されます。

HTTP Status 404 - /ServletsJSPExample/

--------------------------------------------------------------------------------

type Status report

message /ServletsJSPExample/

description The requested resource is not available.


--------------------------------------------------------------------------------

Apache Tomcat/7.0.47

URL は次のようになります。

http://localhost:8080/ServletsJSPExample/Serv1

しかし、それは

http://localhost:8080/ServletsJSPExample/

明確にするために、アドレスを自分で書き留める場合はすべて問題ありませんが、アプリを実行するときにアドレスが既に存在するようにしたい.

そして、ここに私のweb.xmlがあります:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>ServletsJSPExample</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>ServletExample</display-name>
    <servlet-name>ServletExample</servlet-name>
    <servlet-class>com.example.tutorial.ServletExample</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ServletExample</servlet-name>
    <url-pattern>/Serv1</url-pattern>
  </servlet-mapping>
</web-app>


そして、ここにサーブレットクラスファイルがあります:

package com.example.tutorial;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletExample extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("Hello Java");
    }


}

Tomcat 7 と Eclipse Juno を使用しています。

4

2 に答える 2

2

1) ウェルカム ファイルを追加できます。

<welcome-file>Serv1</welcome-file>

このアプローチでは、すべてのディレクトリにウェルカム ファイルを使用します。たとえば、ディレクトリfoo/barがあり、ユーザーが要求http://server/ServletsJSPExample/foo/bar/し、そこに直接マップされたものが何もない場合、コンテナもhttp://server/ServletsJSPExample/foo/bar/Serv1.

2)マッピング フォームに加えて、から/へのマッピングを追加することもできます。ServletExampleServ1

<servlet-mapping>
    <servlet-name>ServletExample</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

ただし、これにより「デフォルトのサーブレット」になります。つまり、別のリソースまたはサーブレットに明示的にマップされていないすべての URL を提供します。コードでそれを考慮すればOKです。

3)index.jspリダイレクトを行うルート フォルダーに を作成できます。

<% response.sendRedirect("Serv1"); %>

先頭にスラッシュがないことに注意してください。つまり、URL は相対です。この場合、2 つの要求(元の要求とリダイレクト) があり、それに応じてブラウザーの URL が変更されます。

または、JSP は転送を実行できます。

<jsp:forward page="Serv1" />

この場合、単一の要求があり、ブラウザーの URL は同じままです。

于 2013-11-06T11:49:48.403 に答える