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.

<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.

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.