Solved

java bean problem

Posted on 2004-03-20
10
366 Views
Last Modified: 2012-05-04
Hi there experts!!

I am trying to write a login bean for my site. I currently have:

package webtech;

import java.util.Properties;

public class loginbean
{
    private Properties userinfo = new Properties();

   public loginbean() {

     userinfo.put("danielbryant", "mullet1234");
     userinfo.put("mattcorcoran", "mullet1234");

   }

    public boolean checkUser(String inUserID, String inPassword) {

       if (inUserID == null)
          return false;
       if (inPassword == null)
          return false;


      if (userinfo.getProperty(inUserID) != null && userinfo.getProperty(inUserID).equals(inPassword))
         return true;

       return false;
      }
}

as my bean and my jsp looks like this:

<%@ page contentType="text/html;charset=windows-1252"%>
<jsp:useBean id="mybean" scope="session" class="webtech.loginbean" />
<%
    if (request.getParameter("Submit") != null) {
       if (mybean.checkUser(request.getParameter("Username"), request.getParameter("Password"))) {
          RequestDispatcher rd = getServletConfig().getServletContext().getRequestDispatcher("/main.jsp");
          rd.forward(request, response);
       }
    }
%>

I am using the following form actions on my page:

<form name="login" action="/index.jsp" method="post"></p>
Username:<br />
<input type="text" name="Username"><p />
Password:<br />
<input type="password" name="Password"><p />
<input type="submit" Value="Login" Name="Submit" onclick="validateForm(); if (send=='no') {return false}">
</form>

What i want this to do is check the values entered on the form against the usernames and passwords lited in the bean. If they are the same then they should be redirected to main.jsp. If they are different they should stay in index.jsp. This currently does not do this and due to my lack of experience with these i'm not too sure why. Can anyone help me with this at all? Any help would be greatly appreciated.

Many thanks
0
Comment
Question by:tommydowns
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10641496
this might not be the problem...but wont it be better to do this..
public boolean checkUser(String inUserID, String inPassword) {

       if (inUserID == null)
          return false;
       if (inPassword == null)
          return false;


      if (userinfo.getProperty(inUserID) != null && userinfo.getProperty(inUserID).equals (inPassword)) {
         return true;
      }
      else {
           return false;
      }
}

I would test with a print statment to see if this part works : something like this..
if (mybean.checkUser(request.getParameter("Username"), request.getParameter("Password"))) {
          out.println("User validated");
 }

if that works then I would check to see what's wrong with the jsp forward.
hope that helps,
Ghost
0
 

Author Comment

by:tommydowns
ID: 10641545
hi ghost,

thats all working now. How do i create a session on the username in index.jsp so i can call it from main.jsp??

many thanks for your help
0
 

Author Comment

by:tommydowns
ID: 10641548
also it still says

webtech/index.jsp in the address bar of the browser even when it has directed to main.jsp. Is there a reason for this and can i fix it?

many thanks
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10641554
>>How do i create a session on the username in index.jsp so i can call it from main.jsp??

umm why don't you just use the javaBean to get your username...Or am I just not understanding what you mean?

>>webtech/index.jsp in the address bar of the browser even when it has directed to main.jsp. Is there a reason for this and can i fix it?

if you use the <jsp:forward> tag...and you're still getting the wrong address in the address bar.. umm I have no idea.. that should not be happening.

hope that helps,
Ghost
0
 

Author Comment

by:tommydowns
ID: 10641596
Hi Ghost,

All i want to do is assign a session to the persons username so that when they are logged in it can retrieve there name and display a welcome not: e.g. if matthew logs in on index.jsp page then they get the greeting welcome matthew!!. This has to use a session though and be defined within the jsp.

I am using the method:

          RequestDispatcher rd = getServletConfig().getServletContext().getRequestDispatcher("/main.jsp");
          rd.forward(request, response);

to redirect...would that cause problems?

Many thanks for all your help!
0
 
LVL 13

Accepted Solution

by:
copyPasteGhost earned 150 total points
ID: 10641630
I don't think so..I know the <jsp:forward> tag that's the easiest one I think check out this:

http://java.sun.com/products/jsp/tags/11/syntaxref11.fm9.html

umm what you want can be done very easily using javabeans.

just noticed this...
>>> private Properties userinfo = new Properties();
why are you doing that?
public class loginbean
{
    private String userName = "";

   //you can just do something like this:(add to your bean)

   public String getUserName(){
      return userName;
   }

   //if you want to set the userName in your bean write this method.

   public void setUserName(String userName){
      this.userName = userName;
   }

public boolean checkUser(String inUserID, String inPassword) {

       if (inUserID == null)
          return false;
       if (inPassword == null)
          return false;


      if (userinfo.getProperty(inUserID) != null && userinfo.getProperty(inUserID).equals (inPassword)) {
        //SET THE USERNAME HERE LIKE
        //setUserName(userinfo.getProperty(inUserName))
         return true;
      }
      else {
           return false;
      }
}
}


you're going to have to set the username where you check to see if the id is right. to access this method in your bean you just do.

mybean.getUserName();

and that should work
Ghost
0
 

Author Comment

by:tommydowns
ID: 10641749
Hi ghost. How would i apply the jsp:forward tag to the jsp i have at the moment?

I have:

       if (mybean.checkUser(request.getParameter("Username"), request.getParameter("Password"))) {
          RequestDispatcher rd = getServletConfig().getServletContext().getRequestDispatcher("/main.jsp");
          rd.forward(request, response);

which redirects the user to main.jsp if the username and password are correct. Not sure og how i could implement another way?

0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10641775
<jsp:forward page="/main.jsp" />
that should do it.
0
 

Author Comment

by:tommydowns
ID: 10641809
The if statement is inside <%%> tags though so if you put <jsp:forward page="/main.jsp" /> that in it throws lots of errors. Is there a way around this?

Many thanks for all your help!!
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 10642392
<% if( .... ) {
    // code
%>
<jsp:forward .../>
<%
  // code continue
  }
%>


or just use below line inside <% %>
%><jsp:forward ... /><%
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
netstat -a in AIX unix 8 53
spring dependency injection using java config 2 91
error when compiling my project in eclipse luna 30 506
maven j2ee examles 2 64
The world seems to conceive of a curious bubble separating IT from “the business.”  More so than just about any other pursuit in the commercial world, people think of IT as some kind of an island.
Always backup Domain, SYSVOL etc.using processes according to Microsoft Best Practices. This is meant as a disaster recovery process for small environments that did not implement backup processes and did not run a secondary domain controller that ne…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question