Servlet and Applet

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Servlet and Applet

Diana Huang
Hi All,
 
I embedded Jetty (5.1) into my application and started it. I added a Servlet to the jetty servlethttpcontext. That servlet is to generate a html which loads a Applet. Since both my servlet and applet classes have package accessories, I am not sure how to set codebase in the applet tag.
 
What I try to do is pretty simple. Starting a Jetty server. from a browser when I input <A href="http://hostname:port/servletpath">http://hostname:port/servletpath, (my example is http://localhost:8000/Servlet) , i should see a hello string (StringApplet.class).
 
If I don't have package accessory limitation, I know how to set codebase here. For my situation, I really need a hand. Please let me know if I miss something here. Thank you
-------------------------------------------------------------------------------------------
some code snap:
 
MyJetty.java:
 
package com.majitek.maji.rpc.server;
..........
context = (ServletHttpContext) server.addContext("/maji");
 
 context.addHandler(new SecurityHandler());
 
 server.start();
 
context.addServlet("test1","/Servlet1",Servlet1.class.getName());
 
.................
 
Servlet1.java
 
package com.majitek.maji.rpc.test;
........
protected void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException, IOException{
  
  res.setContentType("text/html");
  PrintWriter out = res.getWriter();
  out.println("<HTML><HEAD><TITLE>Test</TITLE>"+
                  "</HEAD><BODY>");
  
  out.println("<applet code=test.StringApplet.class width=200 height=50 codebase=?????>");
  
  out.println("</applet>");
  out.println("</BODY></HTML>");
  out.close();
 }
 
StringApplet.java
 
package test;
 
import java.applet.*;
import java.awt.Graphics;
 
public class StringApplet extends Applet {
  public void paint(Graphics g) {
        g.drawString("hello", 50, 25);
    }
}
 
Files location:
 
                                               c:\source
                                                     |         \
                                                com         test
                                                    |            \
                                              majitek    StringApplet.class
                                                    |
                                                maji
                                                    |
                                                  rpc
                                              /              \
                                       server                test
                                          |                        |
                                MyJetty.class       Servlet1.class
                                  
Reply | Threaded
Open this post in threaded view
|

Re: Servlet and Applet

Niclas Hedhman
On Thursday 08 September 2005 10:10, Diana Huang wrote:
> If I don't have package accessory limitation, I know how to set codebase
> here.

You can do two ways, depending on your circumstances. If you have a very
static codebase, you just add a ResourceHandler, mapping a directory to the
codebases, for instance /codebases. Then you specify a
  /codebases/abc.jar
as the codebase in your applet. This will lead to a total download of the
entire Jar in one go.

If you have many fairly small applets, which each only uses a fraction of the
total codebase, I would recommend that you add your own HttpHandler and map
that to a path (m_ContextPath below), and populate a classloader
(m_ClassLoader) in that handler with the codebase, and serve the classes
individually, such as;

public void handle( String pathInContext, String pathParams,
                    HttpRequest request, HttpResponse response )
    throws IOException
{
    if( pathInContext.startsWith( m_ContextPath ) )
    {
        int pos = m_ContextPath.length();
        pathInContext = pathInContext.substring( pos );
        InputStream in = m_ClassLoader.getResourceAsStream( pathInContext );
        OutputStream out = response.getOutputStream();
        StreamUtils.copy( in, out );
        request.setHandled( true );
        out.close();
        in.close();
    }
}

And map the applet codebase parameter to the m_ContextPath value. When low
latency is needed, and the Jars are 'large' this works nicely.


Cheers
Niclas


-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
jetty-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jetty-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Servlet and Applet

Diana Huang
Hi Niclas,

Thanks for your email and sorry for the delay of response

> You can do two ways, depending on your circumstances. If you have a very
> static codebase, you just add a ResourceHandler, mapping a directory to
> the
> codebases, for instance /codebases. Then you specify a
>  /codebases/abc.jar
> as the codebase in your applet. This will lead to a total download of the
> entire Jar in one go.
>
Do you mean setting an absolute directory path as  the codebase in the
applet? how to set it in Windows? Is it c:/codebases/abc.jar?

Cheers, Diana



-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
jetty-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jetty-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Servlet and Applet

Greg Wilkins-5

Diana,

setting a codebase on an applet only affects the URL the browser will
try to use to get the code.

It will make another request to Jetty asking for the code (check your request
log, it is probably doing this already).   You need to arrange for Jetty to
serve the jar or class files to the browser.... Follow the suggestions from
Niclas for this (or also just use a webapp (see tutorial)).

cheers


Diana Huang wrote:

> Hi Niclas,
>
> Thanks for your email and sorry for the delay of response
>
>> You can do two ways, depending on your circumstances. If you have a very
>> static codebase, you just add a ResourceHandler, mapping a directory
>> to the
>> codebases, for instance /codebases. Then you specify a
>>  /codebases/abc.jar
>> as the codebase in your applet. This will lead to a total download of the
>> entire Jar in one go.
>>
> Do you mean setting an absolute directory path as  the codebase in the
> applet? how to set it in Windows? Is it c:/codebases/abc.jar?
>
> Cheers, Diana
>
>
> -------------------------------------------------------
> SF.Net email is Sponsored by the Better Software Conference & EXPO
> September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
> Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
> Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf



-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
jetty-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jetty-discuss