Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-20-2008, 08:16 PM
Member
 
Join Date: Aug 2008
Posts: 6
pnolans is on a distinguished road
[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
Sponsored Links
  #2 (permalink)  
Old 08-21-2008, 12:59 AM
Member
 
Join Date: Aug 2008
Posts: 6
pnolans is on a distinguished road
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, 02:16 AM
Member
 
Join Date: Aug 2008
Posts: 6
pnolans is on a distinguished road
I meant ... NOW it works. I said "not it works".
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-21-2008, 09:30 PM
udayadas's Avatar
Member
 
Join Date: Aug 2008
Posts: 22
udayadas is on a distinguished road
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-22-2008, 12:00 AM
Member
 
Join Date: Aug 2008
Posts: 6
pnolans is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 08-22-2008, 10:01 PM
udayadas's Avatar
Member
 
Join Date: Aug 2008
Posts: 22
udayadas is on a distinguished road
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, 10:39 PM
Member
 
Join Date: Aug 2008
Posts: 6
pnolans is on a distinguished road
thanks
Thanks. I'll give that a try!
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 08-23-2008, 04:50 PM
Member
 
Join Date: Aug 2008
Posts: 6
pnolans is on a distinguished road
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
Sponsored Links
Reply


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

vB 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 09:34 PM
jsf scope query nc_newie JavaServer Faces 1 08-06-2008 04:34 PM
servlet Filter problem saint_jorjo New To Java 1 03-13-2008 02:05 PM
Problem in Servlet with JDBC *New Programer* Java Servlet 2 12-20-2007 11:15 AM
Servlet Problem after install ^^Urgent^^ black_box Java Servlet 0 08-05-2007 03:16 AM


All times are GMT +3. The time now is 07:08 AM.


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