sachin sav

Greenhorn
+ Follow
since Mar 26, 2004
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by sachin sav

Apologies for posting my code twice is there anyway I can remove both posts.
20 years ago
MIDLET CODE
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class FormPizzaexp extends Form implements CommandListener {

private ChoiceGroup pizzaexpMenu;
private MainScreen midlet;
private Command cmdBack;
private Command cmdSelect;

public FormPizzaexp (String title, MainScreen midlet ){

super (title);
this.midlet = midlet;
cmdBack = new Command("Back", Command.BACK, 1);
cmdSelect = new Command("Select",Command.OK, 2);
pizzaexpMenu = new ChoiceGroup ("What would you like to Order?", Choice.EXCLUSIVE,
new String [] {"ANTIPIZZE", "Garlic Bread �1.95", "Bruschetta �3.25", "Baked Dough Balls �1.95", "SALADS AND VARIATIONS", "Mozzarella and Tomato Salad �6.45", "Lasagne Pasticcate �7.10", "Tortellini �7.10", "PIZZE", "Margherita �4.95", "American Hot �7.70", "Soho Pizza �6.95", "Sloppy Giussepe �7.15", "WINE", "House White Bottle 75cl �10.95", "Half House White Bottle 37.5cl �5.95", "House Red Bottle 75cl �10.95", "Half House Red Bottle 37.5cl �5.95", "SOFT DRINKS", "Coke �1.65", "Sprite �1.65", "Mineral Water �1.00", "Apple Juice �1.65", "DESSERTS", "Choclate Fudge Cake �3.85", "Tiramisu �3.85", "Vanilla Ice Cream �2.10"}, null);
append(pizzaexpMenu);
addCommand(cmdBack);
addCommand(cmdSelect);
setCommandListener(this);

}

public void commandAction(Command c, Displayable s) {
if (c == cmdBack)
midlet.displayMainForm ();
else if (c == cmdSelect){
try{
submitOrder();
}
catch (Exception e){
System.out.println(e.toString());
}


}

}

private void submitOrder () throws IOException {
HttpConnection = null;
InputStream iStrm = null;
String url = "http://localhost:8000/pizzaexporder/pizzaexporderservlet"+ "pizzaexporder=" + pizzaexporder;

boolean selected[] = new boolean [pizzaexpMenu.size()];
pizzaexpMenu.getSelectedFlags(selected);

for (int i=0; i<pizzaexpMenu.size(); i++)
if (selected[i] == true)
String pizzaexporder = pizzaexpMenu.getString(i);


try {
http = (HttpConnection) Connector.open(url);
http.setRequestMethod(HttpConnection.GET);
if (http.getResponseCode() == HttpConnection.HTTP_OK){
iStrm = http.openInputStream();
int length =(int) http.getLength();
if (length>0){
byte servletData[] = new byte [length];
iStrm.read(servletData);
append("You passed to the servlet" + new String(servletData));

}
else
append("Unable to read the data");
}

}
catch (Exception e){
append("Network error");
System.out.println(e.toString());
}
finally {
if (iStrm!=null)
iStrm.close();
if (http != null)
http.close();
}

}
}
SERVLET CODE
import java .io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.sql.*;
public class PizzaExpOrderServlet extends HttpServlet {

private Statement stmt = null;
private Connection conn = null;
private String URL ="jdbc dbc izzaExpOrder";

public void init (ServletConfig config)

throws ServletException {
super.init(config);

try {
Class.forName("Sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection(URL, "", "");

}
catch (Exception e) {
e.printStackTrace () ;
conn = null;
}

}


public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String pizzaexporder = request.getParameter("pizzaexporder");

if (pizzaexporder == null)
{
response.sendError(response.SC_BAD_REQUEST, "Unable to read parameters");
return;
}

PrintWriter output = response.getWriter();
response.setContentType ("text/html");

boolean success = insertIntoDB ("'"+ "',' ',' ','" + pizzaexporder +"',' ','"+ "'");

if (success)
output.print ("<H2>Thank You For Ordering Your Customer Reference is:</H2>");

else
output.print ("<H2> An error ocurred</H2>");

output.close();


}

private boolean insertIntoDB (String stringtoinsert){
try{

stmt = conn.createStatement ();
stmt.execute("INSERT INTO PizzaExpOrder values (" + stringtoinsert + ");");
stmt.close();

}
catch (Exception e) {
System.err.println ("Error: Problems with adding new entry");
e.printStackTrace();
return false;
}
return true;
}

public void destry ()
{
try {
conn.close();
}
catch (Exception e) {
System.err.println ("Problem closing the database");

}
}

}
20 years ago
MIDLET CODE
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class FormPizzaexp extends Form implements CommandListener {

private ChoiceGroup pizzaexpMenu;
private MainScreen midlet;
private Command cmdBack;
private Command cmdSelect;

public FormPizzaexp (String title, MainScreen midlet ){

super (title);
this.midlet = midlet;
cmdBack = new Command("Back", Command.BACK, 1);
cmdSelect = new Command("Select",Command.OK, 2);
pizzaexpMenu = new ChoiceGroup ("What would you like to Order?", Choice.EXCLUSIVE,
new String [] {"ANTIPIZZE", "Garlic Bread �1.95", "Bruschetta �3.25", "Baked Dough Balls �1.95", "SALADS AND VARIATIONS", "Mozzarella and Tomato Salad �6.45", "Lasagne Pasticcate �7.10", "Tortellini �7.10", "PIZZE", "Margherita �4.95", "American Hot �7.70", "Soho Pizza �6.95", "Sloppy Giussepe �7.15", "WINE", "House White Bottle 75cl �10.95", "Half House White Bottle 37.5cl �5.95", "House Red Bottle 75cl �10.95", "Half House Red Bottle 37.5cl �5.95", "SOFT DRINKS", "Coke �1.65", "Sprite �1.65", "Mineral Water �1.00", "Apple Juice �1.65", "DESSERTS", "Choclate Fudge Cake �3.85", "Tiramisu �3.85", "Vanilla Ice Cream �2.10"}, null);
append(pizzaexpMenu);
addCommand(cmdBack);
addCommand(cmdSelect);
setCommandListener(this);

}

public void commandAction(Command c, Displayable s) {
if (c == cmdBack)
midlet.displayMainForm ();
else if (c == cmdSelect){
try{
submitOrder();
}
catch (Exception e){
System.out.println(e.toString());
}


}

}

private void submitOrder () throws IOException {
HttpConnection = null;
InputStream iStrm = null;
String url = "http://localhost:8000/pizzaexporder/pizzaexporderservlet"+ "pizzaexporder=" + pizzaexporder;

boolean selected[] = new boolean [pizzaexpMenu.size()];
pizzaexpMenu.getSelectedFlags(selected);

for (int i=0; i<pizzaexpMenu.size(); i++)
if (selected[i] == true)
String pizzaexporder = pizzaexpMenu.getString(i);


try {
http = (HttpConnection) Connector.open(url);
http.setRequestMethod(HttpConnection.GET);
if (http.getResponseCode() == HttpConnection.HTTP_OK){
iStrm = http.openInputStream();
int length =(int) http.getLength();
if (length>0){
byte servletData[] = new byte [length];
iStrm.read(servletData);
append("You passed to the servlet" + new String(servletData));

}
else
append("Unable to read the data");
}

}
catch (Exception e){
append("Network error");
System.out.println(e.toString());
}
finally {
if (iStrm!=null)
iStrm.close();
if (http != null)
http.close();
}

}
}
SERVLET CODE
import java .io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.sql.*;
public class PizzaExpOrderServlet extends HttpServlet {

private Statement stmt = null;
private Connection conn = null;
private String URL ="jdbc dbc izzaExpOrder";

public void init (ServletConfig config)

throws ServletException {
super.init(config);

try {
Class.forName("Sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection(URL, "", "");

}
catch (Exception e) {
e.printStackTrace () ;
conn = null;
}

}


public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String pizzaexporder = request.getParameter("pizzaexporder");

if (pizzaexporder == null)
{
response.sendError(response.SC_BAD_REQUEST, "Unable to read parameters");
return;
}

PrintWriter output = response.getWriter();
response.setContentType ("text/html");

boolean success = insertIntoDB ("'"+ "',' ',' ','" + pizzaexporder +"',' ','"+ "'");

if (success)
output.print ("<H2>Thank You For Ordering Your Customer Reference is:</H2>");

else
output.print ("<H2> An error ocurred</H2>");

output.close();


}

private boolean insertIntoDB (String stringtoinsert){
try{

stmt = conn.createStatement ();
stmt.execute("INSERT INTO PizzaExpOrder values (" + stringtoinsert + ");");
stmt.close();

}
catch (Exception e) {
System.err.println ("Error: Problems with adding new entry");
e.printStackTrace();
return false;
}
return true;
}

public void destry ()
{
try {
conn.close();
}
catch (Exception e) {
System.err.println ("Problem closing the database");

}
}

}
20 years ago
I have programmed a midlet to send information via a servlet. However I get the following error java.lang.IllegalArgumentException: Space character in URL.
I understand that the error says what it means and that there must be a space in the URL. However I have looked at the URL but see no spaces. Could this error be refering to something else I include my midlet and servlet code below. I have been looking at this for quite a while but cannot see what is wrong. I hope somebody can help.
MIDLET CODE
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;

public class FormPizzaexp extends Form implements CommandListener, Runnable {

private ChoiceGroup pizzaexpMenu;
private MainScreen midlet;
private Command cmdBack;
private Command cmdSelect;

public FormPizzaexp (String title, MainScreen midlet ){

super (title);
this.midlet = midlet;
cmdBack = new Command("Back", Command.BACK, 1);
cmdSelect = new Command("Select",Command.OK, 2);
pizzaexpMenu = new ChoiceGroup ("What would you like to Order?", Choice.EXCLUSIVE,
new String [] {"ANTIPIZZE", "Garlic Bread �1.95", "Bruschetta �3.25", "Baked Dough Balls �1.95", "SALADS AND VARIATIONS", "Mozzarella and Tomato Salad �6.45", "Lasagne Pasticcate �7.10", "Tortellini �7.10", "PIZZE", "Margherita �4.95", "American Hot �7.70", "Soho Pizza �6.95", "Sloppy Giussepe �7.15", "WINE", "House White Bottle 75cl �10.95", "Half House White Bottle 37.5cl �5.95", "House Red Bottle 75cl �10.95", "Half House Red Bottle 37.5cl �5.95", "SOFT DRINKS", "Coke �1.65", "Sprite �1.65", "Mineral Water �1.00", "Apple Juice �1.65", "DESSERTS", "Choclate Fudge Cake �3.85", "Tiramisu �3.85", "Vanilla Ice Cream �2.10"}, null);
append(pizzaexpMenu);
addCommand(cmdBack);
addCommand(cmdSelect);
setCommandListener(this);

}

public void commandAction(Command c, Displayable s) {
if (c == cmdBack)
midlet.displayMainForm ();
else if (c == cmdSelect){

Thread t = new Thread(this);
t.start();



}

}

public void run(){
submitOrder();
}

private void submitOrder () {

HttpConnection http = null;
InputStream iStrm = null;
String pizza = null;

boolean selected[] = new boolean [pizzaexpMenu.size()];
pizzaexpMenu.getSelectedFlags(selected);

for (int i=0; i<pizzaexpMenu.size(); i++)
if (selected[i] == true)
pizza = pizzaexpMenu.getString(i);




String url ="http://localhost:8000/pizzaexporder/PizzaExpOrderServlet"+"pizzaexporder="+pizza;


try {
http = (HttpConnection) Connector.open(url);
http.setRequestMethod(HttpConnection.GET);
if (http.getResponseCode() == HttpConnection.HTTP_OK){
iStrm = http.openInputStream();
int length =(int) http.getLength();
if (length>0){
byte servletData[] = new byte [length];
iStrm.read(servletData);
append("You passed to the servlet" + new String(servletData));

}
else
append("Unable to read the data");
}

}
catch (Exception e){
append("Network error");
System.out.println(e.toString());

}finally {
try {
if (iStrm!=null){
iStrm.close();
}
if (http != null){
http.close();
}
}catch (IOException e){
e.printStackTrace();
}

}

}
}

SERVLET CODE
import java .io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.sql.*;
public class PizzaExpOrderServlet extends HttpServlet {

private Statement stmt = null;
private Connection conn = null;
private String URL ="jdbc dbc izzaExpOrder";

public void init (ServletConfig config)

throws ServletException {
super.init(config);

try {
Class.forName("Sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection(URL, "", "");

}
catch (Exception e) {
e.printStackTrace () ;
conn = null;
}

}


public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String pizzaexporder = request.getParameter("pizzaexporder");

if (pizzaexporder == null)
{
response.sendError(response.SC_BAD_REQUEST, "Unable to read parameters");
return;
}

PrintWriter output = response.getWriter();
response.setContentType ("text/html");

boolean success = insertIntoDB ("'"+ "',' ',' ','" + pizzaexporder +"',' ','"+ "'");

if (success)
output.print ("<H2>Thank You For Ordering Your Customer Reference is:</H2>");

else
output.print ("<H2> An error ocurred</H2>");

output.close();


}

private boolean insertIntoDB (String stringtoinsert){
try{

stmt = conn.createStatement ();
stmt.execute("INSERT INTO PizzaExpOrder values (" + stringtoinsert + ");");
stmt.close();

}
catch (Exception e) {
System.err.println ("Error: Problems with adding new entry");
e.printStackTrace();
return false;
}
return true;
}

public void destry ()
{
try {
conn.close();
}
catch (Exception e) {
System.err.println ("Problem closing the database");

}
}

}
20 years ago
I am in the process of developing a small application which users use to order food at a restaurant using the mobile phone. The food items will be hard coded as a multiple choice group in the midlet. Using the getSelectedFlags() I am able to store the selected options in an array. However I would like to store these in an Access database. Is it possible to send the contents of the array to a database using servlets. I have very limited knowledge about connecting servlets to midlets and writing servlets. In this instance would I use a GET and append the URL with the arrayname or would POST be easier.
One other small thing when testing using System.out.println to see if the array was being populated only true or false was shown with no details about the item selected. Using the getString () I was able to display the name of the item being selected. How can I ensure that only selected items are stored in the array and those that are not selected are not in the array. I will apprecate any help
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class FormPizzaexp extends Form implements CommandListener {

private ChoiceGroup pizzaexpMenu;
private MainScreen midlet;
private Command cmdBack;
private Command cmdSelect;

public FormPizzaexp (String title, MainScreen midlet ){

super (title);
this.midlet = midlet;
cmdBack = new Command("Back", Command.BACK, 1);
cmdSelect = new Command("Select",Command.OK, 2);
pizzaexpMenu = new ChoiceGroup ("What would you like to Order?", Choice.MULTIPLE,
new String [] {"ANTIPIZZE", "Garlic Bread �1.95", "Bruschetta �3.25", "Baked Dough Balls �1.95", "SALADS AND VARIATIONS", "Mozzarella and Tomato Salad �6.45", "Lasagne Pasticcate �7.10", "Tortellini �7.10", "PIZZE", "Margherita �4.95", "American Hot �7.70", "Soho Pizza �6.95", "Sloppy Giussepe �7.15", "WINE", "House White Bottle 75cl �10.95", "Half House White Bottle 37.5cl �5.95", "House Red Bottle 75cl �10.95", "Half House Red Bottle 37.5cl �5.95", "SOFT DRINKS", "Coke �1.65", "Sprite �1.65", "Mineral Water �1.00", "Apple Juice �1.65", "DESSERTS", "Choclate Fudge Cake �3.85", "Tiramisu �3.85", "Vanilla Ice Cream �2.10"}, null);
append(pizzaexpMenu);
addCommand(cmdBack);
addCommand(cmdSelect);
setCommandListener(this);

}

public void commandAction(Command c, Displayable s) {
if (c == cmdBack)
midlet.displayMainForm ();
else if (c == cmdSelect){
boolean selected[] = new boolean [pizzaexpMenu.size()];
pizzaexpMenu.getSelectedFlags(selected);

for (int i=0; i<pizzaexpMenu.size(); i++)
System.out.println (pizzaexpMenu.getStrin(i) +(selected[i]));
}

}


}
20 years ago
I have bought the core j2me sun series book and have adapted some code however the code does not seem to work. The error I get is
unreported exception java.io.IOException; must be caught or declared to be thrown
postInformation();
What does this mean?
Is this to do with the way i have written the URL "http://localhost/8000/midp/loginserv";
The servlet and database are both hosted on the same machine. i have compiled and seployed the servlet. I will appreciate any help. Thank you.
MIDLET CODE
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;


public void commandAction (Command c, Displayable s) {

if (c== cmdSelect){
postInformation();

}else if (c == cmdBack){
midlet.displayMainForm ();
}

}

private void postInformation() throws IOException {
HttpConnection http = null;
OutputStream oStrm = null;
InputStream iStrm = null;
boolean ret = false;

String url = "http://localhost/8000/midp/loginserv";

try {
http = (HttpConnection) Connector.open (url);
oStrm = http.openOutputStream ();
http.setRequestMethod (HttpConnection.POST);
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
//http.setRequestProperty("Connection", "close");
byte data [] = ("username=" + userName.getString()).getBytes();
oStrm.write (data);
data = ("&password=" + passWord.getString()).getBytes();
oStrm.write(data);
oStrm.flush();

iStrm = http.openInputStream();
ret = processServerResponse(http, iStrm);
}
finally
{
if (iStrm != null)
iStrm.close();
if (oStrm != null)
oStrm.close();
if (http != null)
http.close ();

}if (ret == false)
System.out.println("There is something wrong");
}

private boolean processServerResponse (HttpConnection http, InputStream iStrm) throws IOException {

errorMsg = null;

if (http.getResponseCode()==HttpConnection.HTTP_OK)
{
int length = (int) http.getLength ();
String str;
if (length != -1)
{
byte servletData[] = new byte[length];
iStrm.read (servletData);
str = new String(servletData);

}
else
{
ByteArrayOutputStream bStrm = new ByteArrayOutputStream();
int ch;

while ((ch= iStrm.read()) != -1)
bStrm.write(ch);
str = new String (bStrm.toByteArray());
bStrm.close();
}

return true;

}
else
errorMsg = new String (http.getResponseMessage());
return false;
}
}
SERVLET CODE
import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class LoginServlet extends HttpServlet
{

protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{

String username = req.getParameter("username"),
password = req.getParameter("password");
String balance = accountLookup(username, password);
if (balance == null)
{
res.sendError(res.SC_BAD_REQUEST, "Unable to locate account.");
return;
}

res.setContentType("text/plain");
PrintWriter out = res.getWriter();
out.print(balance);
out.close();
}
/*--------------------------------------------------
* Lookup bank account balance in database
*-------------------------------------------------*/
private String accountLookup(String username, String password)
{
Connection con = null;
Statement st = null;
StringBuffer msgb = new StringBuffer("");
try
{
// These will vary depending on your server/database
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc dbc:testdb");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("Select members from testdb where username = " +username + "and password = '" + password + "'");

if (rs.next())
return rs.getString(1);
else
return null;
}
catch (Exception e)
{
return e.toString();
}
}

}
20 years ago
I am a bit confused on the POST and GET. Would the midlet in which the user enters their password and username perform a POST and a servlet which retrieves the password/usernane from the midlet perform GET. Being new to this what is ota?
Really appreciate the help.
20 years ago
Hi! I would appreciate any help :-) I am relatively new to J2ME. I have midlet which allows the user to enter a user name and password. How do I authenticate this so that if the user name and password match the database they are permitted access if not then it displays a message to register. The database is a simple MS Access database.
This is all i have sofar

import javax.microedition.lcdui.*;
public class FormSubclass extends Form implements CommandListener {

private TextField passWord;
private TextField userName;
private Command cmdBack;
private MainScreen midlet;

public FormSubclass (String title, MainScreen midlet ){

super (title);
this.midlet = midlet;
passWord = new TextField("Password", "",8,TextField.PASSWORD);
userName = new TextField ("User Name", "",8,TextField.ANY);
cmdBack = new Command ("Back", Command.BACK, 1);
append (userName);
append (passWord);
addCommand (cmdBack);
setCommandListener (this);
}

public void commandAction (Command c, Displayable s) {

if (c == cmdBack)
midlet.displayMainForm ();
}
20 years ago