Solved

java bean problem

Posted on 2004-03-20
10
356 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java.lang.UnsupportedClassVersionError 8 373
constuctor calling other constructor 2 74
unit test DAO layer 1 134
Books that can get me started on JAVA 2 81
Note: This is the second blog post in a series on email clearinghouses (https://www.xmatters.com/alert-management/blog-email-has-failed-us?utm_campaign=70138000000ydLoAAI&utm_source=exex&utm_medium=article&utm_content=blog-post).   Every month t…
A customer recently asked me about anti-malware and the different deployment options available for his business. Daily news about cyberattacks, zero-day vulnerabilities, and companies that suffered a security breach made him wonder if the endpoint a…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now