Monday, December 7, 2009

Beans and Form processing


Forms are a very common method of interactions in web sites.  JSP makes forms processing specially easy.
The standard way of handling forms in JSP is to define a "bean". This is not a full Java bean.  You just need to define a class that has a field corresponding to each field in the form.  The class fields must have "setters" that match the names of the form fields.  For instance, let us modify our GetName.html to also collect email
address and age.

The new version of GetName.html is

<HTML>
<BODY>
<FORM METHOD=POST ACTION="SaveName.jsp">
What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20><BR>
What's your e-mail address? <INPUT TYPE=TEXT NAME=email SIZE=20><BR>
What's your age? <INPUT TYPE=TEXT NAME=age SIZE=4>
<P><INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>

To collect this data, we define a Java class with fields "username", "email" and "age" and we provide setter methods "setUsername", "setEmail" and "setAge", as shown.  A "setter" method is just a method that starts with "set" followed by the name of the field.  The first character of the field name is upper-cased.
So if the field is "email", its "setter" method will be "setEmail".  Getter methods are defined similarly, with "get" instead of "set".   Note that the setters  (and getters) must be public.

package user;
public class UserData {

    String username;
    String email;
    int age;
 
    public void setUsername( String value )
    {
        username = value;
    }
 
    public void setEmail( String value )
    {
        email = value;
    }
 
    public void setAge( int value )
    {
        age = value;
    }
 
    public String getUsername() { return username; }
 
    public String getEmail() { return email; }
 
    public int getAge() { return age; }

}

The method names must be exactly as shown.  Once you have defined the class, compile it and make sure it is available in the web-server's classpath.  The server may also define special folders where you can
place bean classes, e.g. with Blazix you can place them in the "classes" folder.  If you have to change the classpath, the web-server would need to be stopped and restarted if it is already running.  Note that we are using the package name user, therefore the file UserData.class must be placed in a folder named
user under the classpath entry. Now let us change "SaveName.jsp" to use a bean to collect the
data.

<jsp:useBean id="user" class="user.UserData" scope="session"/>
<jsp:setProperty name="user" property="*"/>
<HTML>
<BODY>
<A HREF="NextPage.jsp">Continue</A>
</BODY>
</HTML>

All we need to do now is to add the jsp:useBean tag and the jsp:setProperty tag!  The useBean tag will look for an instance of the "user.UserData" in the session.  If the instance is already there, it will update the old instance.  Otherwise, it will create a new instance of user.UserData (the instance of the user.UserData is called a bean), and put it in the session. The setProperty tag will automatically collect the input data, match names against the bean method names, and place the data in the bean!

Let us modify NextPage.jsp to retrieve the data from bean..
<jsp:useBean id="user" class="user.UserData" scope="session"/> 
<HTML>
<BODY>
You entered<BR>
Name: <%= user.getUsername() %><BR>
Email: <%= user.getEmail() %><BR>
Age: <%= user.getAge() %><BR>
</BODY>
</HTML>

Notice that the same useBean tag is repeated.  The bean is available as the variable named "user" of class "user.UserData". The data entered by the user is all collected in the bean. We do not actually need the "SaveName.jsp", the target of GetName.html could have been NextPage.jsp, and the data would still be available the same way as long as we added a jsp:setProperty tag. But in the next tutorial, we will actually use SaveName.jsp as an error handler that automatically forwards the request to NextPage.jsp,
or asks the user to correct the erroneous data.

Exercise:  1)  Write a JSP/HTML set that allows a user to enter the name of a system property, and then displays the value returned by System.getProperty for that property name (handle errors appripriately.)
2)  Go back to the exercises where you manually modified boolean variables. Instead of a boolean variable, make these come from a HIDDEN form field that can be set to true or false.

Join my groups to share knowledge at
http://groups.google.com/group/rjetla?hl=en
http://rjetla.blogspot.com


Thanks & Regards


l¯¯l)¯¯)
l__l\__\aju
       l¯¯l
(¯¯(/__/etla

Tuesday, November 17, 2009

Configuring outlook for accessing Gmail

To configure Outlook 2007 for your Gmail address:

  1. Enable POP in your email account. Don't forget to click Save Changes when you're done.
  2. Open Outlook.
  3. Click the Tools menu, and select Account Settings...
  4. On the E-mail tab, click New...
  5. If you are prompted to Choose E-mail Service, select Microsoft Exchange, POP3, IMAP, or HTTP, and click Next.
  6. Fill in all necessary fields to include the following information:

    Your Name: Enter your name as you would like it to appear in the From: field of outgoing messages.
    Email Address: Enter your full Gmail email address (username@gmail.com). Google Apps users, enter your full address in the format username@your_domain.com.
    Password: Enter your email password.

    Manually configure server settings or additional server types: Leave this option unchecked if you want to automatically configure Outlook 2007. If you want to manually configure Outlook 2007, check this box now. Google Apps users should configure manually as follows.


  7. Click Next. If you are configuring Outlook 2007 automatically, you're done! Just click Finish.


  8. If you are configuring Outlook 2007 manually, select Internet E-mail and click Next.
  9. Verify your User Information, and enter the following additional information:

    Server Information

    Account Type: POP3

    Incoming mail server:
    pop.gmail.com (Google Apps users, enter the server names provided, don't add your domain name in these steps)

    Outgoing mail server (SMTP):
    smtp.gmail.com

    Logon Information

    User Name: Enter your Gmail username (including @gmail.com). Google Apps users, enter your full address in the format username@your_domain.com

    Password: Enter your email password.

    Require logon using Secure Password Authentication (SPA): Leave this option unchecked.


  10. Click the More Settings... button, and select the Outgoing Server tab.
  11. Check the box next to My outgoing server (SMTP) requires authentication and select Use same settings as my incoming mail server.


  12. Click the Advanced tab, and check the box next to This server requires an encrypted connection (SSL) under Incoming Server (POP3).
  13. In the Outgoing server (SMTP) box, enter 587, and select TLS from the drop-down menu next to Use the following type of encrypted connection:.


  14. Click OK.
  15. Click Test Account Settings... After receiving 'Congratulations! All tests completed successfully', click Close.
  16. Click Next, and then click Finish.

Congratulations! You're done configuring your client to send and retrieve Gmail messages.

Friday, November 13, 2009

Use Gmail IMAP in Microsoft Outlook 2007

We’ve all been hearing about the new IMAP support in Gmail, but how do we access that from Outlook? Those of you that have been waiting patiently will be happy to know all the instructions are on this page.
Using IMAP we can synchronize email across multiple devices, since the mail stays on the server. You already stopped reading this part and skipped down to the instructions, didn’t you?
Enable IMAP in Gmail Settings
Open your Settings panel in Gmail, and then click on the “Forwarding and POP/IMAP” tab.
image
Now click the Enable IMAP radio button, and click Save Changes.
image
Add Account to Outlook
Use the Tools menu you to open the Account Settings panel.
image
If this is your first account you’ll be prompted for the wizard, otherwise you’ll need to click the New button under the E-mail tab.
image
Now select the “Microsoft Exchange, POP3, IMAP or HTTP” option and hit next.
image
Select the checkbox for “Manually configure server settings” and everything on that page will gray out. Hit the next button again.
image
Now select “Internet E-mail”  (Didn’t we just do this? This wizard is taking too long…)
image
Finally we can enter some settings! Add in all your personal information here.
image
Note: If you are outside of the US you may need to use imap.googlemail.com and smtp.googlemail.com instead of imap.gmail.com and smtp.gmail.com.
Note: for Gmail Apps accounts, you’ll need to put in your full email address wherever you see an @gmail.com above. For instance, if your account was geek@howtogeek.com you would put that in the E-mail address field as well as the User Name field.
Click on the “More Settings” button and find the Outgoing Server tab, where you’ll need to check the box for “My outgoing server requires authentication”
image
Now choose the Advanced tab, and enter the following values (Very important)

  • Incoming Server: 993
  • Incoming Server encrypted connection: SSL
  • Outgoing Server: 587
  • Outgoing Server encrypted connection: TLS
image
Note: If you are having an issue with your sent email not showing up in Gmail, you can use the Folders tab, and then select the [Gmail] \ Sent Mail folder.
Note: Only do this step if your mail is not showing up, otherwise it will duplicate the sent mail.
image
Now that you’ve closed out that dialog, you can click the Test Account Settings button to make sure everything is going to work.
image
If all goes well, you should see a success message.
image
Note: Some ISPs restrict outgoing mail, so if you have a problem sending a test message you’ll need to consult with their documentation.
Accessing Your Mail
Now that everything is working you’ll notice a new set of folders for your Gmail account. (I’m using Gmail apps, which is why you don’t see an @gmail.com email address)
All of your labels will show up as separate folders, and the built-in “folders” in Gmail will be under the new [Gmail] folder.
 image
Outlook Flags = Gmail Stars
If you want to star a message in Gmail, you just need to flag it in Outlook. When I flagged this message from Mysticgeek…
image 
It shows up in Gmail as a starred message. (And it works the other way too)
image
Sending Email from Outlook
To send a new email from your Gmail account, just change the “Account” drop-down to your Gmail address before you send a message.
image
Note that if you only use Gmail, you won’t have this drop-down and it will default to your Gmail.
Final Thoughts
Here’s a few things to keep in mind:

  • Gmail Labels = Outlook Folders. To label a message, add it to a folder.
  • Gmail Stars = Outlook Flags.
  • Do Not move spam messages to the “Junk E-mail” folder in Outlook, move them to the [Gmail] \ Spam folder.
  • Outlook has a built-in Search Folder for “Large Mail”, which will let you easily find messages with large attachments.
Now that you can access your starred and labeled messages, you can start using Outlook’s excellent Search Folders to find messages more easily.



Raju Jetla

Recent Comments