Solved

java bean problem

Posted on 2004-03-20
10
360 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
method inner class 6 82
Help with LDAp login using jsp 6 113
java.util.concurrent.locks in java 3 191
memory representation of int[][][] a=new int[2][][]; 10 60
One of the biggest threats in the cyber realm pertains to advanced persistent threats (APTs). This paper is a compare and contrast of Russian and Chinese APT's.
Knowing where your website is hosted is as important as the features you receive, the monthly fee, and the support you receive. Due diligence should be done when choosing your next hosting provider.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

776 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