Avatar of tommydowns
tommydowns

asked on 

java bean problem

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
JSP

Avatar of undefined
Last Comment
kennethxu
Avatar of copyPasteGhost
copyPasteGhost
Flag of Canada image

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
Avatar of tommydowns
tommydowns

ASKER

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
Avatar of tommydowns
tommydowns

ASKER

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
>>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
Avatar of tommydowns
tommydowns

ASKER

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!
ASKER CERTIFIED SOLUTION
Avatar of copyPasteGhost
copyPasteGhost
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of tommydowns
tommydowns

ASKER

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?

<jsp:forward page="/main.jsp" />
that should do it.
Avatar of tommydowns
tommydowns

ASKER

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!!
Avatar of kennethxu
kennethxu

<% if( .... ) {
    // code
%>
<jsp:forward .../>
<%
  // code continue
  }
%>


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

JavaServer Pages (JSP) allow the development of dynamically generated web pages. It uses the Java programming language; JSP pages are translated into servlets at runtime, with each servlet being cached and reused until the JSP is modified. JSP allows Java code to be interleaved with static web markup content, so the resulting page can be compiled and executed on the server to deliver the content.

16K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo