tags:

views:

64

answers:

2

I'm trying to realize a simple form:

there's a globalname and globalpass, and the User is asked to type in his name and pass.

If name == globalname and pass = globalpass -> "Hello name"
If name == blank or pass == blank -> "O NOEZ, you fergit sumpn!"
If name != globalname or pass != globalpass -> "WHUT? Who be you, intruder?"

So far I only manage to get the first if-working...

<% 
  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))
   {
%>
    <jsp:include page="formular-modul1.jsp" />
<% } else
   {
%>
<% }
%>

<%
 else if (name =="b" && pass =="c" && name.equals("b") && pass.equals("c"))
  {
%>
    <jsp:include page="formular-modul2.jsp" />
<%} else
    {
%>
<%
}%>

The name=="b" && pass =="c" was just for test purposes, which failed! Can somebody help me out, please?

+2  A: 

Strings are objects, not primitives. When using == to compare objects, it would only return true if both points to the same reference. It doesn't test the object's value. There you already have Object#equals() for.

Apart from this problem, using those old fashioned scriptlets in a JSP file is a sign of a bad practice. In this specific case you'd better use a Servlet class for this. Good luck.

BalusC
NoCanDo
It's hard to spot the error when you're using ugly scriptlets and you don't elaborate about the actual problem. "it aint working" gives us basically nothing to work with. I recommend to create a Java class which does the task and test it using main(). Or you can put a bit more effort in elaborating the problem and tell what actually is happening at code level (including exceptions/errors).
BalusC
Oh, don't forget to read the appserver logs. As you're using scriptlets, you won't see any exceptions with helpful debugging information at the page but just a blank nothing-saying page in case of failure. My cents on a `NullPointerException`.
BalusC
A: 

Got it working now.

<% 
  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{
  }
%>

Now I need to use .toLowercase.

Means name and pass have to be converted into .toLowercases THEN used. Anyone got any idea? Simply using newname = name.toLowercase(); or newpass = pass.toLowercase(); doesn't work!

NoCanDo
The `String` class has a `equalsIgnoreCase()` method. Guess what it does :)
BalusC
By the way, I really won't consider "PaSsWoRd" and "password" as equal.
BalusC
Neither would I. this .jsp doesn't consider "password" and "PaSsWoRd" equal. If globalpassword is password, then PaSsWoRd returns "Incorrect Userdata!"
NoCanDo
`String#toLowercase()` yields a new string. Because Strings are immutable. It should work ok if you assgn the `.toLowercase()` to a new variable.In your code above, you use `|` operators which are bitwise OR and do NOT shortcut the conditional evaluation. What you are looking for is the `||` logical OR operator.
rsp
Then what's the point of doing `pass.toLowercase()` ? Just don't do that. By the way, if you still have problems, you really, really need to elaborate "doesn't work!" in *more* detail.
BalusC
Because I want and need to. BalusC
NoCanDo
The point I want to use .toLowerCase is because folks my type in WebEng, but the globalname is webeng. So I need the typed in "WebEng" transformed into "webeng" in order for it to be correct. The point WHY HOW doesn't matter. I just want and need it to.
NoCanDo