I'm sorry, I forgot to add test(email) in the condition that send the email.

package net.sourceforge.subsonic.controller;

import java.io.IOException;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.mail.*;
import javax.mail.internet.*;

public class ContactServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
	
		
	String email = request.getParameter("email");
	String subject = request.getParameter("subject");
	String message = request.getParameter("message");
		
	request.setAttribute("emailState", email);
	request.setAttribute("subState", subject);
	request.setAttribute("msgState", message);
	
	if (email.isEmpty()){
		request.setAttribute("err_email", "Please proivde a email");
	}
	if (!test(email)){
		request.setAttribute("err_email", "Provided incorrect email");
	}
	if (message.isEmpty()){
		request.setAttribute("err_msg", "Please provide a message");
	}
	if (subject.isEmpty()){
		request.setAttribute("err_sub", "Please provide a subject");
	}
	
		
		if(!email.isEmpty() && !message.isEmpty() && !subject.isEmpty() && test(email)){
			Properties props = new Properties();
			props.put("mail.smtp.host", "mysmptserver.com");
			props.put("mail.smtp.port", "587");
			
	        	Session session = Session.getDefaultInstance(props, null);
			
		try {
	                Message m = new MimeMessage(session);
	                m.setFrom(new InternetAddress(email, ""));
	                m.addRecipient(Message.RecipientType.TO, new InternetAddress("myadress@mydomain.com", ""));
	                m.setSubject(subject);
	                m.setText(message);
	                Transport.send(m);

	        } catch (AddressException e) {
	        	e.printStackTrace();
	        } catch (javax.mail.MessagingException e) {
		        e.printStackTrace();
		}
	        
		RequestDispatcher view = request.getRequestDispatcher("success.jsp");
			
		view.forward(request, response);
		
			}else{
		RequestDispatcher view = request.getRequestDispatcher("contact.jsp");
			
		view.forward(request, response);
			
			

		}
	}
	public static boolean test(String str){
		String reg = "[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})";
		Pattern p = Pattern.compile(reg);
		Matcher m = p.matcher(str);
		while(m.find())
		{
			String s1 = m.group(0);
			if (str == s1) return true;
		}

		return false;
	}

}

Contact form:

<form action="Contact.do" method="get">
    <p>Your email address: <input name="email" value="<% if(request.getAttribute("emailState") != null) out.println(request.getAttribute("emailState")); %>"></p><% if(request.getAttribute("err_email") != null) out.println(request.getAttribute("err_email")); %>
    <p>Mail subject: <input name="subject" value="<% if(request.getAttribute("subState") != null) out.println(request.getAttribute("subState")); %>"><% if(request.getAttribute("err_sub") != null) out.println(request.getAttribute("err_sub")); %></p>
    <p>Mail message: <textarea name="message"><% if(request.getAttribute("msgState") != null) out.println(request.getAttribute("msgState")); %></textarea><% if(request.getAttribute("err_msg") != null) out.println(request.getAttribute("err_msg")); %></p>
    <p><input type="submit"></p>
</form>

Thanks! cant get it to save the information in the subject form it looks like this:

<fmt:message key="help.text.subject"/>:<br>
    <option value="" selected="true"><fmt:message key="help.text.choose"/></option>
    <option value="Help"><fmt:message key="help.text.help"/></option>
    <option value="Other"><fmt:message key="help.text.other"/></option>

i also would like something that writes a message "Your Message Has been sent!" on the same contact.jsp instead of redrict to another page, if this is possible.

It's easy, use a condition on each option.

<% String s = (String) request.getAttribute("subState"); %>
    <option value=""><fmt:message key="help.text.choose"/></option>
    <option value="Help" <% if (s.equals("Help")) out.println("selected=\"selected\""); %>><fmt:message key="help.text.help"/></option>
    <option value="Other" <% if (s.equals("Other")) out.println("selected=\"selected\""); %>><fmt:message key="help.text.other"/></option>

Instead of using RequestDispatcher object in ContactServlet for the success page, redirect to the same contact form, send an attribute with request.setAttribute() , then finally in the contact form is practical to use a condition (check the attribute isn't null), to display the message.

It's easy, use a condition on each option.

<% String s = (String) request.getAttribute("subState"); %>
    <option value=""><fmt:message key="help.text.choose"/></option>
    <option value="Help" <% if (s.equals("Help")) out.println("selected=\"selected\""); %>><fmt:message key="help.text.help"/></option>
    <option value="Other" <% if (s.equals("Other")) out.println("selected=\"selected\""); %>><fmt:message key="help.text.other"/></option>

tried that code and got a java.lang.NullPointerException error!


ive got this code now. but still not working!

<fmt:message key="help.text.subject"/>:<br>
	<select name="subject"> <% String s = (String) request.getAttribute("subState"); %>
    <option value="" selected="true"><fmt:message key="help.text.choose"/></option>
    <option value="Option1"> <% if (s.equals("Option1")) out.println("selected=\"selected\""); %>><fmt:message key="help.text.1"/></option>
    <option value="Option2"> <% if (s.equals("Option2")) out.println("selected=\"selected\""); %>><fmt:message key="help.text.2"/></option>
    <option value="Option3"> <% if (s.equals("Option3")) out.println("selected=\"selected\""); %>><fmt:message key="help.text.3"/></option>
    <option value="Option4"> <% if (s.equals("Option4")) out.println("selected=\"selected\""); %>><fmt:message key="help.text.4"/></option>
    </select>
    <span class="warning">
<% if(request.getAttribute("err_sub") != null) out.println(request.getAttribute("err_sub")); %>
	</span>

Add s != null && before s.equals("Option3") on each condition

Add s != null && before s.equals("Option3") on each condition

<fmt:message key="help.text.subject"/>:<br>
	<select name="subject"> 
	<% String s = (String) request.getAttribute("subState"); %>
    <option value="" <fmt:message key="help.text.choose"/></option>
    <option value="Songs"> <% if s != null && (s.equals("Songs")) out.println("selected=\"selected\""); %>><fmt:message key="help.text.songs"/></option>
    <option value="Albums"> <% if s != null && (s.equals("Albums")) out.println("selected=\"selected\""); %>><fmt:message key="help.text.albums"/></option>
    <option value="Artists"> <% if s != null && (s.equals("Artists")) out.println("selected=\"selected\""); %>><fmt:message key="help.text.artists"/></option>
    <option value="Others"> <% if s != null && (s.equals("Others")) out.println("selected=\"selected\""); %>><fmt:message key="help.text.other"/></option>
    </select>
    <span class="warning">
<% if(request.getAttribute("err_sub") != null) out.println(request.getAttribute("err_sub")); %>
	</span>

got nullexpetion error!

<% if s != null && (s.equals("Songs"))

There's a missing parenthesis, should be:

<% if (s != null && s.equals("Songs"))

Do the same in the other cases.

There's a missing parenthesis, should be:

<% if (s != null && s.equals("Songs"))

Do the same in the other cases.

Null errors are gone but its not saving the information.

<fmt:message key="help.text.subject"/>:<br>
	<select name="subject"> 
	<% String s = (String) request.getAttribute("subState"); %>
    <option value="" Selected="True" <fmt:message key="help.text.choose"/></option>
    <option value="Songs"> <% if (s != null && s.equals("Songs")) out.println("selected=\"selected\""); %>><fmt:message key="help.text.songs"/></option>
    <option value="Albums"> <% if (s != null && s.equals("Albums")) out.println("selected=\"selected\""); %>><fmt:message key="help.text.albums"/></option>
    <option value="Artists"> <% if (s != null && s.equals("Artists")) out.println("selected=\"selected\""); %>><fmt:message key="help.text.artists"/></option>
    <option value="Others"> <% if (s != null && s.equals("Others")) out.println("selected=\"selected\""); %>><fmt:message key="help.text.others"/></option>
    </select>
    <span class="warning">
<% if(request.getAttribute("err_sub") != null) out.println(request.getAttribute("err_sub")); %>
	</span>

Remove Selected="True" and add > after the value on line 4

Remove Selected="True" and add > after the value on line 4

Did that and now when i Push submit it looks like this

That happens because before <% if (s != null && s.equals("Songs")) out.println("selected=\"selected\""); %> there is an extra > symbol, needs to be removed on each case.

That happens because before <% if (s != null && s.equals("Songs")) out.println("selected=\"selected\""); %> there is an extra > symbol, needs to be removed on each case.

Its working now :) was thinking about the email validate filter, i still can write "gfdgfdg43g@iusdadha.com" and send the message, it would be nice with a nice filter that blocks this and the filter blocks this mail which is bad because it can be a real mail " test@mky-ong.com" also find out that if i click the submit button like 3 times and my mail is not valid, it will automatycly write a space in mail form.

An extremely complex filter maybe could perform a domain check and a smtp validation but that can be slow... and that could take time to develop.

An extremely complex filter maybe could perform a domain check and a smtp validation but that can be slow... and that could take time to develop.

Okay nevermind then hehe, i think i have everything i want now. thanks for your help!

An extremely complex filter maybe could perform a domain check and a smtp validation but that can be slow... and that could take time to develop.

Hmm there is a strange bug when someone writes a bad email and then push submit, it adds a space after eachtime you press the submit so the mail will become invalid and the user wont be able to send a message. how to fix this?

I can't reproduce the error. Any other hint you could give me?

I can't reproduce the error. Any other hint you could give me?

Hmm you will see what i mean with this video.
clip0001.zip

Maybe, I'm using different application server and browser. Use trim() to remove leading/trailing spaces in ContactServlet:

request.setAttribute("emailState", email.trim());

Maybe, I'm using different application server and browser. Use trim() to remove leading/trailing spaces in ContactServlet:

request.setAttribute("emailState", email.trim());

Thanks as i can see this working in IE and FF not google chrome. anymore help that can fix this?

This can be solved using a trim function from client-side, Javascript.

This can be solved using a trim function from client-side, Javascript.

Okay but i think its working now, tried it with opera,safari,firefox,internet explorer, only buggs in google chrome and i dont care about that. do you know how to fix a onlinecounter on the site?

Post the issue in a separate thread. Post the code and we'll try to fix it.

Hi..
I tried this code but this is not working for me can you please help me solve this

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.