Solved

java bean problem

Posted on 2004-03-20
10
365 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
  • 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Starting your own business is always a daunting process, and for most people it is brand new experience. Avoid the common pitfalls by following these tips to start on the road to success.
Many businesses neglect disaster recovery and treat it as an after-thought. I can tell you first hand that data will be lost, hard drives die, servers will be hacked, and careless (or malicious) employees can ruin your data.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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