0

Java サーブレットを使用して特定のリソースの URL を指定する方法を教えてもらえますか? エクリプスを使用しています。以下のコードは、URL でメイン ページを表示できるサーブレットを生成します。

http://www.mysite.com/myapp/thisservlet/  

代わりに、次の URL で特定の jsp にアクセスできるようにしたいと考えています。

http://www.mysite.com/myapp/thisservlet/thishere.jsp  

また、thishere.jsp が、Eclipse のアプリケーション ディレクトリ構造の別の場所にあるコードを呼び出せるようにしたいと考えています。たとえば、私の Eclipse プロジェクトでは、thishere.jsp は myapp/web/WEB-INF/web フォルダーにあります。ただし、ThatThereApplet.java は、パス myapp/src/myapp.myapplets/ThatThereApplet.java の私の Eclipse プロジェクトにあるパッケージ myapp.myapplets にあります。あるいは、Eclipse プロジェクトの myapp/web/WEB-INF/lib フォルダーに配置した ThatThereApplet.class を test_applets_1.jar にバンドルしてから、Eclipse のコンテキスト メニューを使用してアプリケーションのビルド パスに追加しました。 .

上で説明したことを行うように、以下のコードを変更する方法を誰か教えてもらえますか?

ThisServlet.java のコードは次のとおりです。

package myapp.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;

public class ThisServlet extends HttpServlet {

   private RequestDispatcher jsp;

   public void init(ServletConfig config) throws ServletException {
      ServletContext context = config.getServletContext();
      jsp = context.getRequestDispatcher("/WEB-INF/jsp/thishere.jsp");
   }

   protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
   throws ServletException, IOException {
      jsp.forward(req, resp);
   }
}

thishere.jsp のコードは次のとおりです。

<html>
<head>
<title>Test Applets Go Here</title>
</head>
<body>
<h1>Gonna put an applet below:</h1>
<applet code="myapp.myapplets.ThatThereApplet.class" archive="test_applets_1.jar" width="500" height="500">
<h1>The applet should be above this line.</h1>
</body>
</html>

test_applets_1.jar 内にカプセル化し、ビルド パスに追加したコードを次に示します。

package myapp.myapplets;
import java.applet.*;
import java.awt.*;

public class ThatThereApplet extends Applet {

   int width, height;

   public void init() {
      width = getSize().width;
      height = getSize().height;
      setBackground( Color.black );
   }

   public void paint( Graphics g ) {
      g.setColor( Color.green );
      for ( int i = 0; i < 10; ++i ) {
         g.drawLine( width, height, i * width / 10, 0 );
      }
   }
}

web.xml の関連部分は次のとおりです。

<servlet>
    <servlet-name>thisservlet</servlet-name>
    <servlet-class>myapp.web.ThisServlet</servlet-class>
</servlet>

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

編集:


Sudhakar の素晴らしいアドバイスに従い、web.xml を次のように変更しました。

<url-pattern>/thisservlet/thishere.jsp</url-pattern>  

しかし、ブラウザで thishere.jsp をロードすると、アプリケーションがアプレットの場所を見つけることができないため、thishere.jsp がロードされてもアプレットはロードされません。thatthere.jsp を Web ブラウザにロードしたときに Java コンソールを実行したところ、生成されたエラー ログは次のとおりです。

load: class myapp.myapplets.ThatThereApplet.class not found.
java.lang.ClassNotFoundException: myapp.myapplets.ThatThereApplet.class
    at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Exception: java.lang.ClassNotFoundException: myapp.myapplets.ThatThereApplet.class

アプリケーションが ThatThereApplet.class を見つけて thishere.jsp に正常にロードできるように、上記のコードを変更する方法を教えてもらえますか?

4

1 に答える 1

1

url-patternweb.xml の をに変更します

<servlet-mapping>
    <servlet-name>thisservlet</servlet-name>
    <url-pattern>/thisservlet/thishere.jsp</url-pattern>
</servlet-mapping> 
于 2013-02-23T05:44:38.587 に答える