So, you want to obtain user input, eh?
I put together a simple little multimedia tutorial, along with sample code, that shows you how to do just that:
Obtaining User Input with an HTML Form, a Servlet and a JSP The key is your form:
<FORM method="post" action="NumberGuesserServlet">
<INPUT type="text" name="guess" size="10">
<INPUT type="submit"
name="submit" value="SUBMIT"></FORM>
And your Servlet/JSP obtaining data from the form:
public class NumberGuesserServlet extends HttpServlet implements Servlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String magicNumber = System.currentTimeMillis() % 9 + 1 +"";
String guess = request.getParameter("guess");
if (guess.equals(magicNumber)){
response.getWriter().print("Correct! The magic number was: " + magicNumber);
}
else {
response.getWriter().print("Sorry, the magic number was: "+ magicNumber);
}
}
}
Enjoy the Tutorial!
-Cameron McKenzie