Home arrow Java arrow Page 3 - What Can JSP Do For Me?
JAVA

What Can JSP Do For Me?


JDBC, EJB, Java IDL, JNDI, JTS, Servlets, the list goes on. There is limitless possibilities that arise when one is developing web applications using the JSP technology. Read more to get a glimpse at how to develop JSP pages using Tomcat.

Author Info:
By: Ben Shepherd
Rating: 3 stars3 stars3 stars3 stars3 stars / 17
July 28, 2003
TABLE OF CONTENTS:
  1. · What Can JSP Do For Me?
  2. · Getting Up to Speed with JSP
  3. · Simple JSP Files
  4. · Conclusion

print this article
SEARCH DEVARTICLES

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.


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- 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 ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials