Can anyone show me how to specify urls of specific resources using java servlets? I am using eclipse. The code below produces a servlet whose main page can be viewed at the url:
http://www.mysite.com/myapp/thisservlet/
Instead, I want to be able to access the specific jsp at the url:
http://www.mysite.com/myapp/thisservlet/thishere.jsp
Also, I would like for thishere.jsp to be able to call code that is located somewhere else in my application directory structure in eclipse. For example, within my eclipse project, thishere.jsp is located in the myapp/web/WEB-INF/web folder. However, ThatThereApplet.java is located in package myapp.myapplets, which is located in my eclipse project in the path myapp/src/myapp.myapplets/ThatThereApplet.java. Alternatively, I have also bundled ThatThereApplet.class in test_applets_1.jar, which I placed in the myapp/web/WEB-INF/lib folder in my eclipse project before adding it to adding it to my application's build path using the context menu in eclipse.
Can anyone show me how to alter the code below so that it does what I have described above?
Here is the code for 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);
}
}
Here is the code for 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>
Here is the code that I encapsulated within test_applets_1.jar and added to the build path:
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 );
}
}
}
Here is the relevant part of 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>
EDIT:
I followed Sudhakar's great advice and changed web.xml to indicate:
<url-pattern>/thisservlet/thishere.jsp</url-pattern>
However, when I load thishere.jsp in my browser, the application is not able to find the location of the applet, so the applet does not load even though thishere.jsp does load. I ran the Java Console when I loaded thatthere.jsp in my web browser, and the following is the error log it produced:
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
Can anyone show me how to change the code above so that the application is able to find ThatThereApplet.class and successfully load it into thishere.jsp?