aspose file tools*
The moose likes Servlets and the fly likes servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlets" Watch "servlets" New topic
Author

servlets

anjali ramachandran
Greenhorn

Joined: Aug 12, 2009
Posts: 1
I am trying to run a servlet in command prompt.But I am unable to compile it into a
class file.I have set the classpath but still I am getting package javax.servlet.* not found error.
My Program is as folows:

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;


public class WelcomeServlet extends HttpServlet {

@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*
* Get the value of form parameter
*/
String name = request.getParameter("name");
String welcomeMessage = "Welcome "+name;
/*
* Set the content type(MIME Type) of the response.
*/
response.setContentType("text/html");

PrintWriter out = response.getWriter();
/*
* Write the HTML to the response
*/
out.println("<html>");
out.println("<head>");
out.println("<title> A very simple servlet example</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>"+welcomeMessage+"</h1>");
//out.println("<a href="/servletexample/pages/form.html">"+"Click here to go back to input page "+"</a>");
out.println("</body>");
out.println("</html>");
out.close();

}


public void destroy() {

}
}
classpath I have set are
TOMCAT C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar
JAVA_HOME C:\Program Files\Java\jdk1.6.0\bin

Can you please find me some alternative

Thanking you in advance
Gerardo Tasistro
Ranch Hand

Joined: Feb 08, 2005
Posts: 362
An alternative I'd suggest is running it in a server like Tomcat. Of all the things that come to mind as possible limitations to running a servlet from a command line, lacking a main is top on my list.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Anjali,

First of all you can't execute a servlet from command prompt. It needs container for the execution. For e.g. tomcat, jboss, webspher, weblogic etc. Secondly the problem what you are facing is a classpath issue. Your classpath is not properly pointing to servlet-api.jar. Third problem noticeable in your code is HttpServlet belongs to javax.servlet.http package, however you haven't imported that.


Swastik
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

anjali please Use A Meaningful Subject Line for your topics. Writing only Servlet in the title doesn't let us understand what the question is about.

Also when you post a source code, please Use Code Tags...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
When you say:
I am trying to run a servlet in command prompt


do you mean you are trying to compile a servlet? Thats what the package not found sounds like.

The package javax.servlet is NOT in the standard library, you need the packages distributed with a servlet container such as Tomcat in your classpath for the compiler to see them. Defining a TOMCAT variable is not enough.

Bill
Shruthi Sam
Ranch Hand

Joined: Jul 09, 2008
Posts: 73
Hi,
Make sure that the servlet-api.jar is in your classpath.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

Please be sure to take the time to compose descriptive subjects for your posts; read this for more information.

Using a title of "servlets" in a forum completely dedicated to questions on Servlets isn't very helpful. What if everyone title theri servlet questions "servlets"?

Please go back and change your post to add a more meaningful subject by clicking the


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: servlets