JSP is just a view technology providing a template to write client side markup/style/scripting languages in, such as HTML/CSS/JS code, alongside with the possibility to control the page flow dynamically with help of taglibs such as JSTL and access to backend data with help of EL. In your speficic case a plain vanilla HTML form would already suffice.
<form action="servletname" method="post">
<input type="text" name="foo"><br>
<input type="text" name="bar"><br>
<input type="submit"><br>
</form>
To control, preprocess and/or postprocess the request and response, the best way is to use a Servlet
. Basically just extend HttpServlet
and implement the doGet()
to preprocess data or the doPost()
to postprocess the data. The servlet can be mapped on a certain url-pattern
in web.xml. The HTML form action URL should match this url-pattern
.
If you want to make use of the very same form to redisplay the submitted page and any error messages, then you can just make use of EL:
<form action="servletname" method="post">
<input type="text" name="foo" value="${param.foo}" ${not empty messages.succes ? 'disabled' : ''}>
<span class="error">${messages.foo}</span><br>
<input type="text" name="bar" value="${param.bar}" ${not empty messages.succes ? 'disabled' : ''}>
<span class="error">${messages.bar}</span><br>
<input type="submit">
<span class="succes">${messages.succes}</span><br>
</form>
Where ${messages}
is basically a Map<String, String>
which you've put in the request scope in the servlet. For example:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Map<String, String> messages = new HashMap<String, String>();
request.setAttribute("messages", messages);
String foo = request.getParameter("foo");
String bar = request.getParameter("bar");
if (foo == null || foo.trim().isEmpty()) {
messages.put("foo", "Please enter this field");
}
if (bar == null || bar.trim().isEmpty()) {
messages.put("bar", "Please enter this field");
}
if (messages.isEmpty()) {
YourMailer.send(createTemplate(foo, bar), mailto);
messages.put("succes", "Mail successfully sent!");
}
// At end, forward request to JSP page for display:
request.getRequestDispatcher("pagename.jsp").forward(request, response);
}
More about JSP/Servlet can be found in Java EE 5 tutorial part II chapters 4-9 and in Marty Hall's Coreservlets.com tutorials. To go a step further you can always abstract all the boilerplate stuff (request parameter retrieval, value conversion/validation, event handling, navigation, etcetera) away with help of any MVC framework which is built on top of Servlet API, like Sun JSF, Apache Struts, Spring MVC, etcetera.
With regard to captcha's, you can just use any Java Captcha API to your taste and follow the instructions. Often they have their own servlet/filter which stores a key/toggle/signal in the request or session scope which determines whether the captcha did match or not. You can just access its outcome inside your servlet.
With regard to mailing, you can just use any Java mail API to your taste, however the choice is only limited to the great JavaMail API and the more convenienced one provided by Apache which is built on top of JavaMail API.