Java Servlet: Send form data by email. (Netbeans)
I'm using Netbeans to write a simple program, but I'm a little confused and would like some help. Already made a lot of research but servlets are new to me.
I have an Java web app created, simple website coded in html that must gather information from a form and send it to an specific email. The email is always the same, and will be stored in the submit button in the form.
Everything is fine with the html code, the problem is that once I create the servlet I don't know how to link it to the html code or form in it.
Any help will be good.
I don't know how to link it to the html code or form in it.
Map the servlet on an URL pattern in web.xml or by @WebServlet annotation. Let the action attribute of the <form> point to exactly that URL. Assuming that form's method is POST, collect the request parameters in doPost() method of the servlet. Finally do your job with them the usual Java way.
<form action="servletURL" method="post"> <input type="text" name="foo" /> ... </form>
with in doPost() method
String foo = request.getParameter("foo"); // ...
- Our Servlets wiki page - Contains some Hello World examples and several good tutorial links
Sending a mail in a servlet is not different from in a plain vanilla Java class with a main() method. So you don't necessarily need to focus on "sending a mail with servlet" for which you won't find much examples, but just on "sending a mail with java" for which you will certainly find much examples.
you can find example here for servlet and jsp.
Please refer to this link : http://www.servletworld.com/servlet-tutorials/simple-servlet-example.html In the doPost() method, they are generating an html response. In addition the the HTML response, you can write the code to send email. For that, please refer this link: http://www.javapractices.com/topic/TopicAction.do?Id=144