Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Space character in URL error when running midlet

 
sachin sav
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");

}
}

}
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you post, there is a "CODE" tag that you can use so that your posted code still keeps its formatting, like indentation. Posting code like you did makes it very difficult to read and makes that even more difficult for us to help you.
There are Buttons under the "Add Reply" button that can help.
Mark
 
sachin sav
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");

}
}

}
 
sachin sav
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");

}
}

}
 
sachin sav
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies for posting my code twice is there anyway I can remove both posts.
 
kok seong khew
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, just want to test on the CODE tag as mentioned...
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sachin sav:
Apologies for posting my code twice is there anyway I can remove both posts.
Yes, there is. Just click on the paper-and-pen button on top of the post you want to remove, check the "Delete post" checkbox, and click the "Edit post" form submit button.
 
David Price
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, I think I may see why there's a space in your URLs. See this line:

And 'pizza' is one of the entries in your ChoiceGroup, e.g. "Margherita �4.95". So your URL will be:
"http://localhost:8000/pizzaexporder/PizzaExpOrderServletpizzaexporder=Margherita �4.95"
I.e., the space is before the price. Also, I expect you meant to include a "?" between the servlet name and the query parameter name. And you've still got a problem in that some pizza names include spaces. You could use code like this:

To really do it right, you should 'URL encode' the pizza name. Have a look at the JavaDocs for J2SE class java.net.URLEncoder, then implement a small method to do the same thing (since that class is not part of MIDP).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic