Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: ServletExample code problem - beginner

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ServletExample code problem - beginner

    Hello Friends

    I tried compiling this code

    Servlet Example, Java Servlet Example, Servlet source code (WelcomeServlet.java file)

    in my linux system and I am getting the following error and I can't see the problem for I copied and paste. I am a newbie and learning servlet



    james@ubuntu:/usr/local/tomcat/MyProjects/Servlet-Example$ javac WelcomeServlet.java
    WelcomeServlet.java:41: ')' expected
    out.println("<a href="/Servlet-Example/form.html">"+"Click here to go back to input page "+"</a>");
    ^
    WelcomeServlet.java:41: not a statement
    out.println("<a href="/Servlet-Example/form.html">"+"Click here to go back to input page "+"</a>");
    ^
    WelcomeServlet.java:41: ';' expected
    out.println("<a href="/Servlet-Example/form.html">"+"Click here to go back to input page "+"</a>");
    ^
    3 errors


    I suspect I am using Linux instead of windows.

    Thanks
    Sawubona


  2. #2
    Junior Member
    Join Date
    Jun 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ServletExample code problem - beginner

    Hi Sawubona,
    You need to escape the double quotes (") in your code. Replace the following line

    out.println("<a href="/servletexample/pages/form.html">"+"Click here to go back to input page
    "+"</a>");

    with

    out.println("<a href=\"/servletexample/pages/form.html\">"+"Click here to go back to input page "+"</a>");

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ServletExample code problem - beginner

    Thanks java_13 . I might have made progress and now it is complaining about this


    WelcomeServlet.java:6: package javax.servlet does not exist
    import javax.servlet.ServletConfig;
    ^
    WelcomeServlet.java:7: package javax.servlet does not exist
    import javax.servlet.ServletException;
    ^
    WelcomeServlet.java:8: package javax.servlet.http does not exist
    import javax.servlet.http.HttpServlet;
    ^
    WelcomeServlet.java:9: package javax.servlet.http does not exist
    import javax.servlet.http.HttpServletRequest;
    ^
    WelcomeServlet.java:10: package javax.servlet.http does not exist
    import javax.servlet.http.HttpServletResponse;
    ^
    WelcomeServlet.java:12: cannot find symbol
    symbol: class HttpServlet
    public class WelcomeServlet extends HttpServlet {
    ^
    WelcomeServlet.java:15: cannot find symbol
    symbol : class ServletConfig
    location: class jsptube.tutorials.servletexample.WelcomeServlet
    public void init(ServletConfig config) throws ServletException {
    ^
    WelcomeServlet.java:15: cannot find symbol
    symbol : class ServletException
    location: class jsptube.tutorials.servletexample.WelcomeServlet
    public void init(ServletConfig config) throws ServletException {
    ^
    WelcomeServlet.java:20: cannot find symbol
    symbol : class HttpServletRequest
    location: class jsptube.tutorials.servletexample.WelcomeServlet
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    ^
    WelcomeServlet.java:20: cannot find symbol
    symbol : class HttpServletResponse
    location: class jsptube.tutorials.servletexample.WelcomeServlet
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    ^
    WelcomeServlet.java:20: cannot find symbol
    symbol : class ServletException
    location: class jsptube.tutorials.servletexample.WelcomeServlet
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    ^
    WelcomeServlet.java:16: cannot find symbol
    symbol : variable super
    location: class jsptube.tutorials.servletexample.WelcomeServlet
    super.init(config);
    ^
    WelcomeServlet.java:14: method does not override or implement a method from a supertype
    @Override
    ^
    13 errors


    I suspect this:

    package jsptube.tutorials.servletexample;

    was I supposed to import this package?

    Thanks

  4. #4
    Junior Member
    Join Date
    Jun 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ServletExample code problem - beginner

    You code is correct. Do you have "servlet-api.jar" in your java class-path? This is the jar file that have all the packages and classes that compiler is complaining about.
    -> If you are using an IDE, like eclipse, you can configure build path and add this jar.
    -> If you are compiling it from command prompt, you can use -classpath option and put jars under class-path. Here is the command :

    javac -classpath "C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar" WelcomeServlet.java

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ServletExample code problem - beginner

    Hi Java_13

    I tried compiling using the classpath and I am still getting the same errors. I think it can t find the packages . I used the command prompt in Linux (Ubuntu) and verified the servlet-api was in the lib directory - javac -classpath "/usr/local/Tomcat/lib/servlet-api.jar" WelcomeServlet.java

  6. #6
    Junior Member
    Join Date
    Jun 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ServletExample code problem - beginner

    See, if you can provide me your sample code. I will try compiling it and see if anything is wrong there. Because, i have used that command several times to compile Servlet code. don't know whats wrong with it.

Similar Threads

  1. Beginner trying to write Java code, has issue w/ printing result and 2 decimals
    By flpanthers1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 5th, 2011, 11:11 AM
  2. Beginner method call problem
    By Lasda in forum Java Theory & Questions
    Replies: 2
    Last Post: May 10th, 2011, 03:31 PM
  3. problem with code
    By Imeri0n in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 25th, 2011, 08:06 AM
  4. Beginner Problem
    By nve5009 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 19th, 2010, 11:29 AM
  5. Beginner - Help with my code.
    By eddross in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 12th, 2010, 09:30 AM