Results 1 to 8 of 8
  1. #1
    pnolans is offline Member
    Join Date
    Aug 2008
    Posts
    6
    Rep Power
    0

    Default [SOLVED] scope problem with a servlet

    Hi
    I'm a guy who hasn't written Java in a few years, and am trying to do the same thing in Java Servlets that I can currently do in Server-side Perl. Why?
    Just trying to improve my skills. widen my horizons. improve my resume.
    or something!

    So, I'm writing Java from scratch (and borrowed snippets) ...

    I also cross-posted this in the Java forum.

    --------------------------------------------------------------

    I get the following error when I compile the snippet below. If I comment out the line it complains about, it compiles cleanly.

    I'd like to be able to call a subroutine... I mean, invoke a method... from within a doPost or doGet. All of the
    examples I've seen put all the code within one method or they have methods that don't do anything.

    I'd like to avoid having all of the print statements in one method. It seems to defeat the purpose of OO programming.

    Thanks in advance for any help.

    Pat
    R:\SoftwareDev\MyJavaWork\Snippet1>javac -classpath "R:\SoftwareDev\Tomcat 4.1\c
    ommon\lib\servlet.jar" Snippet1.java
    Snippet1.java:31: cannot find symbol
    symbol : variable output
    location: class Snippet1
    output.println(" Here's a method with more stuff ");
    ^
    1 error

    Here's the code
    -----------------------------------------------------
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;

    public class Snippet1 extends HttpServlet {

    public void doGet (HttpServletRequest request,
    HttpServletResponse response) throws
    ServletException, IOException {
    String formText;

    response.setContentType("text/html");
    PrintWriter output=response.getWriter();

    formText=request.getParameter("message");

    if (formText==null || formText.length()==0) {
    formText="This is my first servlet in quite a while <br> ";
    }

    output.println(formText);


    }

    public void morestuff() {

    output.println(" Here's a method with more stuff "); //
    //If I comment out the line above . .. I geta clean compile

    }

    }

  2. #2
    pnolans is offline Member
    Join Date
    Aug 2008
    Posts
    6
    Rep Power
    0

    Default

    Hi

    The original post has been solved.. I did the following, adding declarations
    for Request and Response ... throwing clause... not it works. :

    public void morestuff(HttpServletRequest req,
    HttpServletResponse resp) throws
    ServletException, IOException {

    resp.setContentType("text/html");
    PrintWriter output2 = resp.getWriter();



    output2.println(" <h2> in morestuff method </h2>");

    }

  3. #3
    pnolans is offline Member
    Join Date
    Aug 2008
    Posts
    6
    Rep Power
    0

    Default

    I meant ... NOW it works. I said "not it works".

  4. #4
    udayadas's Avatar
    udayadas is offline Member
    Join Date
    Aug 2008
    Posts
    22
    Rep Power
    0

    Default

    The easier way could have been;

    Declare your PrintWriter output as a global variable.In that case output can be shared by both the methods.

  5. #5
    pnolans is offline Member
    Join Date
    Aug 2008
    Posts
    6
    Rep Power
    0

    Default global?

    Quote Originally Posted by udayadas View Post
    The easier way could have been;

    Declare your PrintWriter output as a global variable.In that case output can be shared by both the methods.
    Might you feel like giving an example of how to do that? I guess I was dissuaded from doing that since my "Java in a Nutshell" book said that Java does not have global variables.

    What would be the syntax for that?

    thanks,
    Pat

  6. #6
    udayadas's Avatar
    udayadas is offline Member
    Join Date
    Aug 2008
    Posts
    22
    Rep Power
    0

    Default

    mport java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;

    public class Snippet1 extends HttpServlet {

    PrintWriter output;

    public void doGet (HttpServletRequest request,
    HttpServletResponse response) throws
    ServletException, IOException {
    String formText;

    response.setContentType("text/html");
    output=response.getWriter();

    formText=request.getParameter("message");

    if (formText==null || formText.length()==0) {
    formText="This is my first servlet in quite a while <br> ";
    }

    output.println(formText);


    }

    public void morestuff() {

    output.println(" Here's a method with more stuff "); //
    //If I comment out the line above . .. I geta clean compile

    }

    }

  7. #7
    pnolans is offline Member
    Join Date
    Aug 2008
    Posts
    6
    Rep Power
    0

    Default thanks

    Thanks. I'll give that a try!

  8. #8
    pnolans is offline Member
    Join Date
    Aug 2008
    Posts
    6
    Rep Power
    0

    Default thanks udayadas

    I found another way to do it as well, but your way is even easier.


    Thanks very much,

    Pat

Similar Threads

  1. File Upload Servlet problem
    By jeniramires in forum Advanced Java
    Replies: 3
    Last Post: 08-18-2008, 07:34 PM
  2. jsf scope query
    By nc_newie in forum JavaServer Faces (JSF)
    Replies: 1
    Last Post: 08-06-2008, 02:34 PM
  3. servlet Filter problem
    By saint_jorjo in forum New To Java
    Replies: 1
    Last Post: 03-13-2008, 12:05 PM
  4. Problem in Servlet with JDBC
    By *New Programer* in forum Java Servlet
    Replies: 2
    Last Post: 12-20-2007, 09:15 AM
  5. Servlet Problem after install ^^Urgent^^
    By black_box in forum Java Servlet
    Replies: 0
    Last Post: 08-05-2007, 01:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •