Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Servlet basic program

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
class Ch1Servlet extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)
{
PrintWriter out=response.getWriter();
java.util.Date today=new java.util.Date();
out.println("<html>"+"<body>"+"h1 align=center>HF\'s Chappter1 Servlet</h1>"
+"
"+today+"</body>"+"</html>");

}
}
While compiling this program i am getting these errors

Ch1Servlet.java:1: package javax.servlet does not exist
import javax.servlet.*;
^
Ch1Servlet.java:2: package javax.servlet.http does not exist
import javax.servlet.http.*;
^
Ch1Servlet.java:4: cannot find symbol
symbol: class HttpServlet
class Ch1Servlet extends HttpServlet
^
Ch1Servlet.java:6: cannot find symbol
symbol : class HttpServletRequest
location: class Ch1Servlet
public void doGet(HttpServletRequest req,HttpServletResponse res)
^
Ch1Servlet.java:6: cannot find symbol
symbol : class HttpServletResponse
location: class Ch1Servlet
public void doGet(HttpServletRequest req,HttpServletResponse res)
^
Ch1Servlet.java:8: cannot find symbol
symbol : variable response
location: class Ch1Servlet
PrintWriter out=response.getWriter();
^
6 errors

I already added the servlet-api.jar to the class path.
How can i overcome these errors? Please help me to rectify these errors.


Thank You

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aresh
try making your servlet class public and then compile it ... this is going to be run with the help of web container.

Sreedhar
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

servlet-api jar is not recognised. and probably your classpath is not correct . What servlet container are you using ? how do you setup classpath ? For jars , complete path with jar to be used in classpath.

In windows : type in cmd like CLASSPATH
In Unix : type env to find out the vlaue set for CLASSPATH.


You can also compile servlet using javac -classpath test.java
 
aresh babu
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Balu sadashivam,
i am using Tomcat6.0 as a servlet container and i wrote the classpath is as follows

C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar;

But i am unable to compile the program.

Whats wrong in this classpath?

What should i do to compile this program
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you try pinting out CLASSPATH in cmd and check ?
How did you setup CLASSPATH and compile ? Are you usng IDE ? or ant ?

set CLASSPATH=.;C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar;

 
aresh babu
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Balu Sadashivam,

I compiled the program by changing the classpath as you have given.

I am preparing for SCWCD for that reason i am not using any IDE.

My i know about Ant and how it is helpful?

Thank You for your guidance,
Aresh Babu
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ant is a build tool which is handy when compiling projects.. To get heads up check out the inhouse material Ant and Ant manual

cheers
Balu
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic