Click here to Skip to main content
15,887,776 members
Home / Discussions / Java
   

Java

 
GeneralRe: Trying to understand this Pin
Waldermort15-Apr-11 0:40
Waldermort15-Apr-11 0:40 
AnswerRe: Trying to understand this Pin
CodingLover14-Apr-11 18:24
CodingLover14-Apr-11 18:24 
QuestionUnable to load C# Dlls from Java Application Pin
Vodka_Chill14-Apr-11 3:02
Vodka_Chill14-Apr-11 3:02 
AnswerRe: Unable to load C# Dlls from Java Application Pin
Richard MacCutchan14-Apr-11 5:29
mveRichard MacCutchan14-Apr-11 5:29 
AnswerRe: Unable to load C# Dlls from Java Application Pin
jschell14-Apr-11 9:49
jschell14-Apr-11 9:49 
QuestionIWAB0379E Unable to open http://localhost:8080/WebServiceProject/CalculatorPort?WSDL Pin
R V Reddy13-Apr-11 22:29
R V Reddy13-Apr-11 22:29 
AnswerRe: IWAB0379E Unable to open http://localhost:8080/WebServiceProject/CalculatorPort?WSDL Pin
David Skelly14-Apr-11 1:43
David Skelly14-Apr-11 1:43 
Questionsent from J2ME to webServer Pin
williamroma13-Apr-11 10:21
williamroma13-Apr-11 10:21 
hi everybody
(SORRY FOR MY BAD ENGLISH)
i want to send something from mobile application wrote in J2ME to web server wrote in JSP with JAVA Servlet
my question is how to get the string sent from J2ME to Web Server
the code for J2ME is

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author William
 */
public class midle extends MIDlet implements CommandListener {

    private static String defalutUrl="http://localhost:8080/DeleteWebApplication/RomaServlet";
    //GUI to input string form the user
    private Display myDisplay=null;
    private Form mainScreen;
    private TextField requestField;

    //GUI for displaying header information
    private Form resultScreen;
    private StringItem resultField;

    //the "SEND" button used in the mainScreen
    Command sendCommand = new Command("SEND", Command.OK, 1);

    //the "BACK" button used in the resultScreen
    Command backCommand = new Command("BACK", Command.OK, 1);

    public midle() {
        myDisplay=Display.getDisplay(this);
        mainScreen=new Form("ROMA4EVER");
        requestField=new TextField("enter a string please", "", 100, TextField.ANY);
        mainScreen.append(requestField);
        mainScreen.addCommand(sendCommand);
        mainScreen.setCommandListener(this);
    }

    public void commandAction(Command c, Displayable d) {
        if(c==sendCommand){
            //retrieving the String that user had entered
            String requestString= requestField.getString();
            //sending a POST request to the web server
            String resultString= sendPostRequest(requestString);
            //displaying the response back from the web server
            resultScreen=new Form("Azzuri");
            resultField=new StringItem(null,resultString);
            resultScreen.append(resultField);
            resultScreen.addCommand(backCommand);
            resultScreen.setCommandListener(this);
            myDisplay.setCurrent(resultScreen);
        }else{
            if(c==backCommand){
                //do it all again
                requestField.setString("SOMETHING GOOD");
                myDisplay.setCurrent(mainScreen);
            }else{
            }
        }
    }

    //send a POST request to the web server
    public String sendPostRequest(String requestString){
        HttpConnection hc=null;
        DataInputStream dis=null;
        DataOutputStream dos=null;
        StringBuffer messagebuffer=new StringBuffer();
        try {
            // open a http connection with the web server to send and recieve operation
            hc=(HttpConnection)Connector.open(defalutUrl,Connector.READ_WRITE);
            //set the request method to POST
            hc.setRequestMethod(HttpConnection.POST);
            //sent the string entered by user byte by byte
            dos=hc.openDataOutputStream();
            byte[] requst_budy=requestString.getBytes();
            for(int i=0;i<requst_budy.length;i++){
                dos.writeByte(requst_budy[i]);
            }
            dos.flush();
            dos.close();
            //retrieve the response back from the web server
            dis=new DataInputStream(hc.openInputStream());
            int ch;
            //first we have to check the content lenght
            long len=hc.getLength();
            if(len!=-1){
                for(int i=0;i<len;i++)
                    if((ch=dis.read())!=-1){
                        messagebuffer.append((char)ch);
                    }else{
                        while((ch=dis.read())!=-1){
                            messagebuffer.append((char)ch);
                        }
                    }
            }
            dis.close();
        } catch (IOException ioe) {
            messagebuffer.append("ERROR!");
        }finally{
            //free up i/0 streams and http connection
            try {
                if(hc!=null)
                    hc.close();
            } catch (Exception e) {
            }
            try {
                if(dis!=null)
                    dis.close();
            } catch (Exception e) {
            }
            try {
                if(dos!=null)
                    dos.close();
            } catch (Exception e) {
            }
            return messagebuffer.toString();
        }
    }

    protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {

    }

    protected void pauseApp() {

    }

    protected void startApp() throws MIDletStateChangeException {
        myDisplay.setCurrent(mainScreen);
    }

}





the code for web Server is

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package web;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author William
 */
public class RomaServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String username=request.getParameter(null);
        PrintWriter out = response.getWriter();
        
        String totti="";
        for(int i=username.length()-1;i>=0;i--){
            totti=totti+username.charAt(i);
        }
        //out.close();
        System.out.println(request.getParameterMap().size());
        try {
             //TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet RomaServlet</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet RomakServlet at " +totti+ "</h1>");
            out.println("</body>");
            out.println("</html>");
            
        } finally { 
            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
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    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
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}


in the web server code what should i do to receive the message sent from J2ME application
thank you very much
AnswerRe: sent from J2ME to webServer Pin
CodingLover14-Apr-11 18:26
CodingLover14-Apr-11 18:26 
GeneralRe: sent from J2ME to webServer Pin
williamroma15-Apr-11 0:16
williamroma15-Apr-11 0:16 
GeneralRe: sent from J2ME to webServer Pin
CodingLover15-Apr-11 8:28
CodingLover15-Apr-11 8:28 
Questionhow to perform drag and drop funtion Pin
manoj_indits12-Apr-11 22:01
manoj_indits12-Apr-11 22:01 
AnswerRe: how to perform drag and drop funtion Pin
TorstenH.12-Apr-11 22:19
TorstenH.12-Apr-11 22:19 
QuestionTo develope own Java IDE Pin
manoj_indits12-Apr-11 21:54
manoj_indits12-Apr-11 21:54 
AnswerRe: To develope own Java IDE Pin
Luc Pattyn12-Apr-11 22:47
sitebuilderLuc Pattyn12-Apr-11 22:47 
AnswerRe: To develope own Java IDE Pin
CodingLover14-Apr-11 18:29
CodingLover14-Apr-11 18:29 
Questionsending sms from Net to Mobile Pin
R V Reddy12-Apr-11 2:45
R V Reddy12-Apr-11 2:45 
AnswerRe: sending sms from Net to Mobile Pin
Richard MacCutchan12-Apr-11 4:36
mveRichard MacCutchan12-Apr-11 4:36 
GeneralRe: sending sms from Net to Mobile Pin
TorstenH.12-Apr-11 21:37
TorstenH.12-Apr-11 21:37 
Questionmabye this question about netBeans(i dont' konw ) Pin
williamroma9-Apr-11 0:31
williamroma9-Apr-11 0:31 
AnswerRe: mabye this question about netBeans(i dont' konw ) Pin
williamroma9-Apr-11 1:13
williamroma9-Apr-11 1:13 
AnswerRe: mabye this question about netBeans(i dont' konw ) Pin
hyjiacan11-Apr-11 14:43
hyjiacan11-Apr-11 14:43 
Questionquestion Pin
fauz_m6-Apr-11 18:27
fauz_m6-Apr-11 18:27 
AnswerNot the answer Pin
Luc Pattyn7-Apr-11 1:51
sitebuilderLuc Pattyn7-Apr-11 1:51 
AnswerBut a suggestion Pin
Richard MacCutchan8-Apr-11 1:42
mveRichard MacCutchan8-Apr-11 1:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.