在eclipse java web应用程序中引用特定资源的URL

时间:2013-02-22 21:52:08

标签: java eclipse jsp servlets applet

有人能告诉我如何使用java servlets指定特定资源的URL吗?我正在使用eclipse。下面的代码生成一个servlet,其主页面可以在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.myapplets中,它位于我的eclipse项目中的myapp / src / myapp.myapplets / ThatThereApplet.java路径中。或者,我还在test_applets_1.jar中捆绑了ThatThereApplet.class,我将它放在我的eclipse项目的myapp / web / WEB-INF / lib文件夹中,然后添加它以使用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时,应用程序无法找到applet的位置,因此即使thishere.jsp加载,applet也不会加载。我在网络浏览器中加载了that.here.jsp时运行了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中吗?

1 个答案:

答案 0 :(得分:1)

将web.xml中的url-pattern更改为

<servlet-mapping>
    <servlet-name>thisservlet</servlet-name>
    <url-pattern>/thisservlet/thishere.jsp</url-pattern>
</servlet-mapping>