• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlets

 
anjali ramachandran
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1625
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Make sure that the servlet-api.jar is in your classpath.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65122
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic