0

Trying to write some code which checks if something has been entered in a form in the JSP, and if it hasn't, assigns it the value from the session.

Once this has been done an object is made from all of the parameters.

Written all the code but the

User user = new User(username, firstname, surname, password); comes up with a cannot **find symbol error** 

I realise that this is because it isn't in any of the ifs. Will anyone show me how I can change the code so that it can use these values?

The code:

  try {
                    User sessionuser =(User)session.getAttribute("User");
                    String username = sessionuser.getUsername();

                    if (request.getParameter("firstname").equals (null)){
                        String firstname = sessionuser.getFirstname();
                    }
                    else{String firstname = request.getParameter("firstname");
                    }

                    if (request.getParameter("surname").equals (null)){
                        String surname = sessionuser.getSurname();
                    }
                    else{String surname = request.getParameter("surname");
                    }
                    String password = request.getParameter("password");

                    User user = new User(username, firstname, surname, password);
                    //this.updateUser(user); 
                    this.updateUser(username, firstname, surname, password);

                    //user.updateUser();

                    session.setAttribute("User", user);
                    request.setAttribute("u", user);
                    request.getRequestDispatcher("UpdateUser.jsp").forward(request,     response);
0

7 Answers 7

8

A quick hint

if (request.getParameter("firstname").equals (null)){ 

...

if (request.getParameter("surname").equals (null)){

...

This can never be true, it can be false or throw NullPointerException. Use == instead of equals()

Sign up to request clarification or add additional context in comments.

Comments

3

Lets take a snippet of your code and tell you why you are getting the error

if (request.getParameter("firstname").equals (null)){
  String firstname = sessionuser.getFirstname();
}

In here, you are instantiating the string firstname in the if statement, when the condition is true, the string is created but is only accessible inside the if statement.

What you should do is

String firstname = sessionuser.getFirstname();
if (request.getParameter("firstname") != null){
  firstname = request.getParameter("firstname");
}

Comments

2

You are defining the variables firstname and surname in the if/else construct, which is prevented by the compiler. In this case, yes, it will be defined, but in other cases it might not, and the compiler doesn't spend too much time on static code analysis, it will prevent such behaviour without further investigating.

Instead of defining the variable in if/else, use a default, in this case you can use:

String surname = sessionuser.getSurname();

if (request.getParameter("surname") != null){
  surname = request.getParameter("surname");
}

Note that I switched the conditions a bit.

Also, your if will never be true, it will either be false or throw NullPointerException. Use request.getParameter("surname") == null instead.

Comments

2

You need to declare your variables outside the blocks in the if/else chains.

Use something like:

String surname = request.getParameter("surname");
if (surname == null) {
  surname = sessionuser.getSurname();
}

Repeat for the other items.

Local variables are only available in the block in which you define them (and its sub-blocks). The way you're doing it, your creating variables inside the if and else blocks that are discarded (cannot be referred to) at all outside those blocks.

If you need to test if these parameters are either null or empty strings, do the if checks like this:

if ((surname == null) || ("".equals(surname))) { ... }

Don't use == to compare strings, that's not reliable - it compares references, not the string contents.

Comments

1

Alternatively, you can save yourself bit of typing and get rid of IF-ELSE blocks. You can use method like StringUtils.defaultString.

http://commons.apache.org/lang/api-2.5/org/apache/commons/lang/StringUtils.html#defaultString(java.lang.String,%20java.lang.String)

One line for one field will do:

String firstName = StringUtils.defaultString(request.getParameter("firstname"), sessionuser.getFirstname())

Do the same for other fields.

This function basically replaces your IF-ELSE block. As a bonus, you won't make a mistake declaring variable inside IF-ELSE block :)

Comments

-1

You could use string variables and then check to see if it is NULL or not.

String test = request.getParameter("test");

if (username.equals(""))
{
    //todo
}

1 Comment

In the todo section you should write the code for what you are intending to do
-3

Should be like this I think

 try {
                User sessionuser =(User)session.getAttribute("User");
                String username = sessionuser.getUsername(),firstname="",surname="",password="";

                if (request.getParameter("firstname") == ""){
                    firstname = sessionuser.getFirstname();
                }
                else{firstname = request.getParameter("firstname");
                }

                if (request.getParameter("surname") == ""){
                    surname = sessionuser.getSurname();
                }
                else{surname = request.getParameter("surname");
                }
                password = request.getParameter("password");

                User user = new User(username, firstname, surname, password);
                //this.updateUser(user); 
                this.updateUser(username, firstname, surname, password);

                //user.updateUser();

                session.setAttribute("User", user);
                request.setAttribute("u", user);
                request.getRequestDispatcher("UpdateUser.jsp").forward(request,     response);

You can also use null instead of ""

5 Comments

Thanks alot. We fixed it; you showed me I was being a retard haha!
@Jimmy: be warned, that code may not be doing what you think it does. If you expect session.getAttibute("...") to return nulls, this code isn't checking for that. If you're expecting empty strings, this code doesn't either. Use equals for string comparisons, and == null for null checks/
Well Jimmy the only difference between null and empty string is that Empty String takes memory and there "" is stored. Anyways,I had also mention to use null if you want. Upto you.
If's like request.getParameter("firstname") == "" will never be true. If you want to test equality you need to use "".equals(request.getParameter("firstname")). Note that doesn't handle the case when firstname is null.
The way I want it to work is so when something isn't entered in the "username" row of a form it will set the value of "username" to the username stored in the session if that makes sense?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.