Sending a HTTP request from mobile (J2ME) to servletFUs

Pervious tutorial links:

Create J2ME Application In Eclipse

For this application we need to create 2 projects (J2EE, J2ME)

Creation of J2EE maven project
Creation of J2EE maven project

Click on the Next button

Click on the Next button_Image

Choose the appropriate option ‘Maven-archetype-webapp’ then click on the Next

option ‘Maven-archetype-webapp

Maven project is created which is shown below

 Click on Finish after entering details

Maven project is created which is shown below

Maven project is created

The created Maven project structure is shown below.

Maven project structure

After creating the project add plugin then add dependencies in ‘pom.xml’ after that in ‘web.xml’ also we are mapping the ‘servlet class’.

servlet class
servlet class

This class is created in J2EE project

/*Servlet Class*/
package; import; import; import; import; import; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class DBConnection extends HttpServlet { private static final long serialVersionUID = 1L; public void init() { } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, EOFException { DataInputStream in = new DataInputStream( (InputStream) request.getInputStream()); String user = in.readUTF(); String pwd = in.readUTF(); response.setContentType("text/plain"); PrintWriter out = response.getWriter(); String userid = connect(user.toLowerCase().trim(), pwd.toLowerCase() .trim()); if (userid == null) { response.sendError(response.SC_BAD_REQUEST, "Unable to locate user."); return; } out.println(userid); out.close(); in.close(); out.flush(); } private String connect(String user, String pwd) { String username = "root"; String password = "root"; Connection con = null; // Statement stmt = null; try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/sample",username,password); Statement stmt = con.createStatement(); ResultSet rs = stmt .executeQuery("SELECT * FROM login where username='" + user+ "' and password='" + pwd + "'"); System.out.println("hai"); // con.close(); // displaying records if ( { System.out.println("shekhar"); return rs.getString(2); } else { con.close(); return null; } } catch (Exception e) { e.printStackTrace(); return e.toString(); } } }

Create J2ME project

Create J2ME project

Click on Next

click Next

Here we give the project name.

give the project name

Click on Finish
Project is created

Project is created

Project structure

After that we create ‘com.gk’ package in that we create ‘midlet’ class click on the Next

click On midlet

Click on the finish button



/*MIDlet class*/ this is created in J2ME project

package com.gk; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.TextField; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; public class Login extends MIDlet implements CommandListener { private String username; private Display display; private Command exit = new Command("EXIT", Command.EXIT, 1); private Command login = new Command("Login", Command.SCREEN, 1); public TextField tb1; public TextField tb2; public Login() { display = Display.getDisplay(this); } protected void destroyApp(boolean arg0) { } protected void pauseApp() { } protected void startApp() throws MIDletStateChangeException { Form menu = new Form("Login Form"); tb1 = new TextField("Please input user Name: ","",30, TextField.ANY); tb2 = new TextField("Please input password: ", "", 30, TextField.PASSWORD); menu.append(tb1); menu.append(tb2); menu.addCommand(exit); menu.addCommand(login); menu.setCommandListener(this); display.setCurrent(menu); } public void commandAction(Command c, Displayable display) { if (c == exit) { destroyApp(false); notifyDestroyed(); } else if (c == login) { // here we are creating instance for DBConn() DBConn db = new DBConn(this); // here calling connctdb() db.connectDb(tb1.getString(), tb2.getString()); // it goes to start () in DBConn class db.start(); } } }

/*DBConnection in servlet (J2EE)*/

package com.gk; import; import; import; import; import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; public class DBConn implements Runnable,CommandListener { Login midlet; //Servlet url private String url = "http://localhost:8099/J2EE/DBConnection"; private Display display; String user; String pwd; public DBConn(Login midlet) { this.midlet = midlet; //here displaying the all midlet fields display = Display.getDisplay(midlet); } public void start() { Thread t = new Thread(this); t.start();// here its moving to run() } public void run() { // here bulk of data will be stored b using StringBuffer StringBuffer sb = new StringBuffer(); try { // connection between the mobile to servlet using url HttpConnection c = (HttpConnection); //c.setRequestProperty("c", "close"); c.setRequestProperty("User-Agent", "Profile/MIDP-1.0, Configuration/CLDC-1.0"); c.setRequestProperty("Content-Language", "en-US"); // it moves to post() in servlet class c.setRequestMethod(c.POST); DataOutputStream os = (DataOutputStream) c .openDataOutputStream(); System.out.println(user.trim()); os.writeUTF(user.trim()); os.writeUTF(pwd.trim()); os.flush(); os.close(); DataInputStream is = (DataInputStream) c.openDataInputStream(); // is = c.openInputStream(); int ch; sb = new StringBuffer(); while ((ch = != -1) { sb.append((char) ch); } System.out.println(sb); showAlert(sb.toString()); is.close(); c.close(); } catch (Exception e) { showAlert(e.getMessage()); } } public void connectDb(String user, String pwd) { this.user = user; this.pwd = pwd; } private void showAlert(String err) { Alert a = new Alert(""); a.setString(err); a.setTimeout(Alert.FOREVER); display.setCurrent(a); } public void commandAction(Command arg0, Displayable arg1) { // TODO Auto-generated method stub } }

Execution Process

First run the J2EE application like.

Execution Process

executed Project

After that you run J2ME1 application select→Login→run

run the project

default phone with details to login

default phone login and enetr the details

Select login command

Select login command click on 'yes'

Select yes

login successful

Login successfully.

HTML Comment Box is loading comments...