views:

162

answers:

4

Hi, I am new to JSP and related technologies. I need to write a JSP form with some required fields (including Captcha), the form will require validation. Upon successful submission of the form, it should be able to email to a stated email address which is grabbed/parsed from a .txt file.

That is basically the flow. But technically how should I do it in JSP/java? Is there any good tutorial referencing to my above form requirement? How should I be able to grab/parse the text file. And lastly, I remembered that php has a function called mail() to do the emailing, how should I do it in jsp?

Thank you very much.

A: 

That's a lot of questions in one question; here are a couple links which might be helpful (i.e., we've used them in the past):

Kaptcha: http://code.google.com/p/kaptcha/

JavaMail: http://java.sun.com/products/javamail/

ZoogieZork
Also, I'll let others take the lead of suggesting Java web frameworks; I haven't used a large enough variety to suggest one over another (especially for a beginner). :)
ZoogieZork
@ZoogieZork: Sorry, there really are quite a lot of questions in one question. :P Thanks, I will check out the Kaptcha and the JavaMail.
jl
stay away from javamail - it is overly complex. Commons-email is the way to go
Bozho
A: 
  • use jsp to show your form
  • submit your form to a HttpServlet
  • perform the validation - redirect to the same page on failure
  • on success, send the email using apache commons-email
  • use reCaptcha for captcha
Bozho
A: 

Try using skeleton app AppFuse. It offers different frameworks from the basic to advanced ones. Here's an article on captcha integration.

Boris Pavlović
+2  A: 

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.

BalusC