i'm newbie in java servlet, how to let public void doPost can access to public void doGet, stringLength variable?
below is my code: -
import javax.servlet.*;
-
import javax.servlet.http.*;
-
import java.io.*;
-
-
public class LabWork3_Q1 extends HttpServlet
-
{
-
public void doGet(HttpServletRequest request, HttpServletResponse response)
-
throws ServletException, IOException
-
{
-
response.setContentType("text/html");
-
PrintWriter out = response.getWriter();
-
int stringLength = 0; <= this the variable need access by public void doPost method
-
String word = request.getParameter("word");
-
-
if(word != null)
-
{
-
stringLength += word.length();
-
}
-
-
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
-
"Transitional//EN\">");
-
out.println("<HTML><HEAD><TITLE>Lab Work 3 - Q1</TITLE></HEAD><BODY>");
-
out.println("Enter a word here:<input type=\"text\" name=\"word\" size=\"10\">");
-
out.println("<form method=\"POST\" action=" +
-
"http://localhost:8080/LabWork3_Q1/LabWork3_Q1>");
-
out.println("<input type=\"submit\" value=\"Submit\">");
-
out.println("<input type=\"reset\" value=\"Reset\">");
-
out.println("</FORM></BODY></HTML>");
-
out.close();
-
}
-
-
public void doPost (HttpServletRequest request, HttpServletResponse response)
-
throws ServletException, IOException
-
{
-
response.setContentType("text/html");
-
PrintWriter out = response.getWriter();
-
-
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
-
"Transitional//EN\">");
-
out.println("<HTML><HEAD><TITLE>Lab Work 3 - Q1</TITLE></HEAD><BODY>");
-
out.println("Previous enter word lenght is:" + stringLength); <= This variable need access from public void doGet method
-
out.println("<br>Enter a word here:<input type=\"text\" name=\"word\" size=\"10\"><br>");
-
out.println("<form method=\"POST\" action=" +
-
"http://localhost:8080/LabWork3_Q1/LabWork3_Q1>");
-
out.println("<input type=\"submit\" value=\"Submit\">");
-
out.println("<input type=\"reset\" value=\"Reset\">");
-
out.println("</FORM></BODY></HTML>");
-
out.close();
-
}
-
}
-
5 6052
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? -
import javax.servlet.*;
-
import javax.servlet.http.*;
-
import java.io.*;
-
-
public class LabWork3_Q1 extends HttpServlet
-
{
-
public void doGet(HttpServletRequest request, HttpServletResponse response)
-
throws ServletException, IOException
-
{
-
response.setContentType("text/html");
-
PrintWriter out = response.getWriter();
-
-
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
-
"Transitional//EN\">");
-
out.println("<HTML><HEAD><TITLE>Lab Work 3 - Q1</TITLE></HEAD><BODY>");
-
out.println("Enter a word here:<input type=\"text\" name=\"word\" size=\"10\">");
-
out.println("<form method=\"POST\" action=" +
-
"http://localhost:8080/LabWork3_Q1/LabWork3_Q1>");
-
out.println("<input type=\"submit\" value=\"Submit\">");
-
out.println("<input type=\"reset\" value=\"Reset\">");
-
out.println("</FORM></BODY></HTML>");
-
out.close();
-
}
-
-
public void doPost (HttpServletRequest request, HttpServletResponse response)
-
throws ServletException, IOException
-
{
-
response.setContentType("text/html");
-
PrintWriter out = response.getWriter();
-
-
String word = request.getParameter("word");
-
int stringLength = 0;
-
-
if(word.length() != 0)
-
{
-
stringLength += word.length();
-
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
-
"Transitional//EN\">");
-
out.println("<HTML><HEAD><TITLE>Lab Work 3 - Q1</TITLE></HEAD><BODY>");
-
out.println("Previous enter word lenght is:" + stringLength);
-
out.println("<br>Enter a word here:<input type=\"text\" name=\"word\" size=\"10\"><br>");
-
out.println("<form method=\"POST\" action=" +
-
"http://localhost:8080/LabWork3_Q1/LabWork3_Q1>");
-
out.println("<input type=\"submit\" value=\"Submit\">");
-
out.println("<input type=\"reset\" value=\"Reset\">");
-
out.println("</FORM></BODY></HTML>");
-
out.close();
-
}
-
-
else
-
{
-
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
-
"Transitional//EN\">");
-
out.println("<HTML><HEAD><TITLE>Lab Work 3 - Q1</TITLE></HEAD><BODY>");
-
out.println("Please enter a word, Blank not allow");
-
out.println("<br>Enter a word here:<input type=\"text\" name=\"word\" size=\"10\"><br>");
-
out.println("<form method=\"POST\" action=" +
-
"http://localhost:8080/LabWork3_Q1/LabWork3_Q1>");
-
out.println("<input type=\"submit\" value=\"Submit\">");
-
out.println("<input type=\"reset\" value=\"Reset\">");
-
out.println("</FORM></BODY></HTML>");
-
out.close();
-
}
-
-
}
-
}
-
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? -
import javax.servlet.*;
-
import javax.servlet.http.*;
-
import java.io.*;
-
-
public class LabWork3_Q1 extends HttpServlet
-
{
-
public void doGet(HttpServletRequest request, HttpServletResponse response)
-
throws ServletException, IOException
-
{
-
response.setContentType("text/html");
-
PrintWriter out = response.getWriter();
-
-
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
-
"Transitional//EN\">");
-
out.println("<HTML><HEAD><TITLE>Lab Work 3 - Q1</TITLE></HEAD><BODY>");
-
out.println("Enter a word here:<input type=\"text\" name=\"word\" size=\"10\">");
-
out.println("<form method=\"POST\" action=" +
-
"http://localhost:8080/LabWork3_Q1/LabWork3_Q1>");
-
out.println("<input type=\"submit\" value=\"Submit\">");
-
out.println("<input type=\"reset\" value=\"Clear\">");
-
out.println("</FORM></BODY></HTML>");
-
out.close();
-
}
-
-
public void doPost(HttpServletRequest request, HttpServletResponse response)
-
throws ServletException, IOException
-
{
-
response.setContentType("text/html");
-
PrintWriter out = response.getWriter();
-
-
String enter = request.getParameter("word");<= this variable unable to receive the parameter after click on Submit button.
-
-
try
-
{
-
if(enter.length() == 0)
-
{
-
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
-
"Transitional//EN\">");
-
out.println("<HTML><HEAD><TITLE>Lab Work 3 - Q1</TITLE></HEAD><BODY>");
-
out.println("You must enter a word!<br>");
-
out.println("Enter a word here:<input type=\"text\" name=\"word\" size=\"10\">");
-
out.println("<form method=\"POST\" action=" +
-
"http://localhost:8080/LabWork3_Q1/LabWork3_Q1>");
-
out.println("<input type=\"submit\" value=\"Submit\">");
-
out.println("<input type=\"reset\" value=\"Clear\">");
-
out.println("</FORM></BODY></HTML>");
-
out.close();
-
}
-
else
-
{
-
String previous = enter;
-
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
-
"Transitional//EN\">");
-
out.println("<HTML><HEAD><TITLE>Lab Work 3 - Q1</TITLE></HEAD><BODY>");
-
out.println("Previous enter word is:" + previous);
-
out.println("<br>Enter a word here:<input type=\"text\" name=\"word\" size=\"10\"><br>");
-
out.println("<form method=\"POST\" action=" +
-
"http://localhost:8080/LabWork3_Q1/LabWork3_Q1>");
-
out.println("<input type=\"submit\" value=\"Submit\">");
-
out.println("<input type=\"reset\" value=\"Reset\">");
-
out.println("</FORM></BODY></HTML>");
-
out.close();
-
}
-
}
-
catch(Exception ex)
-
{
-
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
-
"Transitional//EN\">");
-
out.println("<HTML><HEAD><TITLE>Lab Work 3 - Q1</TITLE></HEAD><BODY>");
-
out.println("Error:" + ex.getMessage());
-
out.println("</FORM></BODY></HTML>");
-
out.close();
-
}
-
}
-
}
-
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. 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: -
public void doGet( ... ) { ... }
-
public void doPost( ... ) { doGet( ... ); }
-
or this: -
public void doPost( ... ) { ... }
-
public void doGet( ... ) { doPost( ... ); }
-
kind regards,
Jos
I finally figure out the correct way to do this question. Below is the working code. -
import javax.servlet.*;
-
import javax.servlet.http.*;
-
import java.io.*;
-
-
public class LabWork3_Q1 extends HttpServlet
-
{
-
public void doGet(HttpServletRequest request, HttpServletResponse response)
-
throws ServletException, IOException
-
{
-
response.setContentType("text/html");
-
PrintWriter out = response.getWriter();
-
-
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
-
"Transitional//EN\">");
-
out.println("<HTML><HEAD><TITLE>Lab Work 3 - Q1</TITLE></HEAD><BODY>");
-
out.println("<form method=\"POST\" action=" +
-
"http://localhost:8080/LabWork3_Q1/LabWork3_Q1>");
-
out.println("Enter a word here:<input type=\"text\" name=\"word\" size=\"10\">");
-
out.println("<input type=\"submit\" value=\"Submit\">");
-
out.println("<input type=\"reset\" value=\"Clear\">");
-
out.println("</FORM></BODY></HTML>");
-
out.close();
-
}
-
-
public void doPost(HttpServletRequest request, HttpServletResponse response)
-
throws ServletException, IOException
-
{
-
response.setContentType("text/html");
-
PrintWriter out = response.getWriter();
-
-
String enter = request.getParameter("word");
-
int enterLength = 0;
-
-
try
-
{
-
if(enter.length() == 0)
-
{
-
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
-
"Transitional//EN\">");
-
out.println("<HTML><HEAD><TITLE>Lab Work 3 - Q1</TITLE></HEAD><BODY>");
-
out.println("You must enter a word!<br>");
-
out.println("<form method=\"POST\" action=" +
-
"http://localhost:8080/LabWork3_Q1/LabWork3_Q1>");
-
out.println("Enter a word here:<input type=\"text\" name=\"word\" size=\"10\">");
-
out.println("<input type=\"submit\" value=\"Submit\">");
-
out.println("<input type=\"reset\" value=\"Clear\">");
-
out.println("</FORM></BODY></HTML>");
-
out.close();
-
}
-
else
-
{
-
enterLength += enter.length();
-
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
-
"Transitional//EN\">");
-
out.println("<HTML><HEAD><TITLE>Lab Work 3 - Q1</TITLE></HEAD><BODY>");
-
out.println("Previous enter word length is:" + enterLength);
-
out.println("<form method=\"POST\" action=" +
-
"http://localhost:8080/LabWork3_Q1/LabWork3_Q1>");
-
out.println("<br>Enter a word here:<input type=\"text\" name=\"word\" size=\"10\"><br>");
-
out.println("<input type=\"submit\" value=\"Submit\">");
-
out.println("<input type=\"reset\" value=\"Reset\">");
-
out.println("</FORM></BODY></HTML>");
-
out.close();
-
}
-
}
-
catch(Exception ex)
-
{
-
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
-
"Transitional//EN\">");
-
out.println("<HTML><HEAD><TITLE>Lab Work 3 - Q1</TITLE></HEAD><BODY>");
-
out.println("Error:" + ex.getMessage());
-
out.println("</FORM></BODY></HTML>");
-
out.close();
-
}
-
}
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
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.
|
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...
|
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...
|
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....
|
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
| |
by: vaskarbasak |
last post by:
Hi all,
Why do we override the doGet and/or doPost methods instead of the service method?
Thanks!
vaskar
|
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....
|
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.
|
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!...
|
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...
|
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. ...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |