Wednesday, 7 December 2011

Run your web application on HTTPS (HTTP over SSL) protocol

I want to send post request using https(over secure socket layer) protocol.



Run Your java web application on https(http over ssl)

Step 1:
First you need signed certificate

for that write following in command prompt

Windows:

%JAVA_HOME%\bin\keytool -genkey -alias tomcat -keyalg RSA

Unix:

$JAVA_HOME/bin/keytool -genkey -alias tomcat -keyalg RSA

Step 2:

using step 1 keystore file would be generated, whist is saved in user home directory.
file named .keystore.

step 3:

open server.xml file from tomcat folder.

uncomment following code and add keystore filepath and keystorepassword which is given by you during generating

keystore.

<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
               maxThreads="150" scheme="https" secure="true"
               keystoreFile="C:/Documents and Settings/Administrator/.keystore" keystorePass="tomcat"
               clientAuth="false" sslProtocol="TLS" />



Step 4:



Add following lines of code into your web.xml file

<security-constraint> <!-- This tag describe security constraint of our web application -->


<web-resource-collection>
<web-resource-name>app or resourcename</web-resource-name><!-- this is mandatory. It is used for internal purpose. It will not used any other place.  -->
<url-pattern>/*</url-pattern> <!-- write here url pattern on which you want to apply security constraint
ex. <url-pattern>/login.do</url-pattern> -->
<http-method>GET</http-method>
<http-method>POST</http-method> <!-- Put here method list in this tag which is restricted -->
</web-resource-collection>

<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee><!-- There are three types of transportation.  None, Integral and confidential, Write here confidential to send request on http over ssl(https).-->
</user-data-constraint>
</security-constraint>

Full code is here.


<security-constraint>
<web-resource-collection>
<web-resource-name>app or resourcename</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>

save  web.xml file and deploy and run your application.
When login.do servlet will execute , url redirect to https.


https://localhost:8443

Run multiple tomcat at same time

If you want to run multiple tomcat runs concurrently, then you can run them at same times.

But you just need to change port number into server.xml file.

To change port number you just follow these steps.


1. Go to tomcat x/conf/server.xml
2. In server.xml file you will see connector tag.
3. there are port attribute in connector tag.
4. Change the value of port attribute.
5. save file and restrart tomcat.
Ex.
<connector port="8181" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

Wednesday, 23 November 2011

Send Mail using java application


YOU CAN SEND EMAIL TO MULTIPLE EMAIL ADDRESSES WITH THIS JAVA PROGRAM.


import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Properties;
import java.util.Random;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author Krunal Prajapati
 */
public class DownloadBO {

    private static final String SMTP_HOST_NAME = "10.61.36.74";
    private static final String SMTP_AUTH_USER = "username";
    private static final String SMTP_AUTH_PWD = "password";
//    private static final String emailMsgTxt = "This mail is sent by a jsp page.so when ever we want to send an E-Mail we can call that Jsp Page only. Thank You.";
    private static final String emailSubjectTxt = "Download Request Approved";
    private static final String emailFromAddress = "yourname@example.com";
    // Add List of Email address to who email needs to be sent to
    private static final String[] emailList = {"name1@example.com,
yourname2@example.com"};

   

    public void sendEmailToRequester(Statement stmt, String link, String email,  String expiryDate) throws MessagingException {

        DownloadBO downloadBO = new DownloadBO();
        downloadBO.postMail(email, emailSubjectTxt, link, emailFromAddress, 
                            expiryDate);
        System.out.println("Sucessfully Sent mail to All Users");
    }

    public void postMail(String email, String subject, String message, String  
                         from, String expiryDate) throws MessagingException {
        boolean debug = false;


        //Set the host smtp address
        Properties props = new Properties();
        props.put("mail.smtp.host", SMTP_HOST_NAME);
        props.put("mail.smtp.auth", "true");

        Authenticator auth = new SMTPAuthenticator();
        Session session = Session.getDefaultInstance(props, auth);

        session.setDebug(debug);

        // create a message
        Message msg = new MimeMessage(session);

        // set the from and to address
        InternetAddress addressFrom = new InternetAddress(from);
        msg.setFrom(addressFrom);

        InternetAddress addressTo = new InternetAddress();

        addressTo = new InternetAddress(email);

        msg.setRecipient(Message.RecipientType.TO, addressTo);


        // Setting the Subject and Content Type
        msg.setSubject(subject);
        msg.setContent("Dear User, \n\n Please write your message body here",
 "text/plain");
        Transport.send(msg);
    }


    /**
     * SimpleAuthenticator is used to do simple authentication
     * when the SMTP server requires it.
     */
    private class SMTPAuthenticator extends javax.mail.Authenticator {

        public PasswordAuthentication getPasswordAuthentication() {
            String username = SMTP_AUTH_USER;
            String password = SMTP_AUTH_PWD;
            return new PasswordAuthentication(username, password);
        }
    }

   public static void main(String args[]) throws MessagingException {
        DownloadBO bO = new DownloadBO();
        bO.sendEmailToRequester(emailMsgTxt);
    }
}