How To Launch Servlet from Desktop App

Hello Everyone,
I recently started my programing in JavaEE. First lesson was Servlets.
My servlet takes the name and a number from a user using a html form and returns a wellcoming message to the user and calculates PI with "number" decimals.
Well it works using a html form but the next exercise is to launch the servlet from a desktop application. I searched like 2 hours on google and different sites for this. I saw a hint about creating a framework(code was writen in 2001).
I really really have no idea how to launch the Servlet from a desktop client application.

The exercise says:
PI Servlet
Write a servlet capable of calculating the PI number with a specified number of decimals. Access this servlet:
1. From a browser, using a HTML form
2. From a desktop application
For the calculation of PI use Machin formula PI/4 = 4 arctan(1/5) - arctan(1/239) or a similar one.
For the calculation of arctan function use Taylor series: arctan(x) = x - (x^3)/3 + (x^5)/5 - (x^7)/7 + (x^9)/9 ...
My servlet looks like this: without the Methods for calculating PI:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
         try {
            int len = request.getContentLength();
            byte[] input = new byte[len];
            ServletInputStream sin = request.getInputStream();
            int c, count = 0 ;
            while ((c = sin.read(input, count, input.length-count)) != -1) {
                count +=c;
            sin.close();
            String inString = new String(input);
            int index = inString.indexOf("=");
            if (index == -1) {
                response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                response.getWriter().print("Eroare la servlet");
                response.getWriter().close();
                return;
            String value = inString.substring(index + 1);
            //decode application/x-www-form-urlencoded string
            String decodedString = URLDecoder.decode(value, "UTF-8");
            int decc = Integer.parseInt(decodedString);
            String result = CalculatePI(decc).toString();
            PrintWriter out = response.getWriter();
            try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Pi Servlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Pi Servlet  </h1>");
            out.println("<p> Wellcome <br /> PI cu "+ decodedString + "zecimale este: <p>");
            out.println("<br />" + result);
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
            // set the response code and write the response data
            response.setStatus(HttpServletResponse.SC_OK);
            OutputStreamWriter writer = new OutputStreamWriter(response.getOutputStream());
            writer.write(decodedString);
            writer.write(result);
            writer.flush();
            writer.close();
        catch (IOException e) {
            try{
                response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                response.getWriter().print(e.getMessage());
                response.getWriter().close();
            catch (IOException ioe) {
    } The Html form :
<form action="PiServlet" method ="POST">
    Give PI decimals <input type="text" name="PI" size="20"><br />
    <input type="submit" value ="Submit" >
    <input type ="reset" value="Reset" >
</form>And my desktop application :
public static void main( String [] args ) throws Exception {
           if (args.length != 1) {
         System.err.println("errrrr");
         System.exit(1);
     String piString = URLEncoder.encode(args[0], "UTF-8");
     URL url = new URL("http://localhost:8084/MySecondServlet/");
     URLConnection connection = url.openConnection();
     connection.setDoOutput(true);
     OutputStreamWriter out = new OutputStreamWriter(
                              connection.getOutputStream());
     out.write("string=" + piString);
         System.out.println(piString);
     out.close();
     BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                    connection.getInputStream()));
     String decodedString;
     while ((decodedString = in.readLine()) != null) {
         System.out.println(decodedString);
     in.close();
    }Edited by: ELuCID on Jul 22, 2009 9:42 PM
Edited by: ELuCID on Jul 22, 2009 10:11 PM

Similar Messages

  • How to launch an Air desktop app from a second Air desktop app?

    Hi,
    Is there a way to launch an Air desktop app from a second Air desktop app?
    I'm using Air 4 with Flash CS 6 on Windows.
    Thanks

    According to Adobe documentation, AIR doesn't allow you to launch one application using another on desktop. The best bet that I could find at the moment would be to use the File.openWithDefaultAppilcation() function and open a file(like a .doc or .txt file) that has an AIR app set as the default application to use for X filetype. Unfortunately, launching app(.exe, .app, etc) files directly is not allowed. Probably for safety/security reasons. You might be able to get around that with a Native Extension though. Below is a link to the File.openWithDefaultApplication() method which includes a table of all the filetypes not allowed. Tried messing around with some ways around it, but the best I could do was open a new Finder window(testing on a Mac) and highlight the file. The code below assumes that both AIR apps are installed in the same app directory. Not sure how it would work on Windows but the result might be the same. For Mac, no special characters or anything was needed even though the app I was trying to open had spaces in the filename.
    var filePath:String = "file://" + File.applicationDirectory.nativePath + "/App to highlight.app";
    navigateToURL(new URLRequest(filePath));
    File - Adobe ActionScript® 3 (AS3 ) API Reference

  • How to launch photoshop from creative cloud

    i just got the free trail of Photoshop cc. it didn't put a an icon on my desktop (which is fine) but i have no idea how to launch it from the cloud. please help!

    check your programs/programs x86 or applications folder for an adobe > adobe photoshop folder.

  • How to migrate mails from Google Apps to MS Exchange Online IMAP (Getting error)

    Any tips on How to migrate mails from Google Apps to MSOL? What is required? When I am trying to migrate using IMAP but getting fpollowing error
    =======================
    Summary: 1 item(s). 0 succeeded, 1 failed.
    Elapsed time: 00:00:11
    [email protected]
    Failed
    Error:
    Failed to log on successfully for the following reason:
    Server rejected Basic login with following message : * CAPABILITY IMAP4rev1 UNSELECT LITERAL+ IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE.
    Exchange Management Shell command attempted:
    'Microsoft.Exchange.Transporter.Provider.PopImap.InternetMailboxMeta' | Move-XsIMAPMailboxToExchangeOnline -AllowUnsecureConnection $false -TargetCredential 'System.Management.Automation.PSCredential' -MaxThreadCount '10' -Quiet
    Elapsed Time: 00:00:11
    ======================
    Any help will be much appreciated.
    Regards
    Sunil DK

    I just finished migrating a client from Google Apps to Microsoft Exchange Online using the IMAP option in the Microsoft Online Services Migration Tool.  
    I added the mailboxes by creating a CSV file [i.e. GoogleApps(Gmail)_Mailboxes.csv] in the following format:
    SourceIdentity,SourceServer,SourceLoginID,SourcePassword,TargetIdentity
    [email protected],imap.gmail.com,[email protected],P@ssword1,[email protected]
    [email protected],imap.gmail.com,[email protected],P@ssword2,[email protected]
    [email protected],imap.gmail.com,[email protected],P@ssword3,[email protected]
    Then I created a custom folder map XML file [i.e. GoogleApps(Gmail)_FolderMap.xml] to map the Google Apps (Gmail) labels to the appropriate Exchange (Outlook) mailbox folders and create those that didn't exist (i.e. Important, Starred, Follow up,
    Misc, Priority) as sub-folders under the Inbox:
    <?xml version="1.0" encoding="utf-8"?>
    <FolderMappings xmlns="http://tempuri.org/FolderMap.xsd">
    <!-- This xml contains the mapping of foldername in source server to folders in target server -->
    <!--
    "path" is the name of the folder in source server
    "Name" is the name of the folder to be mapped into target server
    "SpecialFolder" is the name of the special folder to be mapped into
    target server (Name will be ignored) valid values are :
    Inbox
    Calendar
    Tasks
    Sent Items
    Deleted Items
    Drafts
    Junk E-mail
    Contacts
    Outbox
    Journal
    Notes
    "Description" Description of the folder
    "ExcludeFolder" indicates folders to exclude. Valid values are:
    true (case sensitive)
    false (case sensitive)
    0
    1
    -->
    <!-- Default Mapping Section -->
    <Folder path = "INBOX">
    <Property SpecialFolder = "Inbox"/>
    </Folder>
    <Folder path = "New Mail">
    <Property SpecialFolder = "Inbox"/>
    </Folder>
    <Folder path = "[Root]">
    <Property SpecialFolder = "Inbox"/>
    </Folder>
    <Folder path = "">
    <Property SpecialFolder = "Inbox"/>
    </Folder>
    <Folder path = "Calendar">
    <Property SpecialFolder = "Calendar"/>
    </Folder>
    <Folder path = "Tasks">
    <Property Name = "Migration items/Tasks"/>
    </Folder>
    <Folder path = "Sent Items">
    <Property SpecialFolder = "Sent Items"/>
    </Folder>
    <Folder path = "Sent Mail">
    <Property SpecialFolder = "Sent Items"/>
    </Folder>
    <Folder path = "Sent">
    <Property SpecialFolder = "Sent Items"/>
    </Folder>
    <Folder path = "Deleted Items">
    <Property SpecialFolder = "Deleted Items"/>
    </Folder>
    <Folder path = "Trash">
    <Property SpecialFolder = "Deleted Items"/>
    </Folder>
    <Folder path = "Drafts">
    <Property SpecialFolder = "Drafts"/>
    </Folder>
    <Folder path = "Draft">
    <Property SpecialFolder = "Drafts"/>
    </Folder>
    <Folder path = "Junk E-mail">
    <Property SpecialFolder = "Junk E-mail"/>
    </Folder>
    <Folder path = "Spam">
    <Property SpecialFolder = "Junk E-mail"/>
    </Folder>
    <Folder path = "Contacts">
    <Property Name = "Migration Items/Contacts"/>
    </Folder>
    <Folder path = "Outbox">
    <Property SpecialFolder = "Outbox"/>
    </Folder>
    <Folder path = "Journal">
    <Property SpecialFolder = "Journal"/>
    </Folder>
    <Folder path = "Notes">
    <Property SpecialFolder = "Notes"/>
    </Folder>
    <Folder path = "Public Folders">
    <Property ExcludeFolder = "true"/>
    </Folder>
    <!-- Google Apps (Gmail) Specific Mapping Section -->
    <Folder path = "[Gmail]/All Mail">
    <Property ExcludeFolder = "true"/>
    </Folder>
    <Folder path = "All Mail">
    <Property ExcludeFolder = "true"/>
    </Folder>
    <Folder path = "[Gmail]/Drafts">
    <Property SpecialFolder = "Drafts"/>
    </Folder>
    <Folder path = "[Gmail]/Important">
    <Property Name = "Inbox/Important"/>
    </Folder>
    <Folder path = "[Gmail]/Sent Mail">
    <Property SpecialFolder = "Sent Items"/>
    </Folder>
    <Folder path = "[Gmail]/Spam">
    <Property SpecialFolder = "Junk E-mail"/>
    </Folder>
    <Folder path = "[Gmail]/Starred">
    <Property Name = "Inbox/Starred"/>
    </Folder>
    <Folder path = "[Gmail]/Trash">
    <Property SpecialFolder = "Deleted Items"/>
    </Folder>
    <!-- Custom Mapping Section -->
    <Folder path = "Follow up">
    <Property Name = "Inbox/Follow up"/>
    </Folder>
    <Folder path = "Misc">
    <Property Name = "Inbox/Misc"/>
    </Folder>
    <Folder path = "Priority">
    <Property Name = "Inbox/Priority"/>
    </Folder>
    </FolderMappings>
    Additionally, I had to tell the Internet E-mail Mailbox Migration Wizard to use the "Individual Account Credentials" option since I was able to specify each account password in the CSV file when adding the mailboxes to Microsoft Online Services Migration
    Tools console.
    Just to note, the users had to manually export their contacts to a CSV file and calendars to iCal (ICS) files in order to import those items into Outlook.
    I hope this will benefit others since Microsoft doesn't seem to have a custom, specifically defined migration strategy for this scenario.  With Google Apps being in direct competition with BPOS/Office365, I assumed that Microsoft would have a simple
    strategy using something like ActiveSync to facilitate the migration of email, contacts, calendar entries, etc.  Hopefully we'll see something along those lines become available in a future release of the migration tools.
    --Jon Payne

  • HT1296 i have downloaded songs through free mp3 app. how to transfer songs from this app to my laptop

    I have downloaded songs through free mp3 app which has a download features in the task bar and all the songs downloaded show up in this. How to transfer songs from this app to my laptop.
    Thanks

    Connect your iPad to iTune (computer) and use File Sharing to copy the MP3 to computer.
    http://i1224.photobucket.com/albums/ee374/Diavonex/Album%208/318aff1df0a4de96506 56dbe6ebb40d6.jpg

  • HT204266 how to transfer ringtones from RMaker app to my iphone 4S?

    How to transfer ringtones from RMaker app to my iphone 4S?

    You want to transfer your iPhone ringtones to your Mac.
    iPubsoft iPad iPhone iPod to PC Transfer is the best tool for iPhone User to backup iPhone Music, Photos’, Song and other video audio files, including your ringtones.
    More:
    How to copy iphone ringtone to computer

  • How to transfer movie from the apps in iphone to my laptop with window 7 os

    how to transfer movie from the apps in iphone to my laptop with window 7 os ?? Pls helpppp =)

    Movie from what app? If this is a movie that you took, with the video camera of the phone, then see this. iOS: Import personal photos and videos from iOS devices to your computer

  • I cant launch creative cloud desktop app... it is already installed...

    i cant launch creative cloud desktop app... it is already installed...

    Online Chat Now button near the bottom for Activation and Deactivation problems may help
    http://helpx.adobe.com/x-productkb/policy-pricing/activation-deactivation-products.html

  • I am based in India, how do i buy from the app store using an indian credit card? The app store is saying that my apple id is valid only for US purchases and that I need a US credit card!

    I am based in India, how do i buy from the app store using an indian credit card? The app store is saying that my apple id is valid only for US purchases and that I need a US credit card!

    snigdhachavan wrote:
    so does this mean i cannot download anything from the app store.. ...
    Not the US Store... if you are in India.
    snigdhachavan wrote:
    ... i wanted to buy the Mac OS X Lion... how do i do the purchase?
    http://www.apple.com/in/mac/app-store/

  • How do I move from Canadian app store to US app store when in Canada?

    How do I move from Canadian app store to US app store when in Canada?

    Hello Hoperyngo,
    I found an article with steps to change the language on the App and iTunes Stores:
    Change your iTunes Store country
    Sign in to the account for the iTunes Store region you'd like to use. Tap Settings > iTunes & App Stores > Apple ID: > View Apple ID > Country/Region
    Follow the onscreen process to change your region, agree to the terms and conditions for the region if necessary, and then change your billing information.
    You can find the full article here:
    iOS: Changing the signed-in iTunes Store Apple ID account
    http://support.apple.com/kb/ht1311
    Thank you for posting in the Apple Support Communities.
    Best,
    Sheila M.

  • HT204266 How do I switch from German apps store to US store?

    How do I switch from German apps store to US store?

    sc00845 wrote:
    I have the same issue. I followed the instructions and successfully changed to the US store  but my App Store and iTunes still accesses the old German one and so I cannot find or download US app store apps.
    Do you meet the following requirements: 1) have a valid billing address in the U.S., 2) have a credit card billed to that address or an iTunes gift card purchased in the U.S. and 3) are you physically present in the U.S.?
    If you don't meet all of those requirements, you can't purchase from the U.S. Store.

  • How do I go from an app directly to safari

    how do I go from an app directly to safari

    Use a multitasking gesture to switch between Apps.
    See http://manuals.info.apple.com/en_US/ipad_user_guide.pdf

  • How to stop iPad from transferring apps to Mac?

    First off, let me state that I have iCloud backups of the most important app data and I don't care that I don't have backups for the rest. I don't care that those apps might one day be gone from the App Store. I don't care about saving my game data.
    What I do care about is that I shouldn't be forced to have a backup of ALL the apps on my iPad/iPhone on my MBP, which didn't come with unlimited storage space.
    I think at one point I was asked by iTunes if I wanted to transfer the purchases to my Mac (or if I wanted to backup the apps? I don't remember) and I foolishly clicked yes. Now, even though I have clicked on "Rest Warnings" multiple times, iTunes still insists on copying all the apps to my Mac without asking me if I wanna do it.
    I saw some suggestions about creating empty folders and stuff but I don't wanna do that. It just doesn't make sense that Apple doesn't give us an option to NOT backup our apps to our Macs. I thought the point of having an App Store where you can download your purchases anytime you want is so that you don't have to keep everything on your Mac.
    Any help is greatly appreciated!
    Keywords: how to stop iPad from copying apps to Mac, how to stop iPad from backing up apps to computer, how to prevent iPad from syncing apps

    I hope you get a solution soon. I've been having this problem too. I under any circumstances don't want my apps on my computer either, I remember when I bought my first iPad iTunes used to have an option to choose whether or not to sync/transfer apps but now its gone and it drives me crazy everytime I want to transfer music. I remember this option was Under the tab "Apps" in the device and under the Apps list. It was a check box like the ones that the Music and other library syncing currently have. But i havent seen it in a while. Apple really screwed this up.

  • HT4623 How do I delete from the apps store an updated app that was downloaded? Prior to iso7.2.0 once the app was downloaded it was automatically deleted from the updated section in the App Store.

    How do I delete from the apps store an updated app that was downloaded and shows open? Prior to iso7.2.0 once the app was downloaded it was automatically deleted from the App Store.

    You don't.
    The update section will continue to show recently updated apps due to the ability to auto download updates.
    It effects nothing, just ignore them.

  • How to call Servlet from jsp page and how to run this app using tomcat..?

    Hi ,
    I wanted to call servlet from jsp action i.e. on submit button of JSP call LoginServlet.Java file.
    Please tell me how to do this into jsp page..?
    Also i wanted to execute this application using tomcat.
    Please tell me how to do this...? what setting are required for this...? what will be url ..??
    Thanks.

    well....my problem is as follows:
    whenever i type...... http://localhost:8080/appName/
    i am getting 404 error.....it is not calling to login.jsp (default jsp)
    but when i type......http://localhost:8080/appName/login.do........it executes servlet properly.
    Basically this 'login.do' is form action (form action='/login.do').....and i wanted to execute this from login jsp only.(from submit button)
    In short can anyone please tell me how to diaplay jsp page using tomcat 5.5
    plz help me.

Maybe you are looking for

  • Two different itunes libraries on same computer

    I hope someone can help me! How do i set up two different libraries on the same computer for two different ipods? My ipod is on a computer i no longer have and I'm trying to put it on a computer that has a different ipod on it. My ipod appears in Win

  • Downloading new music & video

    HELP!!! I got my new ipod classic Saturday, but I had downloaded 3000 songs and videos when I noticed that the videos had not downloaded. When I reconnected to add my videos, it deleted all the music. Is there a way that I can keep all my music witho

  • In Regards to Flash Pro CS6, has this product been Discontinued?

    I cannot find it in the Adobe marketplace. Does the physical boxed product no longer exist or something?

  • Model II Architecture - The Flow?

    Ok in Model II Architecture, 1. What's the convention - always JSP's instantiate the PageBean or the Controller (through another class) instantiate PageBean or is it dependent on specific requirements? 2. I have first.jsp and upon submission I get se

  • Absence Approvals

    Hi gurus, There is a requirement where there are two business groups in a location. Both the business groups have self service HR Employee and Manager access. Business Group 1 - Only Manager approves the absences. But in business group 2 we want have