21/06/2007, 07:34
|
| | Fecha de Ingreso: junio-2007
Mensajes: 12
Antigüedad: 17 años, 5 meses Puntos: 0 | |
Re: Conectar J2ME con Apache Que pasa Luis! sabes quien soy? jejeje
a ver, segun lo q yo se necesitas un servlet que pilla las conexiones procedentes del telefono movil. Es más necesitarás una clase que haga las funciones de cliente dentro de tu aplicación J2ME y un servlet que pille esas conexiones y las procese.
Este es un ejemplo de servlet:
package GlucoServer;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import GlucoServer.Cnst;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.exceptions.*;
/**
* Servlet that manages the conexion between phone and server.
* @author jaime
*/
public class GlucoServlet extends HttpServlet {
private Connection conn;
/** Type of measure*/
private byte typeOfMeasure;
public GlucoServlet(){
super();
try{
Class.forName("com.mysql.jdbc.Driver").newInstance ();
conn = DriverManager.getConnection("jdbc:mysql:///bdglucoweb","root", "pub7ro2");
}catch(Exception e){
e.printStackTrace();
}
}
/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet GlucoServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet GlucoServlet at " + request.getContextPath () + "</h1>");
out.println("</body>");
out.println("</html>");
out.close();
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/** Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/** Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
InputStream is = request.getInputStream();
OutputStream os = response.getOutputStream();
DataInputStream dis = new DataInputStream(is);
DataOutputStream dos = new DataOutputStream(os);
String userID = dis.readUTF();
String password = dis.readUTF();
if(!authenticate(userID,password)){
dos.write(Cnst.RESP_AUTH_ERROR);
dis.close();
dos.close();
is.close();
os.close();
return;
}
byte opCode = dis.readByte();
switch(opCode){
case Cnst.OP_STORE: byte typeOfMeasure = dis.readByte();
storePackets(dis,dos,userID);
break;
case Cnst.OP_APP: queryApp(dis,dos,userID);
}
dis.close();
dos.close();
is.close();
os.close();
}
.............
y el cliente del J2ME podría tener esta pinta...
/** Creates a new instance of NWclient */
public NWclient() {
start();
}
/**
* Only one instance of the client allowed
* @return Instance of the class
*/
public static NWclient getInstance(){
if (instance == null){
instance = new NWclient();
}
return instance;
}
/**
* Server communication running in background
*/
public synchronized void run(){
while (true){
try{
wait();
conn = (HttpConnection) Connector.open(Cnst.SERVER_URL,Connector.READ_WRIT E,true);
conn.setRequestMethod(conn.POST);
dos = conn.openDataOutputStream();
dos.writeUTF(userID);
dos.writeUTF(password);
dos.write(opCode);
switch(opCode){
case Cnst.OP_STORE: dos.write (typeOfMeasure);
sendPackets(dos);
evaluateResponse(opCode);
break;
case Cnst.OP_APP: dos.flush();
evaluateResponse(opCode);
break;
}
}catch(Exception e){
e.printStackTrace();
ActionsController.networkError2();
}finally{
try{
if (dos!=null) dos.close();
if (dis!=null) dis.close();
if (conn!=null) conn.close();
}catch(Exception e){}
}
}
}
el tema de OP STORE Y OP APP es de mi programa olvidalo...
espero q t oriente un poco...
tira! |