Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By jim829

Thread: Get a value from an address and save it in a variable

  1. #1
    STG
    STG is offline Member
    Join Date
    Mar 2014
    Posts
    2
    Rep Power
    0

    Default Get a value from an address and save it in a variable

    Hello,
    My name is STG and I'm new to the forum.
    I just started using java because i want to create a simple web service that will take some values from within a url and save those values in variables.
    To be more specific:
    For my project I use the Spring Tool Suite.
    I want to be able to enter a URL in a browser.. something like "localhost:8080/test?name=Root". When I hit Enter a page will be displayed showing "Hello Root" if the name in the URL is Root or "Hello User" for any other name.
    I accomplish that with this code:

    Java Code:
     
    String UserName = "Empty";
    
    @RequestMapping("/test")
    public @ResponseBody String username(@RequestParam(value="name")String name){
    	if (name == "Root")
    		UserName = "Hello" + name;
        else
                UserName = "Hello User";
    
    	return UserName;
    }
    My problem is that it if I enter Root as a name in the URL it doesn't recognize it and it shows "Hello User". I tried
    Java Code:
    << if (name.toString() == "Root")
    but no luck.
    Can anyone help me out with this and explain me how exactly I should reform this code in order to make it work?
    Thanks in advance!!!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,423
    Rep Power
    5

    Default Re: Get a value from an address and save it in a variable

    Don't compare strings with ==. Use equals().

    Regards,
    Jim
    STG likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    STG
    STG is offline Member
    Join Date
    Mar 2014
    Posts
    2
    Rep Power
    0

    Default Re: Get a value from an address and save it in a variable

    Quote Originally Posted by jim829 View Post
    Don't compare strings with ==. Use equals().

    Regards,
    Jim
    Thanks a lot Jim!!! It worked perfectly.

Similar Threads

  1. Replies: 2
    Last Post: 11-02-2013, 08:53 AM
  2. Replies: 2
    Last Post: 01-09-2013, 08:10 PM
  3. knowing the address of any variable or function in java
    By sharod roy choudhury in forum New To Java
    Replies: 3
    Last Post: 12-27-2012, 08:56 PM
  4. Dynamic variable name based on other variable
    By nadissen in forum Eclipse
    Replies: 4
    Last Post: 05-06-2011, 06:22 PM
  5. How can i save the data Internally(auto save)
    By Rama Koti Reddy in forum AWT / Swing
    Replies: 2
    Last Post: 11-01-2010, 08:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •