String str = request.getParameter("sentence"); System.out.println("before convert - " + str); str = new String(request.getParameter("sentence").getBytes("ISO-8859-1"),"UTF-8"); System.out.println("after convert - " + str);
Category: Programing Languages
Programing Languages
Round a double to any decimal places in java
int n = 2;
round(xxxx.xxxxx, n); // Return xxxx.xx
Install Java in ubuntu 18.04
java -version
sudo apt install apt-get update
sudo apt install default-jdk
Send email In java using smtp
//import jar file from https://mvnrepository.com/artifact/javax.mail/mail/1.4.7
package com.mail.api.controller;
import com.sun.mail.smtp.SMTPTransport;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Mail {
public static boolean sendMail(Email email){
EmailTemplate template = email.getEmailTemplate();
int SMTP_PORT = 465;
final String EMAIL_TO = "";
final String EMAIL_TO_CC = "";
final String EMAIL_TO_BCC = "";
final String EMAIL_SUBJECT = "";
final String EMAIL_TEXT = "";
Properties prop = System.getProperties();
//prop.put("mail.debug", "true");
prop.put("mail.smtp.host", SMTP_SERVER); //optional, defined in SMTPTransport
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.socketFactory.port",SMTP_PORT);
prop.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
prop.put("mail.smtp.socketFactory.fallback", "false");
prop.put("mail.smtp.starttls.enable", "true");
prop.put("mail.smtp.ssl","true");
prop.put("mail.smtp.port", SMTP_PORT); // default port 25
Session session = Session.getInstance(prop, null);
Message msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress(EMAIL_FROM));
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(EMAIL_TO, false));
msg.setRecipients(Message.RecipientType.CC,InternetAddress.parse(EMAIL_TO_CC, false));
msg.setRecipients(Message.RecipientType.BCC,InternetAddress.parse(EMAIL_TO_BCC, false));
msg.setSubject(EMAIL_SUBJECT);
msg.setContent(EMAIL_TEXT,"text/html");
msg.setSentDate(new Date());
// Get SMTPTransport
SMTPTransport t = (SMTPTransport) session.getTransport("smtp");
t.connect(SMTP_SERVER, SMTP_USERNAME, SMTP_PASSWORD);
t.sendMessage(msg, msg.getAllRecipients());
System.out.println("Response: " + t.getLastServerResponse());
t.close();
return true;
} catch (MessagingException e) {
e.printStackTrace();
}
return false;
}
}
Amazon SES setup with django
EMAIL_BECKEND = 'django_smtp_ssl.SSLEmailBackend' (or EMAIL_BECKEND = 'backends.smtp.SSLEmailBackend')
EMAIL_HOST = 'email-smtp.us-east-1.amazonaws.com'
EMAIL_PORT = 465
EMAIL_HOST_USER = 'SMTP USERNAME'
EMAIL_HOST_PASSWORD = 'SMTP PASSWORD'
EMAIL_USE_TLS = True
Run Python Project On Port 80 Apache2 Configuration
<VirtualHost *:80>
ServerName domain.com
ServerAlias www.domain.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/(python project folder Name)
<Directory /var/www/html/(python project folder Name)>
AllowOverride All
</Directory>
Alias /static /var/www/html/(python project folder Name)/static
<Directory /var/www/html/(python project folder Name)/static>
Require all granted
</Directory>
Alias /static /var/www/html/(python project folder Name)/media
<Directory /var/www/html/(python project folder Name)/media>
Require all granted
</Directory>
<Directory /var/www/html/(python project folder Name)/(python project folder Name)>
<Files wsgi.py>
Require all granted
</Files>
Require all granted
</Directory>
LoadModule wsgi_module “/var/www/html/myenv/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so”
WSGIDaemonProcess myenv1 python-path=/var/www/html/(python project folder Name):/var/www/html/myenv/lib/python3.6/site-packages
WSGIProcessGroup myenv1
WSGIScriptAlias / /var/www/(python project folder Name)/(python project folder Name)/wsgi.py
WSGIPassAuthorization On
WSGIApplicationGroup %{GLOBAL}
</VirtualHost>
Setup server for python ubuntu 16.04 LTE
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install apache2
sudo apt-get install python-pip
python –version
sudo pip install –upgrade pip
sudo pip install –upgrade virtualenv
sudo apt-get install mysql-server libmysqlclient-dev
cd document root
virtualenv myvenv
source myvenv/bin/activate
pip freeze (show install modules)
pip install Django
pip install mysqlclient