views:

224

answers:

4

I've got an index.jsp with

[snip]

<% 
  String name = request.getParameter("name");
  String pass = request.getParameter("pass");
  String globalname = "webeng";
  String globalpass = "2009";
  if (name !=null && pass!=null && name.equals(globalname) && pass.equals(globalpass))
   {
   %>
    <hr />
    <p><b>Howdy, <%= request.getParameter("name") %></b></p>
    <hr />
<% }
  else if (name !=null | pass!=null && name.equals("") | pass.equals(""))
  {
  %>
    <hr />
    <p><b>Ooops, one or more fields are empty. Please fill everything out!!</b></p>
    <hr />
<% }
  else if (name !=null | pass!=null && !name.equals(globalname) | !pass.equals(globalpass))
  {
  %>
    <hr />
    <p><b>Incorrect Userdata!</b></p>
    <hr />
<% }
  else{
  }
%>

[snip]

Now, the globalname for example is in lowercase "webeng". Folks may type in "WebEng", "webENG", "WEBENG" and variations thereof.

I need those typed in Strings converted to lowercase. Somehow

String newname = name.toLowerCase();
String newpass = pass.toLowerCase();

is not working. Anybody got any idea?

This is what Eclipse tells me when I use

<% 
      String name = request.getParameter("name");
      String pass = request.getParameter("pass");
      String globalname = "webeng";
      String globalpass = "2009";
      String newname = name.toLowerCase();
      String newpass = pass.toLowerCase();

       if (name !=null && pass!=null && name.equals(globalname) && pass.equals(globalpass))
       {
       %>
        <hr />
        <p><b>Howdy, <%= request.getParameter("name") %></b></p>
        <hr />
    <% }
      else if (name !=null | pass!=null && name.equals("") | pass.equals(""))
      {
      %>
        <hr />
        <p><b>One or more fields are empty!</b></p>
        <hr />
    <% }
      else if (name !=null && pass!=null && !name.equals(globalname) | !pass.equals(globalpass))
      {
      %>
        <hr />
        <p><b>Incorrect Userdata!</b></p>
        <hr />
    <% }
      else{
      }
    %>

Eclipse: http://i.imagehost.org/0277/2009-11-15%5F19%5F34%5F00.png

A: 

You can use compareToIgnoreCase and you don't have to do the conversion yourself.

unholysampler
I have to use toLowerCase().
NoCanDo
How about `equalsIgnoreCase()`? It does perfectly what you want.
BalusC
Again, I have to use toLowerCase()
NoCanDo
"I have to use" makes no sense unless it's homework. If so, you should have tagged the question with "homework" as well.
BalusC
No, its not homework. I just don't want to use new functions without even getting the old ones working properly. That would defeat the purpose of teaching myself how it works.Really, is it that hard to answer a simple question without trowing hundreds of different new functions into the pot? Can't people just simple answer a question?
NoCanDo
Well, I used equalsIgnoreCase() insteat of .equals(), still I haven't been able to get .toLowerCase() working. Long story short: I have learnd a new function equalsIgnoreCase() and how it works, but haven't learned anything how .toLowerCase() works.
NoCanDo
Friend, toLowerCase wasnt working because your name object is null, so you are calling a method on a null object. On the other hand equalsIgnoreCase if used like this globalname.equalsIgnoreCase(name) it will NOT through Null exception even if name is null. Because in this case you are passing a null object, and not using it.For learning Java, always dig inside, If you are using Eclipse or Netbeans, just hold Ctrl and click the method name, it will take you to its source.And you will see that equalsIgnoreCase is actually using toLowerCase in it
medopal
It seems what you don't understand is how method calls work on objects. .getParameter() returns null if the parameter doesn't exist. This means that when you try to call .toLower() on name, it has the potential to be null and thus throw the exception you posted in the screen shot. Read thru BalusC's answer and both of the edits and you should see how to stop the null exception.
unholysampler
+4  A: 

Your code logic is odd. Scriptlets also doesn't make testing more easy. Here's an SSCCE in flavor of a real Java class to kickoff and ease testing:

public class Test {

    public static void main(String[] args) throws Exception {
        String user = "WeBeNg"; // Change this as if it is user input.
        String pass = "2009"; // Change this as if it is user input.

        String expectedUser = "webeng";
        String expectedPass = "2009";

        if (user == null || pass == null || user.isEmpty() || pass.isEmpty()) {
            System.out.println("Please enter both username and password.");
        } else if (user.equalsIgnoreCase(expectedUser) && pass.equals(expectedPass)) {
            System.out.println("Welcome " + user);
        } else {
            System.out.println("Unknown login.");
        }
    }

}

Hope this helps.

Edit #1: please do not post screenshots. Copypaste the exception and stacktrace in code blocks. The exception is by the way not coming from Eclipse. It's coming from Tomcat. Also, the exception in question (NullPointerException) is fairly self-explaining. You accessed an object reference which is actually null.

SomeObject someObject = null;
someObject.doSomething(); // Fails with NPE.
someObject = new SomeObject();
someObject.doSomething(); // Succes.

You need to do a null-check first, e.g.

if (someObject != null) {
    someObject.doSomething(); // Succes.
}

Edit #2 I also recommend you to learn about operators, operator precedence and expression grouping in Java. See, the following isn't "logical"

if (name !=null | pass!=null && name.equals("") | pass.equals(""))

Here's a good tutorial to start with to learn about this: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html Good luck.

BalusC
Notice the subtle difference between this and what you are doing. You need to check for null before you use any string method on name. In your code you try to call toLowerCase() before you ensure name is not null.
unholysampler
A: 

You seem to use | in stead of || . Is that intentional?

extraneon
Yes, because if I use: (name ==null || pass==null || name.equals("") || pass.equals("")), I'll get the <p><b>Ooops, one or more fields are empty. Please fill everything out!!</b></p> immadiately when I load the page, that's not the intention.I want it to appear AFTER I left 1 or both fields empty and AFTER I pressed the "send" button.
NoCanDo
A: 

Here it is with equalsIgnoreCase. Unless you really don't want to shortcut the or with the | operator it's generally safer to go with ||. I also prefer not to fall through to success as in BalusC's answer, there is something unsettling about the default condition being to "login" the user.

<% 
String name = request.getParameter("name");
String pass = request.getParameter("pass");
String globalname = "webeng";
String globalpass = "2009";
if (name ==null || pass==null || name.equals("") || pass.equals("")) {
// You can use StringUtils.isBlank here instead if you have it available to you
 %>
<hr />
 <p><b>Ooops, one or more fields are empty. Please fill everything out!!</b></p>
 <hr />
<% } else if (name.equalsIgnoreCase(globalname) && pass.equalsIgnoreCase(globalpass))
 {
 %>
  <hr />
   <p><b>Howdy, <%= request.getParameter("name") %></b></p>
  <hr />
<% } else {
 %>
  <hr />
   <p><b>Incorrect Userdata!</b></p>
  <hr />
 }
%>
StevenWilkins
`java.lang.String` has an `isEmpty()` method. If you want to cover spaces as well, then do `name.trim().isEmpty()`.
BalusC
isEmpty was introduced in Java 6, since i think many people (including me) are still using 5, you should mention when its a new method.
medopal