i'm newbie in java servlet, how to let public void doPost can access to public void doGet, stringLength variable?
below is my code:
below is my code:
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();
[B]int stringLength = 0; <= this the variable need access by public void doPost method[/B]
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>");
[B]out.println("Previous enter word lenght is:" + stringLength); <= This variable need access from public void doGet method[/B]
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();
}
}
Comment