?
Solved

Multiple buttons in login.jsp

Posted on 2009-12-30
13
Medium Priority
?
1,042 Views
Last Modified: 2013-11-24
Hey all,
  I've been trying to learn struts, installed netbeans with their excellent tutorials, was able to create a simple login application. But now I need to add a Sign Up/Register button along with login button. Tried adding a href link to my register.jsp but does not work, please help.

Thanks guys.
0
Comment
Question by:shakeer_m
13 Comments
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26151692
<<Tried adding a href link to my register.jsp>>
You need to give more details, as to what is the code that you have written, what is the issue you are facing for that.

for signup
1) create a action class to handle the signup actions, SignupAction
2) make an entry in the struts-config.xml
3) invoke the action class from the JSP by

in JSP
<a href="#"  onclick='doSignUp()' >

Javascript
function doSignUp()
{
   document.forms[0].action = "SignupAction.do";
   document.forms[0].submit;
}

4) SignupAction should then forward the control to the register.jsp

Since you are learning Struts, you should try to use the controller (MVC), and not try to invoke a JSP from a JSP.

Thanks
0
 
LVL 3

Expert Comment

by:Nithyanandhan_T
ID: 26151695
I'm not sure why you need a hyperlink, when you have button...

Create your buttons like this..
<html:button property="dispatch"  onclick="submitMyForm()"> Submit </html:button>
<html:button property="dispatch"  onclick="register()"> Sign Up/Register </html:button>


and javascript methods like this...

submitMyForm(){
  document.forms[0].action= "/yourContextPath/actionPathForSubmit";
  document.forms[0].submit();
}

register(){
  document.forms[0].action= "/yourContextPath/actionPathForRegistert";
  document.forms[0].submit();
}


let me know how it helps!!
0
 

Author Comment

by:shakeer_m
ID: 26151791
Thanks for your valuable comments, attached below is with your suggestions please let know whats not right

login.jsp below

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Intro Page</title>
        <link rel="stylesheet" type="text/css" href="stylesheet.css">
    </head>
    <body>
        <h1>Welcome</h1>
        <html:form action="/login">
            <table width="354" height="158" border="0">
                <tbody>
                    <tr>
                        <td colspan="2">
                            <bean:write name="LoginForm" property="error" filter="false"/>
                            &nbsp;</td>
                    </tr>
                    <tr>
                        <td width="95">User ID : </td>
                        <td width="249"><html:text property="userID" /></td>
                    </tr>
                    <tr>
                        <td>Password : </td>
                        <td><html:text property="password"/></td>
                    </tr>
                    <tr>
                    </tr>
                </tbody>
            </table>
            <p>
                <input type="submit" value="Submit">
            </p>
            <a link href="WEB-INF/register.jsp" onclick='register()' >Click to register</a>
            <script type="text/javascript">
                function register()
                {
                    document.forms[0].action = "RegisterAction";
                    document.forms[0].submit;
                }
            </script>
        </html:form>
    </body>
</html>

from strtuts-config
    <action-mappings>
        <action name="LoginForm" path="/login" scope="request" type="com.myapp.struts.LoginAction" validate="false">
            <forward name="success" path="/WEB-INF/success.jsp"/>
            <forward name="failure" path="/login.jsp"/>
        </action>
        <action name="RegisterForm" path="/register" scope="request" type="com.myapp.struts.RegisterAction" validate="false">
            <forward name="success" path="/WEB-INF/registerSuccess.jsp"/>
            <forward name="failure" path="/WEB-INF/register.jsp"/>
        </action>
            </action-mappings>


And Gurvinder whats the better way to handle this using MVC? dont want to struggle if there is a better way

Thanks.


0
Technology Partners: 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 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26151802
<<<a link href="WEB-INF/register.jsp" onclick='register()' >Click to register</a>>>

you should not provide jsp link in the href
<a link href="#" onclick='register()' >Click to register</a>
0
 
LVL 40

Accepted Solution

by:
Gurvinder Pal Singh earned 2000 total points
ID: 26151806
<<And Gurvinder whats the better way to handle this using MVC? dont want to struggle if there is a better way>>
As far as i know, this navigation should happen through action class only, the way other expert and myself has suggested.

0
 

Author Comment

by:shakeer_m
ID: 26151848
changed to <a link href="#" onclick='register()' >Click to register</a>
but link does not work, attached are LoginAction and RegisterAction, please review.
Thanks.
LoginAction.java below
package com.myapp.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class LoginAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";
 
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        // extract user data
        LoginForm formBean = (LoginForm) form;
        String userID = formBean.getUserID();
        String password = formBean.getPassword();
    

// perform validation
        if ((userID == null) || // name parameter does not exist
                password == null || // email parameter does not exist
                userID.equals("")) { // name parameter is empty
            //        password.indexOf("@") == -1)
            // email lacks '@'
            formBean.setError();
            return mapping.findForward(FAILURE);
        }
          return mapping.findForward(SUCCESS);
    }
}




RegisterAction.java below
package com.myapp.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class RegisterAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {


        // extract user data
        RegisterForm formBean = (RegisterForm) form;
        String firstName = formBean.getFirstName();
        String password = formBean.getPassword();

// perform validation
        if ((firstName == null) || // name parameter does not exist
                password == null || // email parameter does not exist
                firstName.equals("")) { // name parameter is empty
            //        password.indexOf("@") == -1)
            // email lacks '@'
            formBean.setError();
            return mapping.findForward(FAILURE);
        }


        return mapping.findForward(SUCCESS);
    }
}

Open in new window

0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26151871
can you try giving

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>

<html:link page="/success.do">Call the Success page</html:link>
0
 

Author Comment

by:shakeer_m
ID: 26151896
Did not work,
org.apache.jasper.JasperException: /login.jsp(8,55) Attempt to redefine the prefix html to /WEB-INF/struts-html.tld, when it was already defined as http://struts.apache.org/tags-html in the current scope.

removed the taglib still the link does not work.
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26151903
there is no need to copy
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
in your code

if you already have
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
in the original code that you wrote
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 26153213
<<<<<action name="RegisterForm" path="/register" scope="request"
 
check that !!
0
 

Author Comment

by:shakeer_m
ID: 26176815
struts mapping for Login is as attached below and hence used the same format for register page, also tried running with change like this,

        <action name="RegisterAction" path="/register" scope="request" type="com.myapp.struts.RegisterAction" validate="false">

still does not work, thanks for all the valuable comments, please guide me to the correct way of adding another button to a jsp page that forwards to a different page which will take in all the registration information.

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">


<struts-config>
    <form-beans>
        <form-bean name="RegisterForm" type="com.myapp.struts.RegisterForm"/>
        <form-bean name="LoginForm" type="com.myapp.struts.LoginForm"/>
    
    </form-beans>
    
    <global-exceptions>
    
    </global-exceptions>


    <action-mappings>
        <action name="LoginForm" path="/login" scope="request" type="com.myapp.struts.LoginAction" validate="false">
            <forward name="success" path="/WEB-INF/success.jsp"/>
            <forward name="failure" path="/login.jsp"/>
        </action>
        <action name="RegisterForm" path="/register" scope="request" type="com.myapp.struts.RegisterAction" validate="false">
            <forward name="success" path="/WEB-INF/registerSuccess.jsp"/>
            <forward name="failure" path="/WEB-INF/register.jsp"/>
        </action>
        
    </action-mappings>
    
    <controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>

    <message-resources parameter="com/myapp/struts/ApplicationResource"/>
    
    <plug-in className="org.apache.struts.tiles.TilesPlugin" >
        <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
        <set-property property="moduleAware" value="true" />
    </plug-in>
    
    <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
        <set-property
            property="pathnames"
            value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
    </plug-in>
  
</struts-config>

Open in new window

0
 

Author Comment

by:shakeer_m
ID: 26177027
From a comment earlier, I'm guessing the correct way is to use the LoginAction class to forward to the page, adding a third mapping along with 'success' and 'failure' as 'register' if the button is clicked, is this right?
0
 
LVL 40

Assisted Solution

by:Gurvinder Pal Singh
Gurvinder Pal Singh earned 2000 total points
ID: 26191350
For each button, you should be having a separate action class.
Best practice/convention is:
For navigation to a different page: prefix the Action class with 'View', like ViewRegisterAction,
For submitting data, like login, prefix the Action class with 'Submit', like SubmitLoginAction

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

What You Need to Know when Searching for a Webhost Provider
A solution for Fortify Path Manipulation.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month15 days, 16 hours left to enroll

850 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