Java
  Home arrow Java arrow Page 3 - What Can JSP Do For Me?
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

What Can JSP Do For Me?
By: Ben Shepherd
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 13
    2003-07-28

    Table of Contents:
  • What Can JSP Do For Me?
  • Getting Up to Speed with JSP
  • Simple JSP Files
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    What Can JSP Do For Me? - Simple JSP Files


    (Page 3 of 4 )

    Getting the Destination From a Request  

    Inside C:\jakarta-tomcat-4.1.24\ C:\jakarta-tomcat-4.1.24\webapps\yadayada, create a JSP file index.jsp using your text editor.

    Listing 1 – A Simple JSP Page – index.jsp 

    <HTML>
    <HEAD>
    <TITLE>My First JSP Page</TITLE>
    </HEAD>
    <BODY>
    <CENTER>
    <FONT FACE=”Verdana” SIZE=3>
    <B>Requesting IP</B>
    <BR><BR>
    OK … let’s use some JSP to find out the IP address<BR> 
    that is making the request.<BR><BR>
    The IP address is <B><%= request.getRemoteAddr() %></B>
    </FONT>
    </CENTER>
    </BODY>
    </HTML>

    In listing 1, we notice that the code looks very much like any other server-side scripting technology. The thing to point out is that the dominant language here is HTML with embedded Java enclosed in <%, %> tags. The result is easily to predict. You should have the page loaded stating that the request is coming from the local host IP address, which is of course 127.0.0.1.

    This can be done using XML formatted tags as follows:

    <?XML VERSION="1.0" ENCODING="UTF-8"?>
    <! DOCTYPE root PUBLIC "-//Sun Microsystems Inc.//DTD JavaServer Pages Version 1.0//EN" "http://java.sun.com/products/jsp/dtd/jspcore_1_0.dtd">
    <jsp:root xmlns:jsp="http://java.sun.com/products/jsp/dtd/jsp_1_0.dtd">
    <HTML>
    <HEAD>
    <TITLE>My First JSP Page</TITLE>
    </HEAD>
    <BODY>
    <CENTER>
    <FONT FACE=”Verdana” SIZE=3>
    <B>Requesting IP</B>
    <BR><BR>
    OK … let’s use some JSP to find out the IP address<BR> 
    that is making the request.<BR><BR>
    The IP address is <B><jsp:expression> request.getRemoteAddr()</jsp:expression></B>
    </FONT>
    </CENTER>
    </BODY>
    </HTML>
    </jsp:root>

    Notice, that <jsp:root> is required to be added so that it is the topmost element and that all of the <%, %> tags are converted into their respected equivalents.

    But, I know you’d like to do more. So, let’s use JSP to do some neat work. If you are using Tomcat v4.x, you have anything you need (i.e. JavaMail 1.2 and JAF is included in package), otherwise, you may need to download Java’s latest-to-date JavaMail 1.3.1 from http://java.sun.com/products/javamail/, as well as the JavaBeans Activation Framework extension (i.e. JAF) from http://java.sun.com/beans/glasgow/jaf.html. Store the jar file in the /bin directory.  

    Sending eMails  

    It is time to send some mail. When sending mail we normally have 4 attributes. These are Sender, Receiver, Subject and the Message. So, inside C:\jakarta-tomcat-4.1.24\ C:\jakarta-tomcat-4.1.24\webapps\yadayada, create a HTML file index.html using your text editor. 

    Listing 2 – HTML Page with a Form to Send to a JSP – index.html 

    <HTML>
    <HEAD>
    <TITLE>Sending eMails with JSP</TITLE>
    </HEAD>
    <BODY>
    <CENTER>
    <B><FONT FACE="Verdana" SIZE="3">Time To Send An eMail Using JSP</FONT></B>
    <FONT FACE="Verdana" SIZE="2">
    <BR><BR>
    </FONT>
    <FORM ACTION="email.jsp" METHOD="post">
    <TABLE BORDER="1" WIDTH="350" CELLSPACING="1">
    <tr>
    <TD WIDTH="101"><FONT FACE="Verdana" SIZE="2">To</FONT></TD>
    <TD WIDTH="233"><FONT FACE="Verdana" SIZE="2"><INPUT TYPE="text" NAME="reciever" SIZE="32" TABINDEX="1"></FONT></TD>
    </tr>
    <tr>
    <TD WIDTH="101"><font face="Verdana" size="2">CC:</font></TD>
    <TD WIDTH="233"><FONT FACE="Verdana" SIZE="2"><INPUT TYPE="text" NAME="CCreciever" SIZE="32" TABINDEX="2"></FONT></TD>
    </tr>
    <TR>
    <TD WIDTH="101"><font face="Verdana" size="2">BCC:</font></TD>
    <TD WIDTH="233"><FONT FACE="Verdana" SIZE="2"><INPUT TYPE="text" NAME="BCCreciever" SIZE="32" TABINDEX="3"></FONT></TD>
    </TR>
    <TR>
    <TD WIDTH="101"><FONT FACE="Verdana" SIZE="2">From</FONT></TD>
    <TD WIDTH="233"><FONT FACE="Verdana" SIZE="2"><INPUT TYPE="text" NAME="sender" SIZE="32" TABINDEX="4"></FONT></TD>
    </TR>
    <TR>
    <TD WIDTH="101"><FONT FACE="Verdana" SIZE="2">Subject</FONT></TD>
    <TD WIDTH="233"><FONT FACE="Verdana" SIZE="2"><INPUT TYPE="text" NAME="subject" SIZE="32" TABINDEX="5"></FONT></TD>
    </TR>
    <TR>
    <TD WIDTH="101"><FONT FACE="Verdana" SIZE="2">Your Message</FONT></TD>
    <TD WIDTH="233"><FONT FACE="Verdana" SIZE="2"><TEXTAREA ROWS="4" NAME="message" COLS="27" TABINDEX="6"></TEXTAREA></FONT></TD>
    </TR>
    <TR>
    <TD COLSPAN="2" WIDTH="340">
    <P ALIGN="center"><FONT FACE="Verdana" SIZE="2"><INPUT TYPE="submit" VALUE="Submit" NAME="submit" TABINDEX="7"></FONT></TD>
    </TR>
    </TABLE>
    </FORM>
    </CENTER>
    </BODY>
    </HTML>

    This was the easy part. Everyone is familiar with using forms in HTML. Now, we will create a JSP to handle the form request. 

    Listing 3 – JSP File to Send Off an Email – email.jsp 

    <%@ page import="java.util.*, javax.mail.*, javax.mail.internet.*, javax.activation.*" %>
    <%
    // Required info
    String host = "mail.smtp.host";
    String server = "your.smtp.server";
    String userName = "user";
    String passWord = "password";
    String eMailAddress = "name@ispname.com.au";
    Properties props = new Properties();
    //To send eMails place your SMTP addresses here
    props.put(host, server);
    //For debugging purposes
    props.put("mail.debug", "true");
    Session sess = Session.getInstance(props);
    try{
    Transport trans = sess.getTransport("SMTP");
    trans.connect("your.smtp.server", userName, passWord);
    Message myMessage = new MimeMessage(sess);
    InternetAddress[] addresses = {new InternetAddress(eMailAddress)};
    String toAddresses = request.getParameter("reciever");
    myMessage.setRecipients(Message.RecipientType.TO, addresses);
    String ccAddresses = request.getParameter("CCreciever");
    myMessage.setRecipients(Message.RecipientType.CC, InternetAddress.parse(eMailAddress,false));
    String bccAddresses = request.getParameter("BCCreciever");
    myMessage.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(eMailAddress,false));
    //This is important so that your eMail can be sent correctly.
    String fromSender = request.getParameter("sender");
    myMessage.setFrom(new InternetAddress(fromSender));
    String subject = request.getParameter("subject");
    myMessage.setSubject(subject);
    String message = request.getParameter("message");
    myMessage.setText(message);
    myMessage.setSentDate(new Date());
    trans.sendMessage(myMessage, addresses);
    trans.close();}
    catch(MessagingException mExc){mExc.printStackTrace();}
    %>
    <html>
    <center>
    <font face="Verdana" size=2>The Message has been sent to the recipients.<br>
    A eMail has also been sent to your account.<br>
    Thank you for sending via <%= request.getParameter("sender")%><br>
    <a href="index.html">Click here to go back!</a></font>
    <center>
    </html>
     

    So you can see that JSP can solve a troublesome task like sending emails to a number of recipients quite swiftly using a subset of the Java API. I could have shown you how to send multipart emails using Java's setMultipartContent function so you could attach files to your email. Using other Java API, such as you may try storing JDBC API to connect to a database and store your emails sent and received.

    You may want to use JSP with Servlets and JavaBeans to handle other tough programming processes. Doing this, in this case, allows you to create a more sophisticated email application for your website.

    More Java Articles
    More By Ben Shepherd


     

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek