R
Ryan Gaffuri
When I go to a url that is a servlet:
The following happens(please let me know if I am right)
1. init()
2. doGet()
-- if i have my webpage in this function, that is executed.
3. how do i get dopost to fire? I tried putting
<FORM METHOD=POST> into my code, but my dopost code is not fired?
I have example code that does and fires the dopost method which just
writes some of the values entered in the form to the screen. Without
posting tons of code, I am having trouble getting my 'doPost' to fire
from my form which is executed by 'doGet'.
when i click submit, all that happens is that the current screen is
refreshed.
my sample code is as follows. it does not have any 'action' in it.
what in this code is actually getting the 'dopost' method to execute?
this code is out of ;java for the web with servlets' book.
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class HttpRequestDemoServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Obtaining Multi-Value Parameters</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("<BR>");
out.println("<BR>Select your favorite music:");
out.println("<BR><FORM METHOD=POST>");
out.println("<BR><INPUT TYPE=CHECKBOX " +
"NAME=favoriteMusic VALUE=Rock>Rock");
out.println("<BR><INPUT TYPE=CHECKBOX " +
"NAME=favoriteMusic VALUE=Jazz>Jazz");
out.println("<BR><INPUT TYPE=CHECKBOX " +
"NAME=favoriteMusic VALUE=Classical>Classical");
out.println("<BR><INPUT TYPE=CHECKBOX " +
"NAME=favoriteMusic VALUE=Country>Country");
out.println("<BR><INPUT TYPE=SUBMIT VALUE=Submit>");
out.println("</FORM>");
out.println("</BODY>");
out.println("</HTML>");
}
public void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
String[] values = request.getParameterValues("favoriteMusic");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
if (values != null ) {
int length = values.length;
out.println("You have selected: ");
for (int i=0; i<length; i++) {
out.println("<BR>" + values);
}
}
}
}
The following happens(please let me know if I am right)
1. init()
2. doGet()
-- if i have my webpage in this function, that is executed.
3. how do i get dopost to fire? I tried putting
<FORM METHOD=POST> into my code, but my dopost code is not fired?
I have example code that does and fires the dopost method which just
writes some of the values entered in the form to the screen. Without
posting tons of code, I am having trouble getting my 'doPost' to fire
from my form which is executed by 'doGet'.
when i click submit, all that happens is that the current screen is
refreshed.
my sample code is as follows. it does not have any 'action' in it.
what in this code is actually getting the 'dopost' method to execute?
this code is out of ;java for the web with servlets' book.
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class HttpRequestDemoServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Obtaining Multi-Value Parameters</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("<BR>");
out.println("<BR>Select your favorite music:");
out.println("<BR><FORM METHOD=POST>");
out.println("<BR><INPUT TYPE=CHECKBOX " +
"NAME=favoriteMusic VALUE=Rock>Rock");
out.println("<BR><INPUT TYPE=CHECKBOX " +
"NAME=favoriteMusic VALUE=Jazz>Jazz");
out.println("<BR><INPUT TYPE=CHECKBOX " +
"NAME=favoriteMusic VALUE=Classical>Classical");
out.println("<BR><INPUT TYPE=CHECKBOX " +
"NAME=favoriteMusic VALUE=Country>Country");
out.println("<BR><INPUT TYPE=SUBMIT VALUE=Submit>");
out.println("</FORM>");
out.println("</BODY>");
out.println("</HTML>");
}
public void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
String[] values = request.getParameterValues("favoriteMusic");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
if (values != null ) {
int length = values.length;
out.println("You have selected: ");
for (int i=0; i<length; i++) {
out.println("<BR>" + values);
}
}
}
}