Results 1 to 8 of 8
- 08-20-2008, 06:16 PM #1
Member
- Join Date
- Aug 2008
- Posts
- 6
- Rep Power
- 0
[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
}
}
- 08-20-2008, 10:59 PM #2
Member
- Join Date
- Aug 2008
- Posts
- 6
- Rep Power
- 0
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>");
}
- 08-21-2008, 12:16 AM #3
Member
- Join Date
- Aug 2008
- Posts
- 6
- Rep Power
- 0
I meant ... NOW it works. I said "not it works".
- 08-21-2008, 07:30 PM #4
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.
- 08-21-2008, 10:00 PM #5
Member
- Join Date
- Aug 2008
- Posts
- 6
- Rep Power
- 0
- 08-22-2008, 08:01 PM #6
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
}
}
- 08-22-2008, 08:39 PM #7
Member
- Join Date
- Aug 2008
- Posts
- 6
- Rep Power
- 0
thanks
Thanks. I'll give that a try!
- 08-23-2008, 02:50 PM #8
Member
- Join Date
- Aug 2008
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
File Upload Servlet problem
By jeniramires in forum Advanced JavaReplies: 3Last Post: 08-18-2008, 07:34 PM -
jsf scope query
By nc_newie in forum JavaServer Faces (JSF)Replies: 1Last Post: 08-06-2008, 02:34 PM -
servlet Filter problem
By saint_jorjo in forum New To JavaReplies: 1Last Post: 03-13-2008, 12:05 PM -
Problem in Servlet with JDBC
By *New Programer* in forum Java ServletReplies: 2Last Post: 12-20-2007, 09:15 AM -
Servlet Problem after install ^^Urgent^^
By black_box in forum Java ServletReplies: 0Last Post: 08-05-2007, 01:16 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks