• Post Reply Bookmark Topic Watch Topic
  • New Topic

using of classes in jsp  RSS feed

 
manohar gunturu
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Help me gentle men

i have 3 .java files in a pakage way2sms:

they are

//credentials

package way2sms;

import java.net.URLEncoder;
import java.util.ArrayList;

public class Credentials {
private ArrayList< String > list = new ArrayList< String >();

//Set
public void set(String name, String value) {
StringBuilder buffer = new StringBuilder();

buffer.append(name);
buffer.append("=");
buffer.append(getUTF8String(value));

add(buffer.toString());
}

//Append
public void append(String name, String value) {
StringBuilder buffer = new StringBuilder();

buffer.append("&");
buffer.append(name);
buffer.append("=");
buffer.append(getUTF8String(value));

add(buffer.toString());
}

//Add
private void add(String item) {
list.add(item);
}

//Get UTF8 String
private String getUTF8String(String value) {
String encodedValue = null;

try {
encodedValue = URLEncoder.encode(value, "UTF-8");
} catch(Exception exception) {
System.err.println("Encoding error! Please try agian...");
System.exit(1);
}

return encodedValue;
}

//Is Empty
public boolean isEmpty() {
return list.isEmpty();
}

//Reset
public void reset() {
list.clear();
}

//Get User Credentials
public String getUserCredentials() {
StringBuilder buffer = new StringBuilder();
int size = list.size();

for(int i = 0; i < size; i++)
buffer.append(list.get(i));

return buffer.toString();
}
}


//url connector
package way2sms;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ByteArrayOutputStream;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;

public class URLConnector {
private static HttpURLConnection connection;
private static Proxy proxy;

//Set Proxy
public static void setProxy(String host, int port) {
proxy = new Proxy(Proxy.Type.HTTP, java.net.InetSocketAddress.createUnresolved(host, port));
}

//Connect
public static void connect(String urlPath, boolean redirect, String method, String cookie, String credentials) {
try {
URL url = new URL(urlPath);

if(null != proxy)
connection = (HttpURLConnection) url.openConnection(proxy);
else
connection = (HttpURLConnection) url.openConnection();

connection.setInstanceFollowRedirects(redirect);

if(cookie != null)
connection.setRequestProperty("Cookie", cookie);

if(method != null && method.equalsIgnoreCase("POST")) {
connection.setRequestMethod(method);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
}

connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.4) Gecko/20100101 Firefox/10.0.4");

connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);

if(credentials != null) {
DataOutputStream wr = new DataOutputStream (connection.getOutputStream ());
wr.writeBytes (credentials);
wr.flush ();
wr.close ();
}
} catch(Exception exception) {
System.out.println("Connection error");
}
}

//Get Cookie
public static String getCookie() {
String cookie = null;

if(connection != null) {
String headerName=null;

for (int i = 1; (headerName = connection.getHeaderFieldKey(i)) != null; i++) {
if (headerName.equals("Set-Cookie")) {
cookie = connection.getHeaderField(i).split(";")[0];
break;
}
}
}

return cookie;
}

//Get Location
public static String getLocation() {
String location = null;

if(connection != null) {
String headerName=null;

for (int i = 1; (headerName = connection.getHeaderFieldKey(i)) != null; i++) {
if (headerName.equals("Location")) {
location = connection.getHeaderField(i).split(";")[0];
break;
}
}
}

return location;
}

//Get Response Code
public static int getResponseCode() {
int responseCode = -1;

if(connection != null) {
try {
responseCode = connection.getResponseCode();
} catch(Exception exception) {
System.err.println("Response code error");
}
}

return responseCode;
}

//Get Response
public static String getResponse() {
ByteArrayOutputStream bos = new ByteArrayOutputStream();

if(connection != null) {
try {
InputStream is = connection.getInputStream();


int next = is.read();

while (next > -1) {
bos.write(next);
next = is.read();
}

bos.flush();
} catch(Exception exception) {
System.err.println("Response error");
}
}

return new String(bos.toByteArray());
}

//Get Error Message
public static String getErrorMessage() {
StringBuilder errorMessage = new StringBuilder();

if(connection != null) {
try {
InputStream es = connection.getErrorStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(es));

String line;
while((line = rd.readLine()) != null) {
errorMessage.append(line);
errorMessage.append("\r\n");
}

rd.close();
} catch(Exception exception) {
System.err.println("Error in getting error message");
}
}

return errorMessage.toString();
}

//Disconnect
public static void disconnect() {
if(connection != null)
connection.disconnect();
}
}



//Way2Sms (entry point)
package way2sms;

import java.net.HttpURLConnection;
import java.util.Calendar;

public class Way2Sms {
private static int responseCode = -1;
private static String userCredentials;
private static String cookie;
private static String token;
private static String site;
private static String actionStr;
private static String random1;
private static String random2;
private static String random3;
private static Credentials credentials = new Credentials();

public static void main(String[] args) {
//setProxy("127.0.0.1", 8080);
login("your mobile number", "password");
sendSMS("receivers mobile number", "message");
//sendSMS(new String[] { "receivers mobile no. 1", "receivers mobile no. 2", "..." }, "message");
System.out.println("Message has been sent successfully!");
}

//Set Proxy
private static void setProxy(String host, int port) {
URLConnector.setProxy(host, port);
}

//Login
private static void login(String uid, String pwd) {
getSite();
preHome();

String location = null;

credentials.set("username", uid);
credentials.append("password", pwd);
credentials.append("userLogin", "no");
credentials.append("button", "Login");
userCredentials = credentials.getUserCredentials();

URLConnector.connect("http://" + site + "/w2sauth.action", false, "POST", cookie, userCredentials);
responseCode = URLConnector.getResponseCode();
if(responseCode != HttpURLConnection.HTTP_MOVED_TEMP && responseCode != HttpURLConnection.HTTP_OK)
exit("authentication failed!");
else
location = URLConnector.getLocation();
URLConnector.disconnect();

URLConnector.connect(location, false, "GET", cookie, null);
responseCode = URLConnector.getResponseCode();
if(responseCode != HttpURLConnection.HTTP_MOVED_TEMP && responseCode != HttpURLConnection.HTTP_OK)
exit("redirection failed!");
URLConnector.disconnect();
}

//Send SMS
private static void sendSMS(String receiversMobNo, String msg) {
getActionString();
credentials.reset();

credentials.set("t_15_k_5", random1);
credentials.append("i_m", "sn2sms");
credentials.append("m_15_b", random2);
if(actionStr != null)
credentials.append("kriya", actionStr);
else
exit("Action string missing!");
credentials.append(random3, "");
credentials.append(random1, token);
credentials.append("catnamedis", "Birthday");
credentials.append("chkall", "on");
credentials.append("diffNo", Calendar.getInstance().getTime().toString().split(" ")[3]);
credentials.append(random2, receiversMobNo);
credentials.append("txtLen", "" + (140 - msg.length()));
credentials.append("textArea", msg);

userCredentials = credentials.getUserCredentials();
URLConnector.connect("http://" + site + "/jsp/w2ssms.action", false, "POST", cookie + "; 12489smssending34908=67547valdsvsikerexzc435457", userCredentials);
responseCode = URLConnector.getResponseCode();

if(responseCode != HttpURLConnection.HTTP_MOVED_TEMP && responseCode != HttpURLConnection.HTTP_OK)
exit("sendSMS failed!");
URLConnector.disconnect();
}

//Send SMS (Same message to multiple users)
private static void sendSMS(String[] receiversMobNos, String msg) {
int noOfReceivers = receiversMobNos.length;

for(int i = 0; i < noOfReceivers; i++)
sendSMS(receiversMobNos[i], msg);
}

/***************************** UTILITY METHODS ***************************
**************************************************************************/

//Get Site
private static void getSite() {
URLConnector.connect("http://www.way2sms.com/", false, "GET", null, null);
responseCode = URLConnector.getResponseCode();
if(responseCode != HttpURLConnection.HTTP_MOVED_TEMP && responseCode != HttpURLConnection.HTTP_OK)
exit("getSite failed!");
else {
site = URLConnector.getLocation();
if(site != null)
site = site.substring(7, site.length() - 1);
}
URLConnector.disconnect();
}

//Pre Home
private static void preHome() {
URLConnector.connect("http://" + site + "/content/prehome.jsp", false, "GET", null, null);
responseCode = URLConnector.getResponseCode();
if(responseCode != HttpURLConnection.HTTP_MOVED_TEMP && responseCode != HttpURLConnection.HTTP_OK)
exit("preHome failed");
else {
cookie = URLConnector.getCookie();
token = cookie.substring(cookie.indexOf("~") + 1);
}
URLConnector.disconnect();
}


//Get Action String
private static void getActionString() {
URLConnector.connect("http://" + site + "/jsp/SingleSMS.jsp?Token=" + token, false, "GET", cookie, null);
responseCode = URLConnector.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_MOVED_TEMP || responseCode == HttpURLConnection.HTTP_OK) {
String str = URLConnector.getResponse();

String aStr = "id='kriya' value='";

int index = str.indexOf(aStr);
if(index > 0) {
index = index + aStr.length();

StringBuffer actionStringChars = new StringBuffer();
char ch = str.charAt(index);

while(ch != '\'') {
actionStringChars.append(ch);
ch = str.charAt(++index);
}

actionStr = actionStringChars.toString();
getRandom1(str);
getRandom2(str);
getRandom3(str);
}
} else
exit("getActionString failed!");
URLConnector.disconnect();
}

//Random 1
private static void getRandom1(String str) {
String aStr = "id='t_15_k_5'";

int index = str.indexOf(aStr);
if(index > 0) {
index = index + aStr.length();

StringBuffer random1Chars = new StringBuffer();
char ch = str.charAt(index);

if(ch == '>') {
ch = str.charAt(++index);

while(ch != '<') {
random1Chars.append(ch);
ch = str.charAt(++index);
}
} else {
index += 8;
ch = str.charAt(index);

while(ch != '\'') {
random1Chars.append(ch);
ch = str.charAt(++index);
}
}

random1 = random1Chars.toString();
}
}

//Random 2
private static void getRandom2(String str) {
String aStr = "id='m_15_b'";

int index = str.indexOf(aStr);
if(index > 0) {
index = index + aStr.length();

StringBuffer random2Chars = new StringBuffer();
char ch = str.charAt(index);

if(ch == '>') {
ch = str.charAt(++index);

while(ch != '<') {
random2Chars.append(ch);
ch = str.charAt(++index);
}
} else {
index += 8;
ch = str.charAt(index);

while(ch != '\'') {
random2Chars.append(ch);
ch = str.charAt(++index);
}
}

random2 = random2Chars.toString();
}
}

//Random 3
private static void getRandom3(String str) {
String aStr = "dnipb";

int index = str.lastIndexOf(aStr);
if(index > 0) {

index -= 40;

StringBuffer random3Chars = new StringBuffer();
char ch = str.charAt(index);

while(ch != '\"') {
random3Chars.append(ch);
ch = str.charAt(--index);
}

random3 = random3Chars.reverse().toString();
}
}

//Exit
private static void exit(String errorMsg) {
System.err.println(errorMsg);
System.exit(1);
}
}


now my question is from a jsp page i'have to give input to login("your mobile number", "password");
sendSMS("receivers mobile number", "message");
(or)

How to put this kind of java code in to a .jsp page
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Java code does not belong in a JSP. If it's already a proper Java class, then just use it as a backing bean, of from within a servlet.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!