September 18, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Developing a Struts Application with the NetBeans IDE

  • June 20, 2010
  • By Anghel Leonard
  • Send Email »
  • More Articles »

Creating the Success.jsp Page

When the user has successfully logged in, you must redirect the application flow to the requested page. In this case, you render a success.jsp page, which is a very simple confirmation of the login credentials. Follow these steps:

  1. Create a new JSP page. In the Projects tab, expand the login project and right-click on the Web Pages folder. Select the New | JSP ... option from the contextual menu and type "success" in the File Name section of the New JSP File wizard. Press Finish.
  2. 2.
  3. Modify the generated code like below (remember to explore the NetBeans Palette, code completion for Struts tags and Struts Javadoc):
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    
    <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
    
    <!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>Login Successfully</title>
     </head>
     <body>
         <h1>Login Successfully Done</h1>
         <p>Email: <bean:write name="LoginActionForm" property="email" />.</p>
         <p>Password: <bean:write name="LoginActionForm" property="pass" />.</p>
     </body>
    </html>


Creating the Failure.jsp Page

When the provided credentials are wrong, you redirect the application flow to a page called failure.jsp. This is generated in the same manner as success.jsp and it looks like this:

<%@page contentType="text/html" pageEncoding="UTF-8"%>

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

<!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>Login Failure</title>
 </head>
 <body>
     <h1>Login Failure</h1>
     <p>Email: <bean:write name="LoginActionForm" property="email" />.</p>
     <p>Password: <bean:write name="LoginActionForm" property="pass" />.</p>
 </body>
</html>


Creating an ActionForm Bean

A Struts ActionForm bean is perfect for storing the provided credentials between requests (or any data provided through a form). Since you need to store the email and the password, which are two strings, your bean will be pretty short and simple (note that in Struts parlance, "an ActionForm is a JavaBean optionally associated with one or more ActionMappings. Such a bean will have had its properties initialized from the corresponding request parameters before the corresponding Action.execute method is called."). Here are the steps for creating it:

  1. In the Projects tab, right-click on the login project and select New | Other ... from the contextual menu.
  2. In the New File wizard's Categories list, select the Struts category, and from the File Types list, select the Struts ActionForm Bean type and press Next (see Figure 5).


  3. Click here for larger image

    Figure 5. Adding a Struts ActionForm Bean

  4. Now you have to type a name for the new action form. For example, type "LoginActionForm" in the Class Name section. Also, choose the com.myapp.struts package from the Package list (see Figure 6). Click Finish.



    Click here for larger image

    Figure 6. Configure Struts ActionForm Bean

  5. When you press the Finish button, NetBeans will generate the new action form for you and display it in the Source Editor. By default, this class provides it with a String called name and an int called number, along with getter/setter methods for them called delete all, because you need to add the email and the password! Also, if you check the struts-config.xml, you can see the new form bean added under the Form Beans section:
    <form-bean name="LoginActionForm" type="com.myapp.struts.LoginActionForm"/>


  6. In the LoginActionForm, add two properties like this:
    private String email;
    private String pass;


  7. Add getter/setter methods for these two properties. Go below this code and right-click in an empty spot. From the contextual menu, select Insert Code ... and Getter and Setter .... In the Generate Getters and Setters window, select the two properties and press Generate. Now, you should see the corresponding methods in your code.

For now, comment the validate method and read further. You will deal with this method in validation section.


Tags: Java EE, NetBeans, Struts



Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel