473,547 Members | 2,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to pass variable from public void doGet to public void doPost?

17 New Member
i'm newbie in java servlet, how to let public void doPost can access to public void doGet, stringLength variable?
below is my code:

Expand|Select|Wrap|Line Numbers
  1. import javax.servlet.*;
  2. import javax.servlet.http.*;
  3. import java.io.*;
  4.  
  5. public class LabWork3_Q1 extends HttpServlet
  6. {    
  7.     public void doGet(HttpServletRequest request, HttpServletResponse response) 
  8.         throws ServletException, IOException
  9.         {
  10.             response.setContentType("text/html");
  11.             PrintWriter out = response.getWriter();
  12.             int stringLength = 0; <= this the variable need access by public void doPost method
  13.             String word = request.getParameter("word");
  14.  
  15.             if(word != null)
  16.             {
  17.                 stringLength += word.length();
  18.             }
  19.  
  20.             out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
  21.                                         "Transitional//EN\">");
  22.             out.println("<HTML><HEAD><TITLE>Lab Work 3 - Q1</TITLE></HEAD><BODY>");
  23.             out.println("Enter a word here:<input type=\"text\" name=\"word\" size=\"10\">");
  24.             out.println("<form method=\"POST\" action=" +
  25.                                 "http://localhost:8080/LabWork3_Q1/LabWork3_Q1>");
  26.             out.println("<input type=\"submit\" value=\"Submit\">");
  27.             out.println("<input type=\"reset\" value=\"Reset\">");
  28.             out.println("</FORM></BODY></HTML>");
  29.             out.close();
  30.           }
  31.  
  32.           public void doPost (HttpServletRequest request, HttpServletResponse response)
  33.               throws ServletException, IOException
  34.               {
  35.                   response.setContentType("text/html");
  36.                   PrintWriter out = response.getWriter();
  37.  
  38.                   out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
  39.                                         "Transitional//EN\">");
  40.                 out.println("<HTML><HEAD><TITLE>Lab Work 3 - Q1</TITLE></HEAD><BODY>");
  41.                 out.println("Previous enter word lenght is:" + stringLength); <= This variable need access from public void doGet method
  42.                 out.println("<br>Enter a word here:<input type=\"text\" name=\"word\" size=\"10\"><br>");
  43.                 out.println("<form method=\"POST\" action=" +
  44.                                 "http://localhost:8080/LabWork3_Q1/LabWork3_Q1>");
  45.                 out.println("<input type=\"submit\" value=\"Submit\">");
  46.                 out.println("<input type=\"reset\" value=\"Reset\">");
  47.                 out.println("</FORM></BODY></HTML>");
  48.                 out.close();
  49.               }
  50. }
  51.  
Oct 1 '07 #1
5 6052
xirowei
17 New Member
I had make some modify but now encounter new problem in count length function.
After i enter a word and click on Submit button, Tomcat show line 34 error, may i know how to solve this problem?

Expand|Select|Wrap|Line Numbers
  1. import javax.servlet.*;
  2. import javax.servlet.http.*;
  3. import java.io.*;
  4.  
  5. public class LabWork3_Q1 extends HttpServlet
  6. {        
  7.     public void doGet(HttpServletRequest request, HttpServletResponse response) 
  8.         throws ServletException, IOException
  9.         {
  10.             response.setContentType("text/html");
  11.             PrintWriter out = response.getWriter();
  12.  
  13.             out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
  14.                                         "Transitional//EN\">");
  15.             out.println("<HTML><HEAD><TITLE>Lab Work 3 - Q1</TITLE></HEAD><BODY>");
  16.             out.println("Enter a word here:<input type=\"text\" name=\"word\" size=\"10\">");
  17.             out.println("<form method=\"POST\" action=" +
  18.                                 "http://localhost:8080/LabWork3_Q1/LabWork3_Q1>");
  19.                out.println("<input type=\"submit\" value=\"Submit\">");
  20.                out.println("<input type=\"reset\" value=\"Reset\">");
  21.                out.println("</FORM></BODY></HTML>");
  22.                out.close();    
  23.         }
  24.  
  25.           public void doPost (HttpServletRequest request, HttpServletResponse response)
  26.               throws ServletException, IOException
  27.               {
  28.                   response.setContentType("text/html");
  29.                   PrintWriter out = response.getWriter();
  30.  
  31.                   String word = request.getParameter("word");
  32.                   int stringLength = 0;
  33.  
  34.                   if(word.length() != 0)
  35.                 {
  36.                     stringLength += word.length();
  37.                     out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
  38.                                         "Transitional//EN\">");
  39.                     out.println("<HTML><HEAD><TITLE>Lab Work 3 - Q1</TITLE></HEAD><BODY>");
  40.                     out.println("Previous enter word lenght is:" + stringLength);
  41.                     out.println("<br>Enter a word here:<input type=\"text\" name=\"word\" size=\"10\"><br>");
  42.                     out.println("<form method=\"POST\" action=" +
  43.                                 "http://localhost:8080/LabWork3_Q1/LabWork3_Q1>");
  44.                     out.println("<input type=\"submit\" value=\"Submit\">");
  45.                     out.println("<input type=\"reset\" value=\"Reset\">");
  46.                     out.println("</FORM></BODY></HTML>");
  47.                     out.close();
  48.                 }
  49.  
  50.                 else
  51.                 {
  52.                     out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
  53.                                         "Transitional//EN\">");
  54.                     out.println("<HTML><HEAD><TITLE>Lab Work 3 - Q1</TITLE></HEAD><BODY>");
  55.                     out.println("Please enter a word, Blank not allow");
  56.                     out.println("<br>Enter a word here:<input type=\"text\" name=\"word\" size=\"10\"><br>");
  57.                     out.println("<form method=\"POST\" action=" +
  58.                                 "http://localhost:8080/LabWork3_Q1/LabWork3_Q1>");
  59.                     out.println("<input type=\"submit\" value=\"Submit\">");
  60.                     out.println("<input type=\"reset\" value=\"Reset\">");
  61.                     out.println("</FORM></BODY></HTML>");
  62.                     out.close();
  63.                 }
  64.  
  65.               }
  66. }
  67.  
Oct 1 '07 #2
xirowei
17 New Member
I had been working on this question and here is my latest version (still contain problem)
My problem is WHY the doPost method UNABLE to receive parameter of textfield name "word" from doGet method?

Expand|Select|Wrap|Line Numbers
  1. import javax.servlet.*;
  2. import javax.servlet.http.*;
  3. import java.io.*;
  4.  
  5. public class LabWork3_Q1 extends HttpServlet
  6. {        
  7.     public void doGet(HttpServletRequest request, HttpServletResponse response) 
  8.         throws ServletException, IOException
  9.         {
  10.             response.setContentType("text/html");
  11.             PrintWriter out = response.getWriter();
  12.  
  13.             out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
  14.                                         "Transitional//EN\">");
  15.             out.println("<HTML><HEAD><TITLE>Lab Work 3 - Q1</TITLE></HEAD><BODY>");
  16.             out.println("Enter a word here:<input type=\"text\" name=\"word\" size=\"10\">");
  17.             out.println("<form method=\"POST\" action=" +
  18.                                 "http://localhost:8080/LabWork3_Q1/LabWork3_Q1>");
  19.                out.println("<input type=\"submit\" value=\"Submit\">");
  20.                out.println("<input type=\"reset\" value=\"Clear\">");
  21.                out.println("</FORM></BODY></HTML>");
  22.                out.close();    
  23.         }
  24.  
  25.           public void doPost(HttpServletRequest request, HttpServletResponse response)
  26.               throws ServletException, IOException
  27.               {
  28.                   response.setContentType("text/html");
  29.                   PrintWriter out = response.getWriter();
  30.  
  31.                   String enter = request.getParameter("word");<= this variable unable to receive the parameter after click on Submit button. 
  32.  
  33.                   try 
  34.                   {
  35.                     if(enter.length() == 0)
  36.                     {
  37.                         out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
  38.                                         "Transitional//EN\">");
  39.                         out.println("<HTML><HEAD><TITLE>Lab Work 3 - Q1</TITLE></HEAD><BODY>");
  40.                         out.println("You must enter a word!<br>");
  41.                         out.println("Enter a word here:<input type=\"text\" name=\"word\" size=\"10\">");
  42.                             out.println("<form method=\"POST\" action=" +
  43.                                 "http://localhost:8080/LabWork3_Q1/LabWork3_Q1>");
  44.                            out.println("<input type=\"submit\" value=\"Submit\">");
  45.                            out.println("<input type=\"reset\" value=\"Clear\">");
  46.                            out.println("</FORM></BODY></HTML>");
  47.                            out.close();    
  48.                     }
  49.                     else
  50.                     {
  51.                         String previous = enter;
  52.                         out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
  53.                                         "Transitional//EN\">");
  54.                         out.println("<HTML><HEAD><TITLE>Lab Work 3 - Q1</TITLE></HEAD><BODY>");
  55.                         out.println("Previous enter word is:" + previous);
  56.                         out.println("<br>Enter a word here:<input type=\"text\" name=\"word\" size=\"10\"><br>");
  57.                         out.println("<form method=\"POST\" action=" +
  58.                                 "http://localhost:8080/LabWork3_Q1/LabWork3_Q1>");
  59.                         out.println("<input type=\"submit\" value=\"Submit\">");
  60.                         out.println("<input type=\"reset\" value=\"Reset\">");
  61.                         out.println("</FORM></BODY></HTML>");
  62.                         out.close();
  63.                     }
  64.                 }
  65.                 catch(Exception ex)
  66.                 {
  67.                     out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
  68.                                         "Transitional//EN\">");
  69.                     out.println("<HTML><HEAD><TITLE>Lab Work 3 - Q1</TITLE></HEAD><BODY>");
  70.                     out.println("Error:" + ex.getMessage());
  71.                     out.println("</FORM></BODY></HTML>");
  72.                     out.close();
  73.                 }            
  74.               }
  75. }
  76.  
Oct 1 '07 #3
r035198x
13,262 MVP
All this does not make any sense.
You should write your code in such a way that either the doGet or the doPost method gets called (depending on the JSP's submit method) not both.
Oct 1 '07 #4
JosAH
11,448 Recognized Expert MVP
All this does not make any sense.
You should write your code in such a way that either the doGet or the doPost method gets called (depending on the JSP's submit method) not both.
And even then you could do this:

Expand|Select|Wrap|Line Numbers
  1. public void doGet( ... ) { ... }
  2. public void doPost( ... ) { doGet( ... ); }
  3.  
or this:

Expand|Select|Wrap|Line Numbers
  1. public void doPost( ... ) { ... }
  2. public void doGet( ... ) { doPost( ... ); }
  3.  
kind regards,

Jos
Oct 1 '07 #5
xirowei
17 New Member
I finally figure out the correct way to do this question. Below is the working code.

Expand|Select|Wrap|Line Numbers
  1. import javax.servlet.*;
  2. import javax.servlet.http.*;
  3. import java.io.*;
  4.  
  5. public class LabWork3_Q1 extends HttpServlet
  6. {        
  7.     public void doGet(HttpServletRequest request, HttpServletResponse response) 
  8.         throws ServletException, IOException
  9.         {
  10.             response.setContentType("text/html");
  11.             PrintWriter out = response.getWriter();
  12.  
  13.             out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
  14.                                         "Transitional//EN\">");
  15.             out.println("<HTML><HEAD><TITLE>Lab Work 3 - Q1</TITLE></HEAD><BODY>");
  16.             out.println("<form method=\"POST\" action=" +
  17.                                 "http://localhost:8080/LabWork3_Q1/LabWork3_Q1>");
  18.             out.println("Enter a word here:<input type=\"text\" name=\"word\" size=\"10\">");
  19.                out.println("<input type=\"submit\" value=\"Submit\">");
  20.                out.println("<input type=\"reset\" value=\"Clear\">");
  21.                out.println("</FORM></BODY></HTML>");
  22.                out.close();    
  23.         }
  24.  
  25.           public void doPost(HttpServletRequest request, HttpServletResponse response)
  26.               throws ServletException, IOException
  27.               {
  28.                   response.setContentType("text/html");
  29.                   PrintWriter out = response.getWriter();
  30.  
  31.                   String enter = request.getParameter("word");
  32.                   int enterLength = 0;
  33.  
  34.                   try 
  35.                   {
  36.                     if(enter.length() == 0)
  37.                     {
  38.                         out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
  39.                                         "Transitional//EN\">");
  40.                         out.println("<HTML><HEAD><TITLE>Lab Work 3 - Q1</TITLE></HEAD><BODY>");
  41.                         out.println("You must enter a word!<br>");
  42.                             out.println("<form method=\"POST\" action=" +
  43.                                 "http://localhost:8080/LabWork3_Q1/LabWork3_Q1>");
  44.                         out.println("Enter a word here:<input type=\"text\" name=\"word\" size=\"10\">");
  45.                            out.println("<input type=\"submit\" value=\"Submit\">");
  46.                            out.println("<input type=\"reset\" value=\"Clear\">");
  47.                            out.println("</FORM></BODY></HTML>");
  48.                            out.close();    
  49.                     }
  50.                     else
  51.                     {
  52.                         enterLength += enter.length();
  53.                         out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
  54.                                         "Transitional//EN\">");
  55.                         out.println("<HTML><HEAD><TITLE>Lab Work 3 - Q1</TITLE></HEAD><BODY>");
  56.                         out.println("Previous enter word length is:" + enterLength);
  57.                         out.println("<form method=\"POST\" action=" +
  58.                                 "http://localhost:8080/LabWork3_Q1/LabWork3_Q1>");
  59.                         out.println("<br>Enter a word here:<input type=\"text\" name=\"word\" size=\"10\"><br>");
  60.                         out.println("<input type=\"submit\" value=\"Submit\">");
  61.                         out.println("<input type=\"reset\" value=\"Reset\">");
  62.                         out.println("</FORM></BODY></HTML>");
  63.                         out.close();
  64.                     }
  65.                 }
  66.                 catch(Exception ex)
  67.                 {
  68.                     out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
  69.                                         "Transitional//EN\">");
  70.                     out.println("<HTML><HEAD><TITLE>Lab Work 3 - Q1</TITLE></HEAD><BODY>");
  71.                     out.println("Error:" + ex.getMessage());
  72.                     out.println("</FORM></BODY></HTML>");
  73.                     out.close();
  74.                 }            
  75.               }
  76. }
  77.  
Oct 1 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

8
3354
by: Brian F | last post by:
Exactly what the subject says. I have an ASP page that I want my C# windows application to open in Internet Explorer, and if possible have it send along a list of values from a list box. Thank you.
4
3395
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then 42. They say that 42 is printed the second time since the value was wrapped in a class and therefore became passed by reference. (sorry for any...
1
1402
by: Don | last post by:
Hi, I jave a form that creates checkboxes dependent on what is selected from a drop-dwon list; this is done using PHP. I have a button that allows a user to "SELECT ALL" which will calls a JavaScript function to check or uncheck all available checkboxes. The thing is, the number and names of the checkboxes are built at run time and named...
5
7806
by: David++ | last post by:
Hi folks, I would be interested to hear peoples views on whether or not 'pass by reference' is allowed when using a Web Service method. The thing that troubles me about pass-by-reference into a WebService is that essentially we are passing an address of an object which resides on the 'local machine' i.e. a local machine object address....
14
1879
by: needin4mation | last post by:
I have this in my web service: public XmlDocument GetDataFromDB(string name) { .... do some stuff with name variable..on the database... return the XmlDocument to my datagrid; } Now, in my client default.aspx I have a button that calls this
1
996
by: vaskarbasak | last post by:
Hi all, Why do we override the doGet and/or doPost methods instead of the service method? Thanks! vaskar
12
3000
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope. The global variables and global functions are hidden to prevent from accessing by the programmers. All global functions share global variables....
14
3589
by: =?Utf-8?B?Umljaw==?= | last post by:
I have seen examples of passing data from FormB to FormA (Parent To Child) but I need an example of passind data from FormA to FormB. My main form is FormA; I will enter some data in textboxes and click a button to bring up FormB which needs to display values entered in FormA. Thanks in advance.
12
11031
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms. Here is a newbie mistake that I found myself doing (as a newbie), and that even a master programmer, the guru of this forum, Jon Skeet, missed!...
0
7507
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7698
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7794
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5361
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3472
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.