Saturday, March 18, 2006

Newbies guide to Servlets.

I better kiss ASP.NET and bang the heck out of servlets. Agreed, servlets are easy to program to a certain extent, but when it is for presentation oriented applications they are terrible to work with. They just mess up your code so much that you begin to hate it yourself.
Anyway, for those who doesnt know servlets - heres a sample servlet and steps.

1. Download Apache Tomcat Server(the best for servlets) and install it.

2. In the installation folder you can see " webapps " folder which is the root folder for your server. You copy your applications into this folder.

3. Write your servlet and compile it. Take a look at sample servlet.
import javax.servlet.http.*;
import java.io.*;
public class SampleServlet extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse resp) throws Exception
{
doPost(req,resp); //call post method handler
}

public void doPost(HttpServletRequest req,HttpServletResponse resp) throws Exception
{
PrintWriter out=resp.getWriter();//obtain the stream to the output - to the browser
//we need to render html content as shown below.doesnt this suck???
out.println("
<html><head><title>Sample Servlet</title></headgt;<body>");
out.println("Bhargava's blog rocks!!!</body></html>");
}
}

How to compile??
Nothing different, but make sure you add path to the Servlet-api.jar which is present in the common/lib folder of Apach tomcat installation folder. How do you do that? well figure it out yourself. otherwise mail me.

4. Now that the servlet is ready, compile it and keep the .class file aside. Now Create a folder, say TestServlet and create a WEB-INF folder inside this folder. Inside the WEB-INF Folder, create a "classes" folder and copy your servlet class into the file. I shall give you a web.xml which is to be placed inside the WEB-INF Folder. Copy the TestServlet folder to webapps directory of the Tomcat installation folder. The following is the directory structure you should get as a result.
Apache installation path.....\webapps |-TestServlet
|-WEB-INF (-> web.xml is also present in web-inf)
|-classes
|-SampleServlet.class

5. Actually the better way is to create a .war archive. Create WEB-INF and place web.xml in it. Also classes directory with SampleServlet .class inside it. Go to command line and navigate to the folder which has this WEB-INF and type the following
jar cvf TestServlet .war WEB-INF
You can just copy the generated war archive into the webapps folder of Tomcat and restart Tomcat.
6. The web.xml is the configuration file that tells the container what servlets are available and other information(i dont know !! ) create the web.xml as it is. Try to understand what it says.

<?xml version="1.0" encoding="ISO-8859-1"?>


<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

<!-- SERVLETS INFORMATION STARTS HERE -->
<!-- sample servlet for testing -->
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>TestServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/TestServlet</url-pattern>
</servlet-mapping>

<!-- end sample servlet-->
</web-app>


Notice the value. It says when ever SampleServlet is called from browser , invoke the servlet whose name is SampleServlet whcih inturn loads the servlet whose class is mapped to this servlet, throuh

Assuming apache runs on localhost, and that you have deployed your servlet(by the way the web.xml is called Deployment Descriptor) you should type the following URL on browser

http://localhost/TestServlet/SampleServlet



You should get
Bhargav 's blog rocks!!


In case you dont get the output and get a 404 Error(resource not found) please verify if you have created the directory as i mentioned and most importantly the web.xml is correct or not. I once got stuck up with this problem and no where on the internet you can find solution for the problem!(not now cos i posted it on my blog! isnt my blog informative?) The problem was that i did not specify the schema to follow in the element inside web.xml, that is even the following portion is very important.



<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">



I hope this brief tutorial gives a kick start for those interested in learning servlets. Actually there is more of servlets than what i just covered(what i told is simply about Deployment in Tomcat, nothing else)? Just google out and if you have any questions , feel free to contact me at krishnabhargav@yahoo.com. I am not bad at J2EE, though not as good as .NET. Anyway i can help you out.

Next on this servlets issue, i would post a tutorial on Forms based authentication using Servlets and Apache Tomcat container. Again there is no proper step-by-step tutorial for this online, so meanwhile please go through some sample servlets code. All the best. And please hate servlets :)) cos compared to servlets Asp.NEt is veryyyyy easy to work with.


Krish.

Wednesday, March 15, 2006

Long ago ..... System.Web.Mail

Hi all,
Its been quite a while since i posted on my blog. I was terribly busy(emotionally disturbed, so took a two week break and i did nothing but "eat,sleep,watch movie,freak with friends"). Anyway the following is the steps you need to do, to send email from .NET application using System.Web.Mail.
1. Add reference to System.Web.dll if it its not already added(for asp.net applications, its already added, so you can skip this)
2. Import the namespace System.Web.Mail
3. Customize the following code
'a) Prepare your message
Dim msg as new MailMessage
msg.To = "to@email.com"
msg.From = "from@email.com"
msg.Subject = "Your subject goes here"
msg.Body = "Body of email"
'b) Send your message
SmtpMail.SmtpServer="smtpserveraddress"
SmtpMail.Send(msg) 'sending the mail.

As you notice, its pretty simple to work with!! But it isnt as simple as it appears. Just for information, Web.Mail uses CDOSYS internally to communicate with SMTP server and this is considered bad(i dont have enough experience to tell why, but its bad from what i read) .
Also another question is "What if my SMTP Server needs authentication?" Well this question has haunted me for two years and have now found out the answer. Just check out this cool support topic from MSDN.
http://support.microsoft.com/default.aspx?scid=kb;en-us;555287

Well there are sooo many issues with System.Web.Mail that there is a dedicated site for the namespace !!! check out www.systemwebmail.com !!! Cool

Also for your information, for .NET 2.0 use System.Net.Mail namespace - which actually makes sense. That is all for now and i have another problem to figure out. I noticed that Query Strings end when they encounter "#" I mean if actual string is Pa=1&Pg=2&Name=C#&Value=CS,when you read that string inside the asp.net applicaiton using "Request.QueryString", all you can see is that its only Pa=1&Pg=2&Name=C and #&Value=CS is skipped!!! Any solution for this?? Comment on my blog.