Saturday, 29 October 2011

Servlets FAQ -Part1


How do I set my CLASSPATH for servlets?
Location: http://www.jguru.com/faq/view.jsp?EID=141
Created: Sep 3, 1999 Modified: 2001-05-05 14:19:15.228
Author: Alex Chaffee (http://www.jguru.com/guru/viewbio.jsp?EID=3)

That depends.
For developing servlets, just make sure that the JAR file containing javax.servlet.* is in your CLASSPATH, and use your normal development tools (javac and so forth).
  • For JSDK: JSDK_HOME/lib/jsdk.jar
  • For Tomcat: TOMCAT_HOME/lib/servlet.jar
For running servlets, you need to set the CLASSPATH for your servlet engine. This varies from engine to engine. Each has different rules for how to set the CLASSPATH, which libraries and directories should be included, and which libraries and directories should be excluded. Note: for engines that do dynamic loading of servlets (e.g. JRun, Apache Jserv, Tomcat), the directory containing your servlet class files shoud not be in your CLASSPATH, but should be set in a config file. Otherwise, the servlets may run, but they won't get dynamically reloaded.
The Servlets 2.2 spec says that the following should automatically be included by the container, so you shouldn't have to add them to your CLASSPATH manually. (Classloader implementations are notoriously buggy, though, so YMMV.)
  • classes in the webapp/WEB-INF/classes directory
  • JAR files in the webapp/WEB-INF/lib directory
This applies to webapps that are present on the filesystem, and to webapps that have been packaged into a WAR file and placed in the container's "webapps" directory. (e.g. TOMCAT_HOME/webapps/myapp.war)
The Complete CLASSPATH Guide for Servlets (http://www.meangene.com/java/classpath.html) by Gene McKenna (mckenna@meangene.com) has detailed instructions on how to set your CLASSPATH for JavaWebServer and JRun.
Comments and alternative answers
Important precision : Under Apache (at least with...
Author: Denis BUCHER (http://www.jguru.com/guru/viewbio.jsp?EID=7742), Jan 22, 2000
Important precision : Under Apache (at least with jserv under Linux) you SHOULD NOT put this in any system path, as it won't work. You must put the new path into jserv.properties file, that you located maybe into your apache config dir !!!

I've found it simpler to just copy the JAR file to...
Author: John Zukowski (http://www.jguru.com/guru/viewbio.jsp?EID=7), Jun 15, 2000
I've found it simpler to just copy the JAR file to the extensions directory of your runtime:
jre/lib/ext
under your JDK directory, as in:
c:\jdk1.3\jre\lib\ext


If you are using JRun (atleast with v2.3.3), adding...
Author: sharad gupta (http://www.jguru.com/guru/viewbio.jsp?EID=100198), Jul 21, 2000
If you are using JRun (atleast with v2.3.3), adding servlet classes in system path won't work. Instead add the path in jsm.properties file.

Unfortunately, Jaz' extension solution doesn't work...
Author: Alex Chaffee (http://www.jguru.com/guru/viewbio.jsp?EID=3), Sep 17, 2000
Unfortunately, Jaz' extension solution doesn't work for some JARs, notably XML parsers. I'm not exactly sure why; perhaps it's package name collision. The webapp solution (WEB-INF/lib) is preferred.

I would not hesitate to have a static block in one...
Author: Aprameya Paduthonse (http://www.jguru.com/guru/viewbio.jsp?EID=4707), Feb 1, 2001
I would not hesitate to have a static block in one of my early loaded servlets to list the environment parameters
static
{
    Properties envProps = System.getProperties();
    System.out.println("__________________________ BEGIN JAVA SETTINGS _______________________________");
    for (Enumeration e = envProps.propertyNames() ; e.hasMoreElements() ;)
    {
        String prop = (String)e.nextElement();
        System.out.println(prop + " : " + envProps.getProperty(prop));
    }
    System.out.println("_____________________________END JAVA SETTINGS _______________________________");
}

I would not hesitate to have a static block in one...
Author: Aprameya Paduthonse (http://www.jguru.com/guru/viewbio.jsp?EID=4707), Feb 1, 2001
I would not hesitate to have a static block in one of my early loaded servlets to list the environment parameters
static
{
    Properties envProps = System.getProperties();
    System.out.println("__________________________ BEGIN JAVA SETTINGS _______________________________");
    for (Enumeration e = envProps.propertyNames() ; e.hasMoreElements() ;)
    {
        String prop = (String)e.nextElement();
        System.out.println(prop + " : " + envProps.getProperty(prop));
    }
    System.out.println("_____________________________END JAVA SETTINGS _______________________________");
}



view this
Author: kumar varma (http://www.jguru.com/guru/viewbio.jsp?EID=1225116), Feb 4, 2005
along with classpath explanation can you please give us details about servlet-api.jar,tools.jar,etc.?

Run time error
Author: Baswaraj Ghule (http://www.jguru.com/guru/viewbio.jsp?EID=1299802), Jun 6, 2006
I had installed JDK1.4.0 and wrote a small java program for testing purpose, it compiled succesfully but while running it prompted following Exception I had also set the path in my environment setting accoring to my installed path. Exception in thread "main" java.lang.NoClassDefFoundError: Test Can any one help me out.

Re: Run time error
Author: sonal Sahu (http://www.jguru.com/guru/viewbio.jsp?EID=1312067), Sep 8, 2006
check the program name and class name in which main function is written they both should have me same. make the same i think it will dafinately work

Re[2]: Run time error. Getting an error while comping the program as same Exception in thread "main" java.lang.noclassDeffoundError: hello
Author: Utkarsh Mishra (http://www.jguru.com/guru/viewbio.jsp?EID=1314161), Sep 26, 2006
well hi sonal.....i tried whatever u said infact everyone is telling me that but its still not working. My class name is hello and i saved the file as hello.java and now when im compiling by typing javac hello.java then java.hello its showing me this run time error. i have read everyones msg plz....somebody help me in this.....i even not able to check any output of my programs i have written.

Re: Run time error
Author: jagan reddy (http://www.jguru.com/guru/viewbio.jsp?EID=1354726), Jan 19, 2008
Hi, You said that program is compiling succesfully but not running.Actually the problem in your program you have to write main() method in your program ,if it not have means u will get NoClass...error.If you thing your program is correct,then set classpath. <set CLASSPATH=%CLASSPATH%;.> then run.I hope ou will get. thankyou.
Is it the "servlets" directory or the "servlet" directory?
Location: http://www.jguru.com/faq/view.jsp?EID=142
Created: Sep 3, 1999 Modified: 1999-12-22 11:09:48.603
Author: Alex Chaffee (http://www.jguru.com/guru/viewbio.jsp?EID=3)

For Java Web Server:
  • on the file system, it's "servlets"
    c:\JavaWebServer1.1\servlets\DateServlet.class
  • in a URL path, it's "servlet"
    http://www.stinky.com/servlet/DateServlet
Other servlet engines have their own conventions. Usually on the file system it's "servlets" and in a URL it's "/servlet" (which is an alias or virtual path).
Comments and alternative answers
It depends on your mapping. Virtual path could be ...
Author: Roceller Alvarez (http://www.jguru.com/guru/viewbio.jsp?EID=41828), Apr 28, 2000
It depends on your mapping. Virtual path could be different from the real path.

Re: It depends on your mapping. Virtual path could be ...
Author: Tim Urberg (http://www.jguru.com/guru/viewbio.jsp?EID=510070), Oct 17, 2001
If you have Tomcat the directory will be:

TOMCAT_HOME/webapps/ROOT/WEB-INF/classes

If you're running JRun it will be:

C:\Program Files\Allaire\JRun\servers\default\default-app\WEB-INF\classes

I hope that helps
Why doesn't my servlet work inside a tag?
Location: http://www.jguru.com/faq/view.jsp?EID=143
Created: Sep 3, 1999 Modified: 2000-05-21 14:36:17.445
Author: Alex Chaffee (http://www.jguru.com/guru/viewbio.jsp?EID=3)

If you use your servlet inside an SSI, you must use res.getOutputStream() and not res.getWriter(). Check the server error logs for more details.
Comments and alternative answers
Does anyone here have any experience getting <s...
Author: Slim Whitman (http://www.jguru.com/guru/viewbio.jsp?EID=29020), Mar 27, 2000
Does anyone here have any experience getting <servlet> tags to work under Netscape Enterprise Server version 4? (I have service pack 3) I don't get any errors, but it's as if the server never parses the servlet tag. Parse HTML is turned on in the server, without using the exec tag, and for all html files. (instead of shtml)

I'm not quite sure about this, but I believe that only...
Author: Roceller Alvarez (http://www.jguru.com/guru/viewbio.jsp?EID=41828), Apr 28, 2000
I'm not quite sure about this, but I believe that only works on JRUN.

Jason Hunter's Servlet book was a defacto when it was...
Author: Matt Woody (http://www.jguru.com/guru/viewbio.jsp?EID=36408), May 12, 2000
Jason Hunter's Servlet book was a defacto when it was released because in 1998. He, under O'Reilly, released the best servlet book to date. Because he used the JavaWebServer in all his examples, he frequently used the <servlet> tag. The <servlet> tag is NOT a part of the Servlet API. It was simply a cool thing that JavaWebServer supported. I would suggest that you look towards JSP if you like that idea of combining HTML and Servlets. Otherwise you could create your own utilities to parse html documents, extract syntax within <servlet> and </servlet>, write to a different file, compile it, and then somehow tie the compiled code and the html together. Another option would be to parse an html document, inverse the outlying html and the syntax within <servlet>, and then compile that. I have quite often pondered doing this.
How do I support both GET and POST protocol from the same Servlet?
Location: http://www.jguru.com/faq/view.jsp?EID=144
Created: Sep 3, 1999 Modified: 2000-08-10 10:04:27.869
Author: Alex Chaffee (http://www.jguru.com/guru/viewbio.jsp?EID=3)

The easy way is, just support POST, then have your doGet method call your doPost method:
 
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
               doPost(req, res);   
}
Note that implementing the <mono>service()</mono> method is usually not what you want to do, since HttpServlet provides its own implementation of <mono>service()</mono> that turns around and calls <mono>doGet(), doPost(),</mono> etc.
Lee Crocker (LCrocker@INFORMANT.COM): "It's probably cleaner not to override <mono>service()</mono> when extending HttpServlet. The existing service method just calls <mono>doGet()</mono>, <mono>doPost()</mono>, etc. as appropriate, so you can certainly override it if you feel like it, but then you wind up not only treating GET and POST identically, but also all other HTTP commands, like HEAD, TRACE, and OPTIONS. If you want GET and POST to do the same thing, just have <mono>doGet()</mono> and <mono>doPost()</mono> call the same private method that does all the work."
See also:
  • What is the difference between the doGet and doPost methods?
  • How does one choose between overriding the doGet(), doPost(), and service() methods?
  • What is the difference between POST and GET methods? What about PUT, DELETE, TRACE, OPTIONS, and HEAD?

  • Comments and alternative answers
    I created a servlet, from which I usually extend, by...
    Author: Nicola Ken Barozzi (http://www.jguru.com/guru/viewbio.jsp?EID=39153), Apr 23, 2000
    I created a servlet, from which I usually extend, by making a new method that gets called by doGet() and doPost() and handles error handling.
    Apart from that it is a normal HttpServlet.
    CODE:
    /**
     * Copyright:    Copyright (c) 2000
     * @author Nicola Ken Barozzi
     */
     import java.io.*;
    import java.util.*;
     
    import javax.servlet.*;
    import javax.servlet.http.*;
     
    public abstract class WebAppServlet extends HttpServlet
    {
     
                                   public void init(ServletConfig config)
                                    throws ServletException
                                   {
                                                  super.init(config);
                                   }
     
     
                                   public final void doGet
                                   (
                                                  HttpServletRequest                            request,
                                                  HttpServletResponse          response
                                   ) throws ServletException, IOException
                                   {
                                                  doSafeGetOrPostWrapper(request, response);
                                   }
     
                                   public final void doPost
                                   (
                                                  HttpServletRequest                            request,
                                                  HttpServletResponse          response
                                  ) throws ServletException, IOException
                                   {
                                                  doSafeGetOrPostWrapper(request, response);
                                    }
     
                                   private void doSafeGetOrPostWrapper
                                   (
                                                  HttpServletRequest                            request,
                                                  HttpServletResponse          response
                                   ) throws ServletException, IOException
                                   {
                                                  HttpSession CurrentSession;
     
                                    try
                                     {
                                                  CurrentSession = request.getSession(/*false*/);
     
                                                  doSafeGetOrPost(request, response, CurrentSession);
                                    }
                                    catch(Throwable t)
                                    {
                                                  System.out.println("Error caught by doSafeGetOrPostWrapper:\n"+t+"\n");
                                                  t.printStackTrace();
     
                                                  System.out.println("Writing to log...");
       
                                                  CurrentSession = request.getSession();
     
                                                  this.getServletContext().log("Error", t);
                                                  
                                                  System.out.println("...ok.");
     
                                    try
                                     {
                                             ServletUtils.sendHttpError(t, request, response);
                                   }
                                   catch(Throwable tr)
                                    {
                                    }
     
                    }
      }
     
        public abstract void doSafeGetOrPost
                                   (
                                                  HttpServletRequest                            request,
                                                  HttpServletResponse          response,
                                                  HttpSession          CurrentSession,
                                   ) throws Throwable;
     
     
    }
     

No comments: