Send SMS from Java (or JSP)

Published by java tech on

Send SMS from Java

In the Java Server Pages (JSP), you can send and receive emails. using this code you also can send and receive short message service (SMS) messages. You may send and receive the messages in Java Applications using the Java Mail and Communications libraries.

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset=”ISO-8859-1″>
<title>Insert title here</title>
</head>
<body>
<form method=”post” action=”SendSms”>
<input type=”text” name=”mono” placeholder=”ex:91xxxxxxxxxx” required=”required”>
<input type=”text” name=”message” placeholder=”message content” required=”required”>
<input type=”submit” value=”Send SMS”>
</form>
</body>

</html>

—————————————————————————————————————————–

SendSms.java

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(“/SendSms”)
public class SendSms extends HttpServlet {
private static final long serialVersionUID = 1L;

public SendSms() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String mono =  request.getParameter(“mono”);
String msg = request.getParameter(“message”);
String url = “XXXXXXXX”;  //Bulk SMS API URL

try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestMethod(“GET”);
int responseCode = connection.getResponseCode();
if (responseCode != 200) {
System.out.println(“not ok”);
}
response.sendRedirect(“index.html”);

} catch (IOException e) {

}

}

}

OUTPUT

(Visited 19 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *