<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: servlet code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 27 Jul 2008 02:00:42 GMT</pubDate>
    <description>DZone Snippets: servlet code</description>
    <item>
      <title>Basic WEBrick setup for local web server</title>
      <link>http://snippets.dzone.com/posts/show/5659</link>
      <description>Put the following functions and aliases into your ~/.bash_login (or alternatives).&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;unset -f webrick_local&lt;br /&gt;&lt;br /&gt;function webrick_local() { &lt;br /&gt;&lt;br /&gt;   declare root_dir="${HOME}/Desktop/www"&lt;br /&gt;&lt;br /&gt;   /bin/mkdir -p "${root_dir}/WEBrickLog"&lt;br /&gt;   /bin/chmod 0754 "${root_dir}" "${root_dir}/WEBrickLog"&lt;br /&gt;&lt;br /&gt;   /usr/bin/touch "${root_dir}/WEBrickLog/webrick.pid" "${root_dir}/WEBrickLog/webrick_ruby.pid"&lt;br /&gt;   /bin/chmod 0644 "${root_dir}/WEBrickLog/webrick.pid" "${root_dir}/WEBrickLog/webrick_ruby.pid"&lt;br /&gt;&lt;br /&gt;#   /usr/bin/touch "${root_dir}/index.html"&lt;br /&gt;#   /bin/chmod 0754 "${root_dir}/index.html"&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;/usr/local/bin/ruby &lt;&lt;-HEREDOC&lt;br /&gt;&lt;br /&gt;   require 'webrick'&lt;br /&gt;   require 'webrick/accesslog'&lt;br /&gt;   include WEBrick&lt;br /&gt;&lt;br /&gt;   require 'thread'&lt;br /&gt;   require 'pp'&lt;br /&gt;&lt;br /&gt;   root_dir = "${root_dir}"&lt;br /&gt;   http_dir = File.expand_path(root_dir)&lt;br /&gt;&lt;br /&gt;   File.open(http_dir + "/WEBrickLog/webrick_ruby.pid", "w") do |f|&lt;br /&gt;      f.puts(Process.pid)&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   # cf. http://microjet.ath.cx/webrickguide/html/Logging.html&lt;br /&gt;   webrick_log_file = File.expand_path(http_dir + "/WEBrickLog/webrick.log")&lt;br /&gt;   #webrick_log_file = '/dev/null'  # disable logging&lt;br /&gt;   webrick_logger = WEBrick::Log.new(webrick_log_file, WEBrick::Log::DEBUG)&lt;br /&gt;&lt;br /&gt;   access_log_stream = webrick_logger&lt;br /&gt;   access_log = [[ access_log_stream, WEBrick::AccessLog::COMBINED_LOG_FORMAT ]]&lt;br /&gt;&lt;br /&gt;   system_mime_table = WEBrick::HTTPUtils::load_mime_types('/private/etc/httpd/mime.types.default')&lt;br /&gt;   system_mime_table.store('rhtml', 'text/html')   # add a mime type for .rhtml files&lt;br /&gt;   system_mime_table.store('php', 'text/html')&lt;br /&gt;   system_mime_table.store('rb', 'text/plain')&lt;br /&gt;   system_mime_table.store('pid', 'text/plain')&lt;br /&gt;   #pp system_mime_table.sort_by { |k,v| k }&lt;br /&gt;&lt;br /&gt;   server = WEBrick::HTTPServer.new(&lt;br /&gt;     :BindAddress     =&gt;    "localhost",&lt;br /&gt;     :Port            =&gt;    9090,&lt;br /&gt;     :DocumentRoot    =&gt;    http_dir,&lt;br /&gt;     :FancyIndexing   =&gt;    true,&lt;br /&gt;     :MimeTypes       =&gt;    system_mime_table,&lt;br /&gt;     :Logger          =&gt;    webrick_logger,&lt;br /&gt;     :AccessLog       =&gt;    access_log&lt;br /&gt;   )&lt;br /&gt;&lt;br /&gt;   server.config.store(:DirectoryIndex, server.config[:DirectoryIndex] &lt;&lt; "default.htm")&lt;br /&gt;   #pp server.config&lt;br /&gt;&lt;br /&gt;   # cf. http://snippets.dzone.com/posts/show/5208&lt;br /&gt;   class TimeServlet &lt; HTTPServlet::AbstractServlet&lt;br /&gt;&lt;br /&gt;      def do_GET(req, res)&lt;br /&gt;         res['Content-Type'] = 'text/html'&lt;br /&gt;         res.status = 200&lt;br /&gt;         res.body = "&lt;html&gt;Time: #{Time.now.to_s}&lt;/html&gt;" + "\n"&lt;br /&gt;      end&lt;br /&gt;&lt;br /&gt;      # cf. http://www.hiveminds.co.uk/node/244, published under the&lt;br /&gt;      # GNU Free Documentation License, http://www.gnu.org/copyleft/fdl.html&lt;br /&gt;&lt;br /&gt;      @@instance = nil&lt;br /&gt;      @@instance_creation_mutex = Mutex.new&lt;br /&gt;&lt;br /&gt;      def self.get_instance(config, *options)&lt;br /&gt;         #pp @@instance&lt;br /&gt;         @@instance_creation_mutex.synchronize { &lt;br /&gt;            @@instance = @@instance || self.new(config, *options) }&lt;br /&gt;      end&lt;br /&gt;&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   # cf. http://ttripp.blogspot.com/2007/01/fun-with-http.html&lt;br /&gt;   class PostDumper &lt; WEBrick::HTTPServlet::AbstractServlet&lt;br /&gt;  &lt;br /&gt;      # Reload file for each request, instantly&lt;br /&gt;      # updating the server with code changes &lt;br /&gt;      # without needing a restart.&lt;br /&gt;&lt;br /&gt;=begin&lt;br /&gt;      def PostDumper.get_instance( config, *options )&lt;br /&gt;         load __FILE__&lt;br /&gt;         PostDumper.new config, *options&lt;br /&gt;      end&lt;br /&gt;=end&lt;br /&gt;&lt;br /&gt;      # cf. http://www.hiveminds.co.uk/node/244, published under the&lt;br /&gt;      # GNU Free Documentation License, http://www.gnu.org/copyleft/fdl.html&lt;br /&gt;&lt;br /&gt;      @@instance = nil&lt;br /&gt;      @@instance_creation_mutex = Mutex.new&lt;br /&gt;&lt;br /&gt;      def self.get_instance(config, *options)&lt;br /&gt;         #pp @@instance&lt;br /&gt;         @@instance_creation_mutex.synchronize { &lt;br /&gt;            @@instance = @@instance || self.new(config, *options) }&lt;br /&gt;      end&lt;br /&gt;&lt;br /&gt;      def do_GET( request, response )&lt;br /&gt;         response.status = 200&lt;br /&gt;         response['Content-Type'] = "text/plain"&lt;br /&gt;         response.body = dump_request( request )&lt;br /&gt;      end&lt;br /&gt;  &lt;br /&gt;      def do_POST( request, response )&lt;br /&gt;         response.status = 200&lt;br /&gt;         response['Content-Type'] = "text/plain"&lt;br /&gt;         response.body = dump_request( request )&lt;br /&gt;         response.body &lt;&lt; request.body&lt;br /&gt;      end&lt;br /&gt;  &lt;br /&gt;      def dump_request( request )&lt;br /&gt;         request.request_line &lt;&lt; "\r\n" &lt;&lt;&lt;br /&gt;         request.raw_header.join( "" ) &lt;&lt; "\r\n"&lt;br /&gt;      end&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   server.mount("/dump", PostDumper)&lt;br /&gt;   server.mount("/time", TimeServlet, {:FancyIndexing=&gt;true})&lt;br /&gt;&lt;br /&gt;   # handle signals&lt;br /&gt;   %w(INT).each do |signal|&lt;br /&gt;      trap(signal) { server.shutdown }&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   server.start&lt;br /&gt;&lt;br /&gt;HEREDOC&lt;br /&gt;&lt;br /&gt;return 0&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;export -f webrick_local&lt;br /&gt;&lt;br /&gt;# start WEBrick alias&lt;br /&gt;alias webrick='swr'&lt;br /&gt;&lt;br /&gt;# start WEBrick&lt;br /&gt;unset -f swr&lt;br /&gt;function swr() {   &lt;br /&gt;  declare webrick_pid_file="${HOME}/Desktop/www/WEBrickLog/webrick.pid"&lt;br /&gt;  if [[ -e "${webrick_pid_file}" ]]; then echo "WEBrick already started!"; return 1; fi&lt;br /&gt;  /bin/bash -c "webrick_local" &amp;&lt;br /&gt;  declare WEBRICKPID=$!&lt;br /&gt;  mkdir -p "${HOME}/Desktop/www/WEBrickLog"&lt;br /&gt;  echo $WEBRICKPID &gt; "${HOME}/Desktop/www/WEBrickLog/webrick.pid"&lt;br /&gt;  return 0&lt;br /&gt;}&lt;br /&gt;export -f swr&lt;br /&gt;&lt;br /&gt;# quit WEBrick&lt;br /&gt;unset -f qwr&lt;br /&gt;function qwr() { &lt;br /&gt;   declare webrick_pid_file="${HOME}/Desktop/www/WEBrickLog/webrick.pid"&lt;br /&gt;   declare webrick_ruby_pid_file="${HOME}/Desktop/www/WEBrickLog/webrick_ruby.pid"&lt;br /&gt;   if [[ ! -e "${webrick_pid_file}" ]]; then echo "no such file: ${webrick_pid_file}"; return 1; fi&lt;br /&gt;   declare PIDW=$(cat "${webrick_pid_file}" 2&gt;/dev/null)&lt;br /&gt;   declare PIDR=$(cat "${webrick_ruby_pid_file}" 2&gt;/dev/null)&lt;br /&gt;   kill -INT $PIDW 2&gt;/dev/null || echo "no such PIDW: ${PIDW}"&lt;br /&gt;   kill -INT $PIDR 2&gt;/dev/null || echo "no such PIDR: ${PIDR}"&lt;br /&gt;   rm -f "${webrick_pid_file}" "${webrick_ruby_pid_file}"&lt;br /&gt;   return 0&lt;br /&gt;}&lt;br /&gt;export -f qwr&lt;br /&gt;&lt;br /&gt;# quit WEBrick; cf. Job Control Commands, http://tldp.org/LDP/abs/html/x8816.html&lt;br /&gt;###alias qwr='echo "quit WEBrick with ctrl-c ..."; fg %webrick 2&gt;/dev/null'&lt;br /&gt;&lt;br /&gt;alias openwww='/usr/bin/open http://localhost:9090'&lt;br /&gt;alias wrlog='/usr/bin/open http://localhost:9090/WEBrickLog/webrick.log'&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#----------------------------------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;source ~/.bash_login&lt;br /&gt;&lt;br /&gt;webrick&lt;br /&gt;&lt;br /&gt;openwww&lt;br /&gt;wrlog&lt;br /&gt;open http://localhost:9090 &lt;br /&gt;open http://localhost:9090/WEBrickLog&lt;br /&gt;open http://localhost:9090/WEBrickLog/webrick.log&lt;br /&gt;&lt;br /&gt;open http://localhost:9090/time&lt;br /&gt;open http://localhost:9090/dump&lt;br /&gt;&lt;br /&gt;wrlog&lt;br /&gt;qwr&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Further information:&lt;br /&gt;&lt;br /&gt;- &lt;a href="http://www.webrick.org"&gt;WEBrick&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://www.ruby-doc.org/stdlib/libdoc/webrick/rdoc/index.html"&gt;WEBrick RDoc&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://www.hiveminds.co.uk/node/245"&gt;What is WEBrick?&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://www.hiveminds.co.uk/node/244"&gt;Guide to using WEBrick for Rails development&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://www.igvita.com/2007/02/13/building-dynamic-webrick-servers-in-ruby/"&gt;Dynamic WEBrick Servers in Ruby&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://microjet.ath.cx/WebWiki/WEBrick.html"&gt;Gnome's Guide to WEBrick&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://codeidol.com/other/rubyckbk/Internet-Services/Running-Servlets-with-WEBrick/"&gt;Running Servlets with WEBrick&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://ruby.jamisbuck.org/rsphandler.rb"&gt;rsphandler&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://eigenclass.org/hiki/webrick-rewrite-rules"&gt;URL rewriting with WEBrick&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;</description>
      <pubDate>Tue, 17 Jun 2008 17:27:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5659</guid>
      <author>ntk ()</author>
    </item>
    <item>
      <title>Ruby Servlets</title>
      <link>http://snippets.dzone.com/posts/show/5208</link>
      <description>This WEBrick example demonstrates a basic servlet which displays the current time. Source code copied from &lt;a href="http://www.linuxjournal.com/article/8356"&gt;At the Forge - Getting Started with Ruby&lt;/a&gt; [linuxjournal.com].&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/sw/bin/ruby&lt;br /&gt;require 'webrick'&lt;br /&gt;include WEBrick&lt;br /&gt;&lt;br /&gt;# ---------------------------------------------&lt;br /&gt;# Define a new class&lt;br /&gt;class CurrentTimeServlet&lt;br /&gt;  &lt; WEBrick::HTTPServlet::AbstractServlet&lt;br /&gt;&lt;br /&gt;  def do_GET(request, response)&lt;br /&gt;    response['Content-Type'] = 'text/plain'&lt;br /&gt;    response.status = 200&lt;br /&gt;    response.body = Time.now.to_s + "\n"&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;# ----------------------------------------------&lt;br /&gt;# Create an HTTP server&lt;br /&gt;s = HTTPServer.new(&lt;br /&gt;  :Port            =&gt; 8000,&lt;br /&gt;  :DocumentRoot    =&gt; "/usr/local/apache/htdocs/"&lt;br /&gt;)&lt;br /&gt;&lt;br /&gt;s.mount("/time", CurrentTimeServlet)&lt;br /&gt;&lt;br /&gt;# When the server gets a control-C, kill it&lt;br /&gt;trap("INT"){ s.shutdown }&lt;br /&gt;&lt;br /&gt;# Start the server&lt;br /&gt;s.start&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;e.g. http://localhost:8001/time&lt;br /&gt;output&lt;br /&gt;&lt;code&gt;&lt;br /&gt;Mon Mar 10 23:06:58 +0000 2008&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Reference: http://www.webrick.org/</description>
      <pubDate>Mon, 10 Mar 2008 23:17:53 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5208</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Example file download servlet</title>
      <link>http://snippets.dzone.com/posts/show/4629</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  /**&lt;br /&gt;     *  Sends a file to the ServletResponse output stream.  Typically&lt;br /&gt;     *  you want the browser to receive a different name than the&lt;br /&gt;     *  name the file has been saved in your local database, since&lt;br /&gt;     *  your local names need to be unique.&lt;br /&gt;     *&lt;br /&gt;     *  @param req The request&lt;br /&gt;     *  @param resp The response&lt;br /&gt;     *  @param filename The name of the file you want to download.&lt;br /&gt;     *  @param original_filename The name the browser should receive.&lt;br /&gt;     */&lt;br /&gt;    private void doDownload( HttpServletRequest req, HttpServletResponse resp,&lt;br /&gt;                             String filename, String original_filename )&lt;br /&gt;        throws IOException&lt;br /&gt;    {&lt;br /&gt;        File                f        = new File(filename);&lt;br /&gt;        int                 length   = 0;&lt;br /&gt;        ServletOutputStream op       = resp.getOutputStream();&lt;br /&gt;        ServletContext      context  = getServletConfig().getServletContext();&lt;br /&gt;        String              mimetype = context.getMimeType( filename );&lt;br /&gt;&lt;br /&gt;        //&lt;br /&gt;        //  Set the response and go!&lt;br /&gt;        //&lt;br /&gt;        //&lt;br /&gt;        resp.setContentType( (mimetype != null) ? mimetype : "application/octet-stream" );&lt;br /&gt;        resp.setContentLength( (int)f.length() );&lt;br /&gt;        resp.setHeader( "Content-Disposition", "attachment; filename=\"" + original_filename + "\"" );&lt;br /&gt;&lt;br /&gt;        //&lt;br /&gt;        //  Stream to the requester.&lt;br /&gt;        //&lt;br /&gt;        byte[] bbuf = new byte[BUFSIZE];&lt;br /&gt;        DataInputStream in = new DataInputStream(new FileInputStream(f));&lt;br /&gt;&lt;br /&gt;        while ((in != null) &amp;&amp; ((length = in.read(bbuf)) != -1))&lt;br /&gt;        {&lt;br /&gt;            op.write(bbuf,0,length);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        in.close();&lt;br /&gt;        op.flush();&lt;br /&gt;        op.close();&lt;br /&gt;    }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 09 Oct 2007 23:12:04 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4629</guid>
      <author>frost137 (Douglas Wyatt)</author>
    </item>
    <item>
      <title>'ant' build file for HelloWorldServlet and JettyLauncher</title>
      <link>http://snippets.dzone.com/posts/show/4096</link>
      <description>A pretty straightforward 'ant' build file. You will need to edit the jetty.home property to indicate where you installed Jetty.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;br /&gt;&lt;project basedir="." default="compile" name="hello-servlet"&gt;&lt;br /&gt;  &lt;property name="build.dir"	location="classes" /&gt;&lt;br /&gt;  &lt;property name="src.dir"	location="src/main/java" /&gt;&lt;br /&gt;  &lt;property name="jetty.home"	location="/home/mrw/projects/jetty-6.1.3" /&gt;&lt;br /&gt;  &lt;property name="jetty.lib"	location="${jetty.home}/lib" /&gt;&lt;br /&gt;&lt;br /&gt;  &lt;path id="jetty.lib.path"&gt;&lt;br /&gt;    &lt;pathelement path="${jetty.lib}/jetty-6.1.3.jar" /&gt;&lt;br /&gt;    &lt;pathelement path="${jetty.lib}/jetty-util-6.1.3.jar" /&gt;&lt;br /&gt;    &lt;pathelement path="${jetty.lib}/servlet-api-2.5-6.1.3.jar" /&gt;&lt;br /&gt;  &lt;/path&gt;&lt;br /&gt;&lt;br /&gt;  &lt;target name="compile" description="Compile the project"&gt;&lt;br /&gt;    &lt;mkdir dir="${build.dir}" /&gt;&lt;br /&gt;    &lt;javac debug="true" destdir="${build.dir}" srcdir="${src.dir}"&lt;br /&gt;	classpathref="jetty.lib.path"&gt;&lt;br /&gt;      &lt;compilerarg value="-Xlint:unchecked" /&gt;&lt;br /&gt;      &lt;compilerarg value="-Xlint:deprecation" /&gt;&lt;br /&gt;    &lt;/javac&gt;&lt;br /&gt;  &lt;/target&gt;&lt;br /&gt;&lt;br /&gt;  &lt;target name="all"  depends="clean,compile"&lt;br /&gt;      description="Recompile from scratch"/&gt;&lt;br /&gt;&lt;br /&gt;  &lt;target name="server" depends="compile" description="Launch the server"&gt;&lt;br /&gt;    &lt;java classname="com.babblemind.JettyLauncher" fork="true"&lt;br /&gt;	classpathref="jetty.lib.path"&gt;&lt;br /&gt;      &lt;classpath&gt;&lt;br /&gt;	&lt;pathelement path="${build.dir}" /&gt;&lt;br /&gt;      &lt;/classpath&gt;&lt;br /&gt;    &lt;/java&gt;&lt;br /&gt;  &lt;/target&gt;&lt;br /&gt;&lt;br /&gt;  &lt;target name="clean" description="Delete all files created by compile"&gt;&lt;br /&gt;    &lt;delete dir="${build.dir}" /&gt;&lt;br /&gt;    &lt;delete dir="docs/api" /&gt;&lt;br /&gt;  &lt;/target&gt;&lt;br /&gt;&lt;/project&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 02 Jun 2007 11:15:45 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4096</guid>
      <author>mikewilsonuk (Mike Wilson)</author>
    </item>
    <item>
      <title>Minimal Java servlet</title>
      <link>http://snippets.dzone.com/posts/show/4095</link>
      <description>There is nothing particularly original here -- there are variations of this all over the place. This works nicely with my JettyLauncher class (see my other posts on DZone Snippets).&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;package com.babblemind;&lt;br /&gt;&lt;br /&gt;import javax.servlet.ServletException;&lt;br /&gt;import javax.servlet.http.HttpServlet;&lt;br /&gt;import javax.servlet.http.HttpServletRequest;&lt;br /&gt;import javax.servlet.http.HttpServletResponse;&lt;br /&gt;import java.io.IOException;&lt;br /&gt;import java.io.PrintWriter;&lt;br /&gt;&lt;br /&gt;public class HelloWorldServlet extends HttpServlet{&lt;br /&gt;    protected void doGet(HttpServletRequest httpServletRequest,&lt;br /&gt;	HttpServletResponse httpServletResponse)&lt;br /&gt;	    throws ServletException, IOException {&lt;br /&gt;&lt;br /&gt;        httpServletResponse.setContentType("text/plain");&lt;br /&gt;        PrintWriter out = httpServletResponse.getWriter();&lt;br /&gt;        out.println("Hello World!");&lt;br /&gt;        out.close();&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 02 Jun 2007 11:13:54 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4095</guid>
      <author>mikewilsonuk (Mike Wilson)</author>
    </item>
    <item>
      <title>Minimal Jetty http server with Java servlet</title>
      <link>http://snippets.dzone.com/posts/show/4094</link>
      <description>This will start a Jetty http server with a single Java servlet at the root. See http://www.mortbay.org/ for Jetty downloads. This needs HelloWorldServlet from one of my other posts. Make life easier by downloading my ant build.xml as well.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;package com.babblemind;&lt;br /&gt;&lt;br /&gt;import org.mortbay.jetty.Server;&lt;br /&gt;import org.mortbay.jetty.servlet.Context;&lt;br /&gt;import org.mortbay.jetty.servlet.ServletHolder;&lt;br /&gt;&lt;br /&gt;public class JettyLauncher {&lt;br /&gt;    public static void main(String[] args) throws Exception {&lt;br /&gt;	Server server = new Server(8080);    &lt;br /&gt;	Context root = new Context(server, "/", Context.SESSIONS);&lt;br /&gt;	root.addServlet(new ServletHolder(new HelloWorldServlet()), "/*");&lt;br /&gt;	server.start();&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 02 Jun 2007 11:11:50 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4094</guid>
      <author>mikewilsonuk (Mike Wilson)</author>
    </item>
    <item>
      <title>Retrieving all Parameters to a Java Servlet</title>
      <link>http://snippets.dzone.com/posts/show/3491</link>
      <description>// &lt;br /&gt;// Ref: http://forum.java.sun.com/thread.jspa?threadID=405328&amp;messageID=2184836&lt;br /&gt;// &lt;br /&gt;// The getParameterMap() returns a Map that has Strings as keys and String[] as&lt;br /&gt;// values. That's so you can have ?x=1&amp;x=2&amp;x=3 in your URL query string. The&lt;br /&gt;// keys in the Map must be unique but there can be multiple values for each&lt;br /&gt;// key.&lt;br /&gt;// &lt;br /&gt;// So.. when you pull a value out of a Map created by the getParameterMap()&lt;br /&gt;// method you must cast it to a String[] or else you'll get the value's&lt;br /&gt;// location in memory instead of it's actual value. If you're writing the&lt;br /&gt;// code and know for a fact that you'll always have only one value for each&lt;br /&gt;// param then you can just use yourVar[0] but if there may be multiple&lt;br /&gt;// values for each key then you'll need to loop over each value array.&lt;br /&gt;// &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    Map params = request.getParameterMap();&lt;br /&gt;    Iterator i = params.keySet().iterator();&lt;br /&gt;    &lt;br /&gt;    while ( i.hasNext() )&lt;br /&gt;      {&lt;br /&gt;        String key = (String) i.next();&lt;br /&gt;        String value = ((String[]) params.get( key ))[ 0 ];&lt;br /&gt;      }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 10 Feb 2007 13:16:54 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3491</guid>
      <author>thitiv (Thiti V. Sintopchai)</author>
    </item>
    <item>
      <title>Simple Java Servlet</title>
      <link>http://snippets.dzone.com/posts/show/3489</link>
      <description>// Simple Servlet Skeleton&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;package net.tvs.servlet;&lt;br /&gt;&lt;br /&gt;import java.io.IOException;&lt;br /&gt;&lt;br /&gt;import javax.servlet.ServletConfig;&lt;br /&gt;import javax.servlet.ServletException;&lt;br /&gt;import javax.servlet.http.HttpServlet;&lt;br /&gt;import javax.servlet.http.HttpServletRequest;&lt;br /&gt;import javax.servlet.http.HttpServletResponse;&lt;br /&gt;&lt;br /&gt;import org.apache.commons.logging.Log;&lt;br /&gt;import org.apache.commons.logging.LogFactory;&lt;br /&gt;&lt;br /&gt;public class MyServlet extends HttpServlet&lt;br /&gt;{&lt;br /&gt;  private static Log _logger = LogFactory.getLog( MyServlet.class );&lt;br /&gt;&lt;br /&gt;  public void init(ServletConfig config) throws ServletException&lt;br /&gt;  {&lt;br /&gt;    super.init( config );&lt;br /&gt;    try&lt;br /&gt;      {&lt;br /&gt;        // Initialization&lt;br /&gt;      }&lt;br /&gt;    catch ( Exception e )&lt;br /&gt;      {&lt;br /&gt;        _logger.fatal( e.getMessage() );&lt;br /&gt;      }&lt;br /&gt;    _logger.info( "MyServlet initialized" );&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  protected void doGet(HttpServletRequest request, HttpServletResponse response)&lt;br /&gt;      throws ServletException, IOException&lt;br /&gt;  {&lt;br /&gt;    processRequest( request, response );&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  protected void doPost(HttpServletRequest request, HttpServletResponse response)&lt;br /&gt;      throws ServletException, IOException&lt;br /&gt;  {&lt;br /&gt;    processRequest( request, response );&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  protected void processRequest(HttpServletRequest request,&lt;br /&gt;      HttpServletResponse response) throws ServletException, IOException&lt;br /&gt;  {&lt;br /&gt;    response.setContentType( "text/html;charset=UTF-8" );&lt;br /&gt;    String param = request.getParameter( "param" );&lt;br /&gt;    _logger.debug( "Received param: " + param );&lt;br /&gt;&lt;br /&gt;    // Implementation...&lt;br /&gt;&lt;br /&gt;    PrintWriter out = response.getWriter();&lt;br /&gt;    response.setContentType( "text/xml" );&lt;br /&gt;    response.setHeader( "Cache-Control", "no-cache" );&lt;br /&gt;&lt;br /&gt;    out.println( "&lt;response&gt;" );&lt;br /&gt;    out.println( "&lt;param&gt;" + param + "&lt;/param&gt;" );&lt;br /&gt;    out.println( "&lt;/response&gt;" );&lt;br /&gt;    out.close();&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 10 Feb 2007 10:08:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3489</guid>
      <author>thitiv (Thiti V. Sintopchai)</author>
    </item>
    <item>
      <title>avoid web caching</title>
      <link>http://snippets.dzone.com/posts/show/2708</link>
      <description>&lt;code&gt;&lt;br /&gt;// Set to expire far in the past.&lt;br /&gt;response.setHeader("Expires", "Sat, 6 May 1995 12:00:00 GMT");&lt;br /&gt;// Set standard HTTP/1.1 no-cache headers.&lt;br /&gt;response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");&lt;br /&gt;// Set IE extended HTTP/1.1 no-cache headers (use addHeader).&lt;br /&gt;response.addHeader("Cache-Control", "post-check=0, pre-check=0");&lt;br /&gt;// Set standard HTTP/1.0 no-cache header.&lt;br /&gt;response.setHeader("Pragma", "no-cache");&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 25 Sep 2006 00:28:46 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2708</guid>
      <author>remvee (Remco van 't Veer)</author>
    </item>
    <item>
      <title>A Client For the XML-RPC Servlet</title>
      <link>http://snippets.dzone.com/posts/show/1636</link>
      <description>// When combined with the Apache XML-RPC library this code&lt;br /&gt;// will let you call the servlet in the snippet "An XML-RPC&lt;br /&gt;// Servlet". Of course, since XML-RPC is pretty ubiquitous&lt;br /&gt;// you can also use this code to call servers in dozens of&lt;br /&gt;// other languages as well.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import java.util.Vector;&lt;br /&gt;&lt;br /&gt;import org.apache.commons.logging.Log;&lt;br /&gt;import org.apache.commons.logging.LogFactory;&lt;br /&gt;import org.apache.xmlrpc.XmlRpcClient;&lt;br /&gt;&lt;br /&gt;public class XMLRPCTestClient {&lt;br /&gt;    private static final String serverAddress = &lt;br /&gt;	"http://localhost:8080/lol/remoteapi";&lt;br /&gt;    private static Log log = LogFactory.getLog(XMLRPCTestClient.class);&lt;br /&gt;    &lt;br /&gt;    /** Creates a new instance of XMLRPCTestClient */&lt;br /&gt;    public XMLRPCTestClient(String address) {&lt;br /&gt;        try {&lt;br /&gt;            XmlRpcClient xmlrpc = new XmlRpcClient(address);&lt;br /&gt;&lt;br /&gt;            Vector params = new Vector();&lt;br /&gt;            params.addElement("Hello World! Hello!");&lt;br /&gt;&lt;br /&gt;            try {&lt;br /&gt;                // this method returns a string&lt;br /&gt;                String result = (String) xmlrpc.execute("echo.echo", params);&lt;br /&gt;                System.out.println(result);&lt;br /&gt;            } catch (Exception e) {&lt;br /&gt;                log.error("The remote procedure call failed.", e);&lt;br /&gt;            }&lt;br /&gt;        } catch (java.net.MalformedURLException mue) {&lt;br /&gt;            log.error(&lt;br /&gt;                "The address given for the XML-RPC interface is bad: " + address);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    /**&lt;br /&gt;     * @param args the command line arguments&lt;br /&gt;     */&lt;br /&gt;    public static void main(String[] args) {&lt;br /&gt;        XMLRPCTestClient xmlRPCTestClient = new XMLRPCTestClient(&lt;br /&gt;            serverAddress);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 06 Mar 2006 01:50:55 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1636</guid>
      <author>JohnMunsch (John Munsch)</author>
    </item>
  </channel>
</rss>
