Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-20-2008, 06:16 PM
Member
 
Join Date: Aug 2008
Posts: 6
Rep Power: 0
pnolans is on a distinguished road
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

}

}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 08-20-2008, 10:59 PM
Member
 
Join Date: Aug 2008
Posts: 6
Rep Power: 0
pnolans is on a distinguished road
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>");

}
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-21-2008, 12:16 AM
Member
 
Join Date: Aug 2008
Posts: 6
Rep Power: 0
pnolans is on a distinguished road
Default
I meant ... NOW it works. I said "not it works".
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-21-2008, 07:30 PM
udayadas's Avatar
Member
 
Join Date: Aug 2008
Posts: 22
Rep Power: 0
udayadas is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-21-2008, 10:00 PM
Member
 
Join Date: Aug 2008
Posts: 6
Rep Power: 0
pnolans is on a distinguished road
Default global?
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 08-22-2008, 08:01 PM
udayadas's Avatar
Member
 
Join Date: Aug 2008
Posts: 22
Rep Power: 0
udayadas is on a distinguished road
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

}

}
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 08-22-2008, 08:39 PM
Member
 
Join Date: Aug 2008
Posts: 6
Rep Power: 0
pnolans is on a distinguished road
Default thanks
Thanks. I'll give that a try!
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 08-23-2008, 02:50 PM
Member
 
Join Date: Aug 2008
Posts: 6
Rep Power: 0
pnolans is on a distinguished road
Default thanks udayadas
I found another way to do it as well, but your way is even easier.


Thanks very much,

Pat
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT +2. The time now is 12:34 PM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org