<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>javAbout.com &#187; Java Tutorials</title>
	<atom:link href="http://www.javabout.com/category/javatutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javabout.com</link>
	<description>All About For Java</description>
	<lastBuildDate>Tue, 14 Jul 2009 07:41:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Introduction to Java Servlets with Eclipse (Part4)</title>
		<link>http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part4/</link>
		<comments>http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part4/#comments</comments>
		<pubDate>Tue, 10 Feb 2009 11:48:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java Tutorials]]></category>
		<category><![CDATA[Introduction to Java Servlets with Eclipse]]></category>
		<category><![CDATA[Introduction to Java Servlets with Eclipse (Part3)]]></category>
		<category><![CDATA[Introduction to Java Servlets with Eclipse (Part4)]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[java about]]></category>
		<category><![CDATA[java servlet]]></category>
		<category><![CDATA[java servlet examples]]></category>
		<category><![CDATA[Java Servlets with Eclipse]]></category>
		<category><![CDATA[javabout]]></category>
		<category><![CDATA[part3]]></category>
		<category><![CDATA[part4]]></category>
		<category><![CDATA[servlet examples]]></category>

		<guid isPermaLink="false">http://www.javabout.com/?p=100</guid>
		<description><![CDATA[

(Part1) &#8211; (Part2)  -  (Part3) &#8211; Part4
Implementation of Java Servlet
Back to our Eclipse and modify your index.jsp to something like below.




 &#60;form action="GreetingServlet" method="POST"&#62;
 First Name: &#60;input type="text" name="firstName" size="20"&#62;&#60;br&#62;
 Surname: &#60;input type="text" name="surname" size="20"&#62;
 &#60;br&#62;&#60;br&#62;
 &#60;input type="submit" value="Submit"&#62;
 &#60;/form&#62;




It means that every time we execute the Submit button in our JSP, it will  call GreetingServlet that we created earlier. However, now, our Java Servlet  actually do nothing. We need to modify our GreetingServlet.java as well.
This GreetingServlet.java contains our implementation of the Java Servlet. If  you carefully pay attention to this file, you ...]]></description>
			<content:encoded><![CDATA[
<!-- google_ad_section_start -->
<p><a href="http://www.javabout.com/2009/02/09/introduction-to-java-servlets-with-eclipse-part1" target="_self">(Part1)</a> &#8211; <a href=" http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part2" target="_self">(Part2) </a> -  <a href="http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part3" target="_self">(Part3)</a> &#8211; <strong>Part4</strong></p>
<p><strong>Implementation of Java Servlet</strong></p>
<p>Back to our Eclipse and modify your index.jsp to something like below.</p>
<div class="java">
<table border="0" cellspacing="0" cellpadding="3" bgcolor="#ffffff">
<tbody>
<tr><!-- start source code --></p>
<td align="left" valign="top"><code><span style="color: #ffffff;"> </span><span style="color: #000000;">&lt;form action=</span><span style="color: #2a00ff;">"GreetingServlet" </span><span style="color: #000000;"><a class="tfTextLink" style="border-bottom: 1px solid #991616; display: inline; font-size: 1em; padding-bottom: 1px; color: #991616; line-height: 1em; background-color: transparent; text-decoration: underline;" href="javascript:void(0)">method</a>=</span><span style="color: #2a00ff;">"POST"</span><span style="color: #000000;">&gt;</span><br />
<span style="color: #ffffff;"> </span><span style="color: #000000;">First Name: &lt;input type=</span><span style="color: #2a00ff;">"text" </span><span style="color: #000000;">name=</span><span style="color: #2a00ff;">"firstName" </span><span style="color: #000000;">size=</span><span style="color: #2a00ff;">"20"</span><span style="color: #000000;">&gt;&lt;br&gt;</span><br />
<span style="color: #ffffff;"> </span><span style="color: #000000;">Surname: &lt;input type=</span><span style="color: #2a00ff;">"text" </span><span style="color: #000000;">name=</span><span style="color: #2a00ff;">"surname" </span><span style="color: #000000;">size=</span><span style="color: #2a00ff;">"20"</span><span style="color: #000000;">&gt;</span><br />
<span style="color: #ffffff;"> </span><span style="color: #000000;">&lt;br&gt;&lt;br&gt;</span><br />
<span style="color: #ffffff;"> </span><span style="color: #000000;">&lt;input type=</span><span style="color: #2a00ff;">"submit" </span><span style="color: #000000;">value=</span><span style="color: #2a00ff;">"Submit"</span><span style="color: #000000;">&gt;</span><br />
<span style="color: #ffffff;"> </span><span style="color: #000000;">&lt;/form&gt;</span></code></td>
<p><!-- end source code --></tr>
</tbody>
</table>
</div>
<p><!-- =       END of automatically generated HTML code       = --><!-- ======================================================== -->It means that every time we execute the Submit button in our JSP, it will  call GreetingServlet that we created earlier. However, now, our Java Servlet  actually do nothing. We need to modify our GreetingServlet.java as well.</p>
<p>This GreetingServlet.java contains our implementation of the Java Servlet. If  you carefully pay attention to this file, you should realize that this class  extends HttpServlet. So wherever you found Java classes that extends  HttpServlet, those classes must definitely be a Java Servlet and there must be  an entry in web.xml.</p>
<p>There are few Java methods created by default in GreetingServlet.java i.e.</p>
<div class="java">
<table border="0" cellspacing="0" cellpadding="3" bgcolor="#ffffff">
<tbody>
<tr><!-- start source code --></p>
<td align="left" valign="top"><code><span style="color: #ffffff;"> </span><span style="color: #7f0055;"><strong>protected </strong></span><span style="color: #7f0055;"><strong>void </strong></span><span style="color: #000000;">doGet</span><span style="color: #000000;">(</span><span style="color: #000000;">HttpServletRequest request, HttpServletResponse response</span><span style="color: #000000;">)</span><br />
<span style="color: #ffffff;"> </span><span style="color: #7f0055;"><strong>protected </strong></span><span style="color: #7f0055;"><strong>void </strong></span><span style="color: #000000;">doPost</span><span style="color: #000000;">(</span><span style="color: #000000;">HttpServletRequest request, HttpServletResponse response</span><span style="color: #000000;">)</span></code></td>
<p><!-- end source code --></tr>
</tbody>
</table>
</div>
<p><!-- =       END of automatically generated HTML code       = --><!-- ======================================================== -->and many more.</p>
<p>Let’s focus on the doGet and doPost methods as these two methods are the most  important. Do you remember that in our &lt;form&gt;&lt;/form&gt; tag, we  actually specify the method attribute as POST? For your information, we can also  set the method in the &lt;form&gt;&lt;/form&gt; tag into GET. If we specify it  as POST, the doPost method will be called. On the other hand, if we specify the  &lt;form&gt; tag as GET, doGet method will be executed. Then, what would be the  difference between GET and POST?</p>
<p>Each method has its own advantages and disadvantages. GET has limited length  for the information that is submitted but it is easily appended on the last URL  of your Java Servlet. POST does not have any limitation of the length of  information sent and it is hidden from the URL. For example, GET will display  something like  http://localhost:7001/MyFirstServlet/GreetingServlet?firstName=david&amp;surname=test  while POST would not display firstName and surname in the end of the URL. POST  has URL like “http://localhost:7001/MyFirstServlet/GreetingServlet”. It is  obvious that GET may have some issues on security as everyone may clearly see  the information sent to the Java Servlet. POST would be a better choice but  sometimes, there would just be a situation where we need to use GET to make our  development easier. I am sure that as you go along, you will meet a situation  where you need to use GET for sending some information to Servlet.</p>
<p>As our JSP used POST, let’s add some Java codes into our doPost method. Add  these two lines into your doPost method so it should be like below.</p>
<div class="java">
<table border="0" cellspacing="0" cellpadding="3" bgcolor="#ffffff">
<tbody>
<tr><!-- start source code --></p>
<td align="left" valign="top"><code><span style="color: #ffffff;"> </span><span style="color: #7f0055;"><strong>protected </strong></span><span style="color: #7f0055;"><strong>void </strong></span><span style="color: #000000;">doPost</span><span style="color: #000000;">(</span><span style="color: #000000;">HttpServletRequest request, HttpServletResponse response</span><span style="color: #000000;">)</span><br />
<span style="color: #ffffff;"> </span><span style="color: #7f0055;"><strong>throws </strong></span><span style="color: #000000;">ServletException, IOException </span><span style="color: #000000;">{ </span><br />
<span style="color: #ffffff;"> </span><span style="color: #000000;">String firstName = request.getParameter</span><span style="color: #000000;">(</span><span style="color: #2a00ff;">"firstName"</span><span style="color: #000000;">)</span><span style="color: #000000;">.toString</span><span style="color: #000000;">()</span><span style="color: #000000;">;</span><br />
<span style="color: #ffffff;"> </span><span style="color: #000000;">System.out.println</span><span style="color: #000000;">(</span><span style="color: #2a00ff;">"firstName = " </span><span style="color: #000000;">+ firstName</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span><br />
<span style="color: #ffffff;"> </span><span style="color: #000000;">}</span></code></p>
<p><span style="color: #ffffff;"> </span><span style="color: #000000;">String firstName = request.getParameter</span><span style="color: #000000;">(</span><span style="color: #2a00ff;">&#8220;firstName&#8221;</span><span style="color: #000000;">)</span><span style="color: #000000;">.toString</span><span style="color: #000000;">()</span><span style="color: #000000;">;</span></td>
<p><!-- end source code --></tr>
</tbody>
</table>
</div>
<p><!-- =       END of automatically generated HTML code       = --><!-- ======================================================== -->What does above code means? If you remember, the textbox that we created in  our JSP contains the name attribute.</p>
<div class="java">
<table border="0" cellspacing="0" cellpadding="3" bgcolor="#ffffff">
<tbody>
<tr><!-- start source code --></p>
<td align="left" valign="top"><code><span style="color: #ffffff;"> </span><span style="color: #000000;">&lt;input type=</span><span style="color: #2a00ff;">"text" </span><span style="color: #000000;">name=</span><span style="color: #2a00ff;">"firstName" </span><span style="color: #000000;">size=</span><span style="color: #2a00ff;">"20"</span><span style="color: #000000;">&gt;</span></code></td>
<p><!-- end source code --></tr>
</tbody>
</table>
</div>
<p><!-- =       END of automatically generated HTML code       = --><!-- ======================================================== -->This is where the name attribute is useful. We can get the value entered by  the user in the index.jsp by executing request.getParameter(“firstName”).  Remember that firstName is case sensitive so firstName is different with  FirstName or firstname. The next line of code is used to print out the value  into your Tomcat console.</p>
<div class="java">
<table border="0" cellspacing="0" cellpadding="3" bgcolor="#ffffff">
<tbody>
<tr><!-- start source code --></p>
<td align="left" valign="top"><code><span style="color: #ffffff;"> </span><span style="color: #000000;">System.out.println</span><span style="color: #000000;">(</span><span style="color: #2a00ff;">"firstName = " </span><span style="color: #000000;">+ firstName</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span></code></td>
<p><!-- end source code --></tr>
</tbody>
</table>
</div>
<p><!-- =       END of automatically generated HTML code       = --><!-- ======================================================== -->Okay, let’s see how the index.jsp works with our incomplete Java Servlet now.  Right-click your Dynamic Web Project, choose Export and choose WAR file and keep  the Destination and Web Module same as before (remember to tick the Overwrite  existing file in the final step of your WAR export wizard) then press Finish  button. Then pay attention to your Tomcat carefully. Remember that Tomcat  AUTOMATICALL re-deploy your application if it found a new WAR file is located in  its webapps folder.</p>
<p>After few seconds, Tomcat will detect that the WAR file has changed and it  automatically re-deploy it as shown on illustration below.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image061.png"><img class="size-full wp-image-101 aligncenter" title="image061" src="http://www.javabout.com/wp-content/uploads/2009/02/image061.png" alt="image061" width="550" height="273" /></a></p>
<p>Now, try to go to your browser and go to <a href="http://localhost:8080/MyFirstServlet">http://localhost:8080/MyFirstServlet</a> again, provide your first name and surname and try to press the Submit button  and see the Tomcat console on what’s happening.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image063.png"><img class="size-full wp-image-102 aligncenter" title="image063" src="http://www.javabout.com/wp-content/uploads/2009/02/image063.png" alt="image063" width="550" height="334" /></a></p>
<p>After pressing Submit button, it goes to blank page. This is happening because  we have not specified the successful page.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image065.png"><img class="size-full wp-image-103 aligncenter" title="image065" src="http://www.javabout.com/wp-content/uploads/2009/02/image065.png" alt="image065" width="550" height="334" /></a></p>
<p>And the Tomcat console has something interesting to look at.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image067.png"><img class="size-full wp-image-104 aligncenter" title="image067" src="http://www.javabout.com/wp-content/uploads/2009/02/image067.png" alt="image067" width="550" height="273" /></a></p>
<p>It displays “firstName = David”. It means that our doPost implementation of  our Java Servlet has been successfully executed. Try to play around with it and  provide with few different first names and pay attention to your Tomcat  console.</p>
<p>Now, our Java Servlet has been proven to execute successfully but it has not  greeted the user properly. After pressing Submit button, it goes to blank page.  We need to modify it to go to success page. Ok, let’s get back to our  Eclipse.</p>
<p>Please modify your doPost method in your GreetingServlet.java into below  codes.</p>
<div class="java">
<table border="0" cellspacing="0" cellpadding="3" bgcolor="#ffffff">
<tbody>
<tr><!-- start source code --></p>
<td align="left" valign="top"><code><span style="color: #ffffff;"> </span><span style="color: #000000;">response.setContentType</span><span style="color: #000000;">(</span><span style="color: #2a00ff;">"text/html;charset=UTF-8"</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span><br />
<span style="color: #ffffff;"> </span><span style="color: #000000;">PrintWriter out = response.getWriter</span><span style="color: #000000;">()</span><span style="color: #000000;">;</span><br />
<span style="color: #ffffff;"> </span><span style="color: #000000;">String firstName = request.getParameter</span><span style="color: #000000;">(</span><span style="color: #2a00ff;">"firstName"</span><span style="color: #000000;">)</span><span style="color: #000000;">.toString</span><span style="color: #000000;">()</span><span style="color: #000000;">;</span><br />
<span style="color: #ffffff;"> </span><span style="color: #000000;">String surname = request.getParameter</span><span style="color: #000000;">(</span><span style="color: #2a00ff;">"surname"</span><span style="color: #000000;">)</span><span style="color: #000000;">.toString</span><span style="color: #000000;">()</span><span style="color: #000000;">;</span></code></p>
<p><span style="color: #ffffff;"> </span><span style="color: #000000;">out.println</span><span style="color: #000000;">(</span><span style="color: #2a00ff;">&#8220;&lt;html&gt;&#8221;</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span><br />
<span style="color: #ffffff;"> </span><span style="color: #000000;">out.println</span><span style="color: #000000;">(</span><span style="color: #2a00ff;">&#8220;&lt;head&gt;&#8221;</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span><br />
<span style="color: #ffffff;"> </span><span style="color: #000000;">out.println</span><span style="color: #000000;">(</span><span style="color: #2a00ff;">&#8220;&lt;title&gt;Servlet GreetingServlet&lt;/title&gt;&#8221;</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span><br />
<span style="color: #ffffff;"> </span><span style="color: #000000;">out.println</span><span style="color: #000000;">(</span><span style="color: #2a00ff;">&#8220;&lt;/head&gt;&#8221;</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span><br />
<span style="color: #ffffff;"> </span><span style="color: #000000;">out.println</span><span style="color: #000000;">(</span><span style="color: #2a00ff;">&#8220;&lt;body&gt;&#8221;</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span><br />
<span style="color: #ffffff;"> </span><span style="color: #000000;">out.println</span><span style="color: #000000;">(</span><span style="color: #2a00ff;">&#8220;&lt;h1&gt;Servlet GreetingServlet at &#8221; </span><span style="color: #000000;">+ request.getContextPath </span><span style="color: #000000;">() </span><span style="color: #000000;">+ </span><span style="color: #2a00ff;">&#8220;&lt;/h1&gt;&#8221;</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span><br />
<span style="color: #ffffff;"> </span><span style="color: #000000;">out.println</span><span style="color: #000000;">(</span><span style="color: #2a00ff;">&#8220;&lt;p&gt;Welcome &#8221; </span><span style="color: #000000;">+ firstName + </span><span style="color: #2a00ff;">&#8220; &#8221; </span><span style="color: #000000;">+ surname + </span><span style="color: #2a00ff;">&#8220;&lt;/p&gt;&#8221;</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span><br />
<span style="color: #ffffff;"> </span><span style="color: #000000;">out.println</span><span style="color: #000000;">(</span><span style="color: #2a00ff;">&#8220;&lt;/body&gt;&#8221;</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span><br />
<span style="color: #ffffff;"> </span><span style="color: #000000;">out.println</span><span style="color: #000000;">(</span><span style="color: #2a00ff;">&#8220;&lt;/html&gt;&#8221;</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span></p>
<p><span style="color: #ffffff;"> </span><span style="color: #000000;">out.close</span><span style="color: #000000;">()</span><span style="color: #000000;">;</span></td>
<p><!-- end source code --></tr>
</tbody>
</table>
</div>
<p><!-- =       END of automatically generated HTML code       = --><!-- ======================================================== -->Let’s see on what the codes do.</p>
<div class="java">
<table border="0" cellspacing="0" cellpadding="3" bgcolor="#ffffff">
<tbody>
<tr><!-- start source code --></p>
<td align="left" valign="top"><code><span style="color: #ffffff;"> </span><span style="color: #000000;">String firstName = request.getParameter</span><span style="color: #000000;">(</span><span style="color: #2a00ff;">"firstName"</span><span style="color: #000000;">)</span><span style="color: #000000;">.toString</span><span style="color: #000000;">()</span><span style="color: #000000;">;</span><br />
<span style="color: #ffffff;"> </span><span style="color: #000000;">String surname = request.getParameter</span><span style="color: #000000;">(</span><span style="color: #2a00ff;">"surname"</span><span style="color: #000000;">)</span><span style="color: #000000;">.toString</span><span style="color: #000000;">()</span><span style="color: #000000;">;</span></code></td>
<p><!-- end source code --></tr>
</tbody>
</table>
</div>
<p><!-- =       END of automatically generated HTML code       = --><!-- ======================================================== -->The two lines above are used to get the firstname and the lastname from our  JSP.</p>
<p>Then, these two variables are used and displayed into JSP and located in  below codes. The out.println(“”); is used to render the HTML into JSP page.</p>
<div class="java">
<table border="0" cellspacing="0" cellpadding="3" bgcolor="#ffffff">
<tbody>
<tr><!-- start source code --></p>
<td align="left" valign="top"><code><span style="color: #ffffff;"> </span><span style="color: #000000;">out.println</span><span style="color: #000000;">(</span><span style="color: #2a00ff;">"&lt;html&gt;"</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span><br />
<span style="color: #ffffff;"> </span><span style="color: #000000;">out.println</span><span style="color: #000000;">(</span><span style="color: #2a00ff;">"&lt;head&gt;"</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span><br />
<span style="color: #ffffff;"> </span><span style="color: #000000;">out.println</span><span style="color: #000000;">(</span><span style="color: #2a00ff;">"&lt;title&gt;Servlet GreetingServlet&lt;/title&gt;"</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span><br />
<span style="color: #ffffff;"> </span><span style="color: #000000;">out.println</span><span style="color: #000000;">(</span><span style="color: #2a00ff;">"&lt;/head&gt;"</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span><br />
<span style="color: #ffffff;"> </span><span style="color: #000000;">out.println</span><span style="color: #000000;">(</span><span style="color: #2a00ff;">"&lt;body&gt;"</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span><br />
<span style="color: #ffffff;"> </span><span style="color: #000000;">out.println</span><span style="color: #000000;">(</span><span style="color: #2a00ff;">"&lt;h1&gt;Servlet GreetingServlet at " </span><span style="color: #000000;">+ request.getContextPath </span><span style="color: #000000;">() </span><span style="color: #000000;">+ </span><span style="color: #2a00ff;">"&lt;/h1&gt;"</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span><br />
<span style="color: #ffffff;"> </span><span style="color: #000000;">out.println</span><span style="color: #000000;">(</span><span style="color: #2a00ff;">"&lt;p&gt;Welcome " </span><span style="color: #000000;">+ firstName + </span><span style="color: #2a00ff;">" " </span><span style="color: #000000;">+ surname + </span><span style="color: #2a00ff;">"&lt;/p&gt;"</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span><br />
<span style="color: #ffffff;"> </span><span style="color: #000000;">out.println</span><span style="color: #000000;">(</span><span style="color: #2a00ff;">"&lt;/body&gt;"</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span><br />
<span style="color: #ffffff;"> </span><span style="color: #000000;">out.println</span><span style="color: #000000;">(</span><span style="color: #2a00ff;">"&lt;/html&gt;"</span><span style="color: #000000;">)</span><span style="color: #000000;">;</span></code></td>
<p><!-- end source code --></tr>
</tbody>
</table>
</div>
<p><!-- =       END of automatically generated HTML code       = --><!-- ======================================================== -->You need to re-deploy your Dynamic Web Project into Tomcat (by exporting it  into WAR file) and refresh your Internet Browser.</p>
<p>However, there is a little problem. PrintWriter may be red-underlined by  Eclipse as we have not imported the necessary packages for it. Red-underlined  means that it is syntax-error and we should fix it before exporting it into WAR  file for deployment. Do not be afraid, click on the symbol on the left side of  the codes and choose “Import PrintWriter (java.io)”. Yes, IDE is supposed to  help your programming easier and fun. If you develop it in less sophisticated  IDE such as Notepad, you need to figure out by yourself on what import statement  that you need to insert for PrintWriter class.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image069.png"><img class="size-full wp-image-105 aligncenter" title="image069" src="http://www.javabout.com/wp-content/uploads/2009/02/image069.png" alt="image069" width="550" height="334" /></a></p>
<p>Okay, please re-deploy your Dynamic Web Project by exporting it into WAR file  again as described in earlier discussion. Wait until Tomcat console has shown  that it has been re-deployed, refresh your browser and try to press Submit  button again and see what’s happening now.</p>
<p>My final screen looks like</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image071.png"><img class="size-full wp-image-107 aligncenter" title="image071" src="http://www.javabout.com/wp-content/uploads/2009/02/image071.png" alt="image071" width="550" height="334" /></a></p>
<p>Congratulations, you have successfully created your Java Servlet.</p>
<p><strong>How to Shutdown Tomcat Manually</strong></p>
<p>Shutting down Tomcat is simple. You can double click on the shutdown.bat in  your Tomcat’s bin folder. It will open another “closing” console and after few  seconds, both consoles will be disappeared and you should not be able to try to  access <a href="http://localhost:8080/">http://localhost:8080</a> anymore.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image073.png"><img class="size-full wp-image-106 aligncenter" title="image073" src="http://www.javabout.com/wp-content/uploads/2009/02/image073.png" alt="image073" width="550" height="431" /></a></p>
<p><strong>Conclusion</strong></p>
<p>Well, I am sure that you now have been able to create a Servlet using Eclipse  3.1.2 with the help of Web Tools Platform (WTP) plugin. By reading this  tutorial, you should be able to send information to Java Servlet, know how to  get this information from your Java Servlet and redirect the user to the success  page. However, you should practice more and more as your skills will be improved  along your experiences. There are still a lot of things in Java world that you  need to know such as Filter, Listener, Struts, EJB and etc. If you grab the  concept, you are good to go to the next level. I wish you all the best luck</p>
<p>You can find the NetBeans project for the tutorial source codes <a href="http://www.java-tips.org/images/stories/tutorials/servlet-eclipse/MyFirstServlet.zip">here</a>.<br />
The  README file for the sources is available <a href="http://www.java-tips.org/images/stories/tutorials/servlet-eclipse/README.txt" target="_blank">here</a>.</p>
<p><strong>Related Posts</strong><a href="http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part2" target="_self"><br />
- Introduction to Java Servlets with Eclipse (Part1)<br />
</a><a href="http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part3" target="_self">- Introduction to Java Servlets with Eclipse (Part2)<br />
</a><a href="http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part3" target="_self">- Introduction to Java Servlets with Eclipse (Part3)</a><a href="http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part2" target="_self"><br />
</a></p>

<!-- google_ad_section_end -->
]]></content:encoded>
			<wfw:commentRss>http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introduction to Java Servlets with Eclipse (Part3)</title>
		<link>http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part3/</link>
		<comments>http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part3/#comments</comments>
		<pubDate>Tue, 10 Feb 2009 11:31:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java Tutorials]]></category>
		<category><![CDATA[Introduction to Java Servlets with Eclipse]]></category>
		<category><![CDATA[Introduction to Java Servlets with Eclipse (Part3)]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[java about]]></category>
		<category><![CDATA[java servlet]]></category>
		<category><![CDATA[java servlet examples]]></category>
		<category><![CDATA[Java Servlets with Eclipse]]></category>
		<category><![CDATA[javabout]]></category>
		<category><![CDATA[part3]]></category>
		<category><![CDATA[servlet examples]]></category>

		<guid isPermaLink="false">http://www.javabout.com/?p=86</guid>
		<description><![CDATA[

(Part1) &#8211; (Part2)  &#8211; Part3 &#8211; (Part4)
How to Start Tomcat Manually
If you have successfully configured Tomcat, you should have a bin folder of  Tomcat. Remember that you need to download the Tomcat from Apache website in the  form of zip file. Do not download the .exe file as it would be good if you have  a manual hands-on experience on this. It should help you to understand how  Tomcat works.

I have extracted my Tomcat into C:\tomcat-5.5.16. Thus, I should have a bin  folder that ...]]></description>
			<content:encoded><![CDATA[
<!-- google_ad_section_start -->
<p><a href="http://www.javabout.com/2009/02/09/introduction-to-java-servlets-with-eclipse-part1" target="_self">(Part1)</a> &#8211; <a href=" http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part2" target="_self">(Part2) </a> &#8211; <strong>Part3</strong> &#8211; <a href="http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part4" target="_self">(Part4)</a></p>
<p><strong>How to Start Tomcat Manually</strong></p>
<p>If you have successfully configured Tomcat, you should have a bin folder of  Tomcat. Remember that you need to download the Tomcat from Apache website in the  form of zip file. Do not download the .exe file as it would be good if you have  a manual hands-on experience on this. It should help you to understand how  Tomcat works.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image039.png"><img class="size-full wp-image-87 aligncenter" title="image039" src="http://www.javabout.com/wp-content/uploads/2009/02/image039.png" alt="image039" width="528" height="391" /></a></p>
<p>I have extracted my Tomcat into C:\tomcat-5.5.16. Thus, I should have a bin  folder that is C:\tomcat-5.5.16\bin.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image041.png"><img class="size-full wp-image-88 aligncenter" title="image041" src="http://www.javabout.com/wp-content/uploads/2009/02/image041.png" alt="image041" width="550" height="383" /></a></p>
<p>If you pay attention carefully, you would see there is startup.bat and  shutdown.bat. These two files are used to start Tomcat and shutdown Tomcat  respectively.</p>
<p>Double click on startup.bat, a new Tomcat console should open. Watch the  console carefully and you will see something interesting.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image043.png"><img class="size-full wp-image-89 aligncenter" title="image043" src="http://www.javabout.com/wp-content/uploads/2009/02/image043.png" alt="image043" width="550" height="272" /></a></p>
<p>You should see that it displays “Deploying web application archive  MyFirstServlet.war”. Tomcat always checks its webapps folder whether is there  any WAR file. If so, it will extract it and deploy it to its container so that  we can run it after Tomcat started. Tomcat will be started if you have seen the  message “Server startup in N ms”. If you do not have any WAR file, the above  message should not be displayed.</p>
<p>Now, open your browser and go to <a href="http://localhost:8080/">http://localhost:8080</a>.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image045.png"><img class="size-full wp-image-90 aligncenter" title="image045" src="http://www.javabout.com/wp-content/uploads/2009/02/image045.png" alt="image045" width="550" height="334" /></a></p>
<p>This means that we have started Tomcat correctly. As we have already deployed  our MyFirstServlet, execute it by http://localhost:8080/[yourwebappname] so it  would be <a href="http://localhost:8080/MyFirstServlet">http://localhost:8080/MyFirstServlet</a>.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image047.png"><img class="size-full wp-image-91 aligncenter" title="image047" src="http://www.javabout.com/wp-content/uploads/2009/02/image047.png" alt="image047" width="550" height="334" /></a></p>
<p>Awesome! Try to press Submit button, it should do nothing as we have not  specified any action in our &lt;form&gt;&lt;/form&gt; tag. Next, we need to  modify our index.jsp to call our Java Servlet to greet the user and to redirect  the user to the success page.</p>
<p>Please remember not to shutdown the Tomcat by clicking on the close window on  the Tomcat console. Please use the shutdown.bat instead as it helps Tomcat to  gradually shutting down itself.</p>
<p>Now, let’s create our Java Servlet. You can right click on the src folder  (this is your source folder) and choose New and Others. A wizard will be  prompted to you to configure your Java Servlet.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image049.png"><img class="size-full wp-image-92 aligncenter" title="image049" src="http://www.javabout.com/wp-content/uploads/2009/02/image049.png" alt="image049" width="550" height="564" /></a></p>
<p>Go to the Web, choose Servlet and press Next button.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image051.png"><img class="size-full wp-image-93 aligncenter" title="image051" src="http://www.javabout.com/wp-content/uploads/2009/02/image051.png" alt="image051" width="500" height="500" /></a></p>
<p>In this wizard, you may define the package for your Java Servlet. Normally, we  use Package to group a few Java files that has the same functionalities. For  example, if I have StringUtil.java for String manipulation and  EncodingAlgorithmUtil.java for encoding, I will locate them under the same  package called “com.mycompanyname.util” as they both are actually utility  classes. So as this is our first Java Servlet, let’s create one package called  “com.mycompany.servlet”. The class name is basically your Java Servlet name. To  make it self-explanatory, I name it GreetingServlet. You can leave other  configuration as they are.</p>
<p>Then press Next button.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image0511.png"><img class="size-full wp-image-94 aligncenter" title="image0511" src="http://www.javabout.com/wp-content/uploads/2009/02/image0511.png" alt="image0511" width="500" height="500" /></a></p>
<p>In the second step of the Java Servlet configuration, you may or may not provide  any description that you want for your Java Servlet. Initialization Parameter is  extremely useful if your Java Servlet need to have initial value before being  executed. The URL mapping is used to identify on how the Java Servlet should be  called. This is the URL that is called in your &lt;form&gt;&lt;/form&gt; tag in  JSP file. If you set the URL Mapping into /test/GreetingServlet, you need to  call it as /test/GreetingServlet from your &lt;form&gt;&lt;/form&gt; tag in your  JSP file.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image053.png"><img class="size-full wp-image-95 aligncenter" title="image053" src="http://www.javabout.com/wp-content/uploads/2009/02/image053.png" alt="image053" width="500" height="500" /></a></p>
<p>In the last step of Java Servlet configuration, we can keep them as they are.  This wizard allows you to modify your Java Servlet’s methods. Press Finish button and we have  created a non-functional Java Servlet.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image055.png"><img class="size-full wp-image-96 aligncenter" title="image055" src="http://www.javabout.com/wp-content/uploads/2009/02/image055.png" alt="image055" width="500" height="500" /></a></p>
<p>If you have followed correctly, you should have a GreetingServlet.java similar  to the illustration below.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image059.png"><img class="size-full wp-image-97 aligncenter" title="image059" src="http://www.javabout.com/wp-content/uploads/2009/02/image059.png" alt="image059" width="550" height="334" /></a></p>
<p><strong>Related posts<br />
</strong><a href="http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part1" target="_self">- Introduction to Java Servlets with Eclipse (Part1)</a><br />
<a href="http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part2" target="_self">- Introduction to Java Servlets with Eclipse (Part2)<br />
</a><a href="http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part4" target="_self">- Introduction to Java Servlets with Eclipse (Part4)</a><a href="http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part2" target="_self"><br />
</a></p>

<!-- google_ad_section_end -->
]]></content:encoded>
			<wfw:commentRss>http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introduction to Java Servlets with Eclipse (Part2)</title>
		<link>http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part2/</link>
		<comments>http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part2/#comments</comments>
		<pubDate>Tue, 10 Feb 2009 11:03:55 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java Tutorials]]></category>
		<category><![CDATA[Introduction to Java Servlets with Eclipse]]></category>
		<category><![CDATA[Introduction to Java Servlets with Eclipse (Part2)]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[java about]]></category>
		<category><![CDATA[java servlet]]></category>
		<category><![CDATA[java servlet examples]]></category>
		<category><![CDATA[Java Servlets with Eclipse]]></category>
		<category><![CDATA[javabout]]></category>
		<category><![CDATA[part2]]></category>
		<category><![CDATA[servlet examples]]></category>

		<guid isPermaLink="false">http://www.javabout.com/?p=73</guid>
		<description><![CDATA[

(Part1) &#8211; Part2 -  (Part3) &#8211; (Part4)
Implementation of Tutorial’s Example
For our tutorial, we are going to use index.jsp to demonstrate how to implement Java Servlet that greets the users. Index.jsp will be used to obtain the first name as well as the last name / surname of the users. For this case, textboxes should be adequate as first name and surname. However, it would be different case if we are required to get the country of origin of the users. The use of combobox would be more appropriate for choosing ...]]></description>
			<content:encoded><![CDATA[
<!-- google_ad_section_start -->
<p><a href="http://www.javabout.com/2009/02/09/introduction-to-java-servlets-with-eclipse-part1" target="_self">(Part1)</a> &#8211; <strong>Part2<a href=" http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part2" target="_self"></a> </strong>-  <a href="http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part3" target="_self">(Part3)</a> &#8211; <a href="http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part4" target="_self">(Part4)</a></p>
<p><strong>Implementation of Tutorial’s Example</strong></p>
<p>For our tutorial, we are going to use index.jsp to demonstrate how to implement Java Servlet that greets the users. Index.jsp will be used to obtain the first name as well as the last name / surname of the users. For this case, textboxes should be adequate as first name and surname. However, it would be different case if we are required to get the country of origin of the users. The use of combobox would be more appropriate for choosing the country of origin as there are more than one choices to be chosen on. We are also required to create one more JSP file called greeting.jsp to greet the users. Hence, the flow would be index.jsp -&gt; GreetingServlet -&gt; greeting.jsp. In short, index.jsp will first be displayed to users, the users then fill his or her first and surname in index.jsp and press Submit button. This information is sent to our Java Servlet and our Java Servlet redirects the users to the greeting.jsp.</p>
<p>Now, we need to create a JSP file (we will call it index.jsp for our tutorial) to allow the user to enter his or her first name and surname. Right click on your WebContent, choose New and Other. A wizard is prompted to you to choose what type of files you want to include into your Dynamic Web Project.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image021.png"><img class="size-full wp-image-74 aligncenter" title="image021" src="http://www.javabout.com/wp-content/uploads/2009/02/image021.png" alt="image021" width="550" height="644" /></a></p>
<p>Choose JSP and press Next button.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image023.png"><img class="size-full wp-image-75 aligncenter" title="image023" src="http://www.javabout.com/wp-content/uploads/2009/02/image023.png" alt="image023" width="500" height="500" /></a></p>
<p>You can name your JSP with any name that you wish but for our tutorial  sample, I am going to name it index.jsp.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image025.png"><img class="size-full wp-image-76 aligncenter" title="image025" src="http://www.javabout.com/wp-content/uploads/2009/02/image025.png" alt="image025" width="500" height="570" /></a></p>
<p>The last step of configuring our JSP file is not really important. We can ignore  it which also means that we accept the default template from Eclipse. Press  Finish button and you should have index.jsp under your WebContent folder.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image027.png"><img class="size-full wp-image-77 aligncenter" title="image027" src="http://www.javabout.com/wp-content/uploads/2009/02/image027.png" alt="image027" width="500" height="571" /></a></p>
<p>If you double click on your JSP, your screen should look similar as below.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image029.png"><img class="alignnone size-full wp-image-78" title="image029" src="http://www.javabout.com/wp-content/uploads/2009/02/image029.png" alt="image029" width="550" height="334" /></a></p>
<p>In index.jsp, we need to have two textboxes for getting the user’s input and  a button to submit the information to our Java Servlet. After pressing the  button, the user should be redirected into greeting page. Now, let’s create the  textbox. The textbox command would be the same as HTML command that is &lt;input  type=”text” value=”50”&gt; and so on. Please remember that every textbox or  components whose values would like to be passed into Java Servlet must be within  &lt;form&gt;&lt;/form&gt; tag. Thus, please add below lines of codes into your  index.jsp after the &lt;h1&gt;&lt;/h1&gt; tag.</p>
<div class="java">
<table border="0" cellspacing="0" cellpadding="3" bgcolor="#ffffff">
<tbody>
<tr><!-- start source code --></p>
<td align="left" valign="top"><code><span style="color: #ffffff;"> </span><span style="color: #000000;">&lt;form action=</span><span style="color: #2a00ff;">" " </span><span style="color: #000000;">method=</span><span style="color: #2a00ff;">"POST"</span><span style="color: #000000;">&gt;</span><br />
<span style="color: #ffffff;"> </span><span style="color: #000000;">First Name: &lt;input type=</span><span style="color: #2a00ff;">"text" </span><span style="color: #000000;">name=</span><span style="color: #2a00ff;">"firstName" </span><span style="color: #000000;">size=</span><span style="color: #2a00ff;">"20"</span><span style="color: #000000;">&gt;&lt;br&gt;</span><br />
<span style="color: #ffffff;"> </span><span style="color: #000000;">Surname: &lt;input type=</span><span style="color: #2a00ff;">"text" </span><span style="color: #000000;">name=</span><span style="color: #2a00ff;">"surname" </span><span style="color: #000000;">size=</span><span style="color: #2a00ff;">"20"</span><span style="color: #000000;">&gt;</span><br />
<span style="color: #ffffff;"> </span><span style="color: #000000;">&lt;br&gt;&lt;br&gt;</span><br />
<span style="color: #ffffff;"> </span><span style="color: #000000;">&lt;input type=</span><span style="color: #2a00ff;">"submit" </span><span style="color: #000000;">value=</span><span style="color: #2a00ff;">"Submit"</span><span style="color: #000000;">&gt;</span><br />
<span style="color: #ffffff;"> </span><span style="color: #000000;">&lt;/form&gt;</span></code></td>
<p><!-- end source code --></tr>
</tbody>
</table>
</div>
<p><!-- =       END of automatically generated HTML code       = --><!-- ======================================================== -->Now, we have &lt;form&gt; and &lt;/form&gt;HTML Tag.Inside &lt;form&gt;&lt;/form&gt;  tag, we need to specify few values such as action and method. Action is used to  allow the JSP page knows which Java Servlet to be called on the invocation of  submit button and the method is used to select the preferred way to pass your  information to Java Servlet. This method attribute will be discussed further in  the later phase.</p>
<p>However, if you press the Submit button, it should not go anywhere as we do  not specify the action attribute of the &lt;form&gt;&lt;/form&gt; tag. Later on,  if we want this JSP to call our Java Servlet, we need to modify this action  attribute to our Java Servlet URL mapping.</p>
<p>We also have two textboxes now i.e &lt;input type=&#8221;text&#8221; name=&#8221;firstName&#8221;  size=&#8221;20&#8243;&gt; and &lt;input type=&#8221;text&#8221; name=&#8221;surname&#8221; size=&#8221;20&#8243;&gt;. There are  few important things that you need to pay attention here. The name attribute for  these textbox component are extremely important; in our index.jsp, it would be  firstName and surname. These keywords are used to obtain the information in Java  Servlet from the JSP page. It would be demonstrated in later phase once you have  seen the implementation of the Java Servlet. The type is used to define what  kind of component it is; text represents textboxes, button represents a button,  image represents a picture and etc. The size is used to determine the width of  the textbox. For more information, you can reference to the HTML Tag which is available in  Internet.</p>
<p>Now, as Tomcat is run in its own and is not integrated within Eclipse, we  need to start Tomcat manually. Albeit that we can actually run Tomcat within  Eclipse (with few configuration), it is suggested that it would be better to  start and shutdown Tomcat manually as it offers you more flexibility and should  allow you to understand how Tomcat works better. Yes, it may not be that  user-friendly but our intention is to make you understand the concept of Java  Servlet.</p>
<p>You need to export your Dynamic Web Project into .WAR file first before  deploying into Tomcat. WAR (WebArchive File) is actually similar to a  ZIP-compressed file that contains all of your necessary Web Project’s files. It  should be extracted and deployed by Tomcat when Tomcat runs.</p>
<p>Right click on your Dynamic Web Project, choose Export.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image031.png"><img class="size-full wp-image-79 aligncenter" title="image031" src="http://www.javabout.com/wp-content/uploads/2009/02/image031.png" alt="image031" width="385" height="635" /></a></p>
<p>Another wizard will be prompted to you. Choose WAR file and press Next button.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image033.png"><img class="size-full wp-image-80 aligncenter" title="image033" src="http://www.javabout.com/wp-content/uploads/2009/02/image033.png" alt="image033" width="470" height="550" /></a></p>
<p>In the next step of exporting your Dynamic Web Project into WAR file, you  need to provide Web Module and Destination information. Web Module is your  Dynamic Web Project name and the Destination will be the location where your WAR  file is located.</p>
<p>You need to remember one thing. To deploy WAR file to Tomcat, the WAR file  MUST be in the webapps folder of the Tomcat. This is the default behavior of  Tomcat. So if I have extracted my Tomcat into C:\tomcat-5.5.16, I should specify  the destination of my WAR file into C:\tomcat-5.5.16\webapps and then Press  Finish button.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image035.png"><img class="size-full wp-image-81 aligncenter" title="image035" src="http://www.javabout.com/wp-content/uploads/2009/02/image035.png" alt="image035" width="550" height="421" /></a></p>
<p>So the wizard should look like below.</p>
<p>Overwrite existing file should always be ticked as the older and existing WAR  file will be overwritten with the new WAR file. It should be obvious when you  are re-deploying your Dynamic Web Project into Tomcat.</p>
<p>Export source files option enables you to include all your source codes (JAVA  files) with the WAR file. While it is not recommended due to security issue,  this would be useful if you are about to deliver the project to the clients as  they definitely requires you to pass them the source codes.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image037.png"><img class="size-full wp-image-83 aligncenter" title="image037" src="http://www.javabout.com/wp-content/uploads/2009/02/image037.png" alt="image037" width="503" height="550" /></a></p>
<p><strong><br />
Related posts<br />
</strong><a href="http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part1" target="_self">- Introduction to Java Servlets with Eclipse (Part1)</a><br />
<a href="http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part3" target="_self">- Introduction to Java Servlets with Eclipse (Part3)<br />
</a><a href="http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part4" target="_self">- Introduction to Java Servlets with Eclipse (Part4)</a><a href="http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part2" target="_self"><br />
</a></p>

<!-- google_ad_section_end -->
]]></content:encoded>
			<wfw:commentRss>http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introduction to Java Servlets with Eclipse (Part1)</title>
		<link>http://www.javabout.com/2009/02/09/introduction-to-java-servlets-with-eclipse-part1/</link>
		<comments>http://www.javabout.com/2009/02/09/introduction-to-java-servlets-with-eclipse-part1/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 15:00:58 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Headline]]></category>
		<category><![CDATA[Java Tutorials]]></category>
		<category><![CDATA[Introduction to Java Servlets with Eclipse]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[java about]]></category>
		<category><![CDATA[java servlet]]></category>
		<category><![CDATA[java servlet examples]]></category>
		<category><![CDATA[Java Servlets with Eclipse]]></category>
		<category><![CDATA[javabout]]></category>
		<category><![CDATA[servlet examples]]></category>

		<guid isPermaLink="false">http://www.javabout.com/?p=3</guid>
		<description><![CDATA[

Part1 &#8211; (Part2)  -  (Part3) &#8211; (Part4)
Introduction
Java Servlet is the one of the most important Java technologies. It is the simplest model to build a complete Java J2EE Web Application. Furthermore, even for complex J2EE Web Application that uses Struts, Spring, EJB and etc, they are still using Servlet for certain purposes such as Servlet Filter, Listener and etc. Thus, it is just a good idea for you to have well-built understanding of Java Servlet. Prior reading this tutorial, it would be excellent if you have mastered the basic ...]]></description>
			<content:encoded><![CDATA[
<!-- google_ad_section_start -->
<p><strong>Part1</strong> &#8211; <a href=" http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part2" target="_self">(Part2) </a> -  <a href="http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part3" target="_self">(Part3)</a> &#8211; <a href="http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part4" target="_self">(Part4)</a></p>
<p><strong>Introduction</strong></p>
<p>Java Servlet is the one of the most important Java technologies. It is the simplest model to build a complete Java J2EE Web Application. Furthermore, even for complex J2EE Web Application that uses Struts, Spring, EJB and etc, they are still using Servlet for certain purposes such as Servlet Filter, Listener and etc. Thus, it is just a good idea for you to have well-built understanding of Java Servlet. Prior reading this tutorial, it would be excellent if you have mastered the basic Java programming languages.</p>
<p>At the completion of the tutorial, you are expected to comprehend the concept of the Java Servlet, be familiar with the ways to create Java Servlet using Eclipse 3.1.2, differences between POST and GET and should be ready to go to the next level.</p>
<p>In this tutorial, we are going to create one dynamic web application that asks the user for first name and surname. Then the system should response by greeting the users.</p>
<p>The tutorial consists of four main steps.</p>
<ol>
<li>Introduction to Eclipse 3.1.2</li>
<li>Creating New Web Application Project in Eclipse 3.1.2 with Web Tools Platform (WTP) Plug-in.</li>
<li>Creating Server Definition in Eclipse 3.1.2</li>
<li>Implementation of Tutorial’s Example</li>
<li>How to Start Tomcat Manually</li>
<li>How to Shutdown Tomcat Manually</li>
<li>Conclusion</li>
</ol>
<p><strong>Introduction to Eclipse 3.1.2</strong></p>
<p>Nowadays, many J2EE developers are using Eclipse as their IDE for Java programming language. One of the reasons is that Eclipse is free and is easily extendable. Yes, that is correct, there are plentiful plugins available in Internet for Eclipse. Additionally, the famous IBM WebSphere Application Developer for developing J2EE application in WebSphere is also implemented above the Eclipse framework.</p>
<p>Besides Java programming, Eclipse can also be used to edit any file such as C++, C or PHP and etc. You need to remember that Eclipse does not have any server embedded in it. Eclipse is just an editor. Well, let’s start to explore on how Eclipse can assist you in creating a Java Servlet.</p>
<p><strong>Creating New Web Application Project in Eclipse 3.1.2 with Web Tools Platform (WTP) Plug-in</strong></p>
<p>Okay, for our tutorial, we are going to exercise Web Tools Platform (WTP) plugin. I would suggest you to download Web Tools Platform (WTP) together with Eclipse so you should not be bothered on how to install the plugin into Eclipse and etc. Ideally, if you download Eclipse without any plugin, it does not have any support for J2EE Development. That is the core reason why various vendors are implementing J2EE plugins for Eclipse. Web Tools Platform is not the only J2EE plugin available for Eclipse. There are numerous J2EE plugins available in Internet such as Lomboz from ObjectLearn, MyEclipse and etc. If you are interested to know deeper on Eclipse plugin, you can visit <a href="http://www.eclipseplugincentral.com/" target="_blank">http://www.eclipseplugincentral.com/</a> for the complete list of available plugins for Eclipse.</p>
<p>To download Web Tools Platform (WTP), you can go to <a href="http://www.eclipse.org/webtools/ " target="_blank">http://www.eclipse.org/webtools/</a> and you can download it together with Eclipse by choosing the download link on the left panel of that particular site. Web Tools Platform (WTP) plugin with Eclipse is approximately 182 MB. It will be in zip file and you will not have installation for it. You can extract it to C:\ and you can directly use Eclipse by double clicking the Eclipse.exe file in the extracted folder.</p>
<p>Unlike NetBeans, Eclipse does not have any server embedded in it thus, in order to create a Java Servlet in Eclipse; you need to install the server (for our tutorial, you need Apache Tomcat) as well as the Java Development Kit (JDK) from Sun.</p>
<p>Before going further to our tutorial, it would be good if you understand the concept of Java Servlet as explained in the next paragraphs.</p>
<p>Creating a Java Servlet means that you are required to deal with JSP (JavaServer Pages). JSP is actually a HTML but unlike HTML, JSP may have Java codes (usually we call it as Scriptlet) embedded in it. In short words, we may represent JSP as dynamic HTML. In Java J2EE Web Application, JSP plays as a front-end while Java Servlet is the controller that contains the business logics, complex algorithms and etc.</p>
<p>For example, consider “Online University Student Registration System” developed in Java J2EE Web Application, the registration page where you fill in your details such as your name, your address, your username and etc are actually a JSP page. Later on, when you have completed filling out all the details and you press the submit button, all the information will be sent to Java Servlet for further processes. Java Servlet receives this information, does the necessary processes such as validations, generating user id and etc and then keeps the information to database. After successfully saving the data to database, Java Servlet redirects the user to the success page where the user can log in to the system. Likewise, if there is an unexpected error occurred happening in the middle of student registration system’s process, the user will be redirected to the error page.</p>
<p>Assuming that you have installed Eclipse with Web Tools Platform (WTP) Plugin, Java Development Kit 1.5 and Tomcat Server 5.5, let’s start to create our first Java Servlet. Start Eclipse and you should see similar to below illustration in your screen. Yes, this is the main page of Eclipse.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image001.png"></a><a href="http://www.javabout.com/wp-content/uploads/2009/02/image001.png"><img class="size-full wp-image-53 aligncenter" title="image001" src="http://www.javabout.com/wp-content/uploads/2009/02/image001.png" alt="image001" width="550" height="334" /></a></p>
<p>To create our Java Servlet, we are required to create a Project called Dynamic  Web Project. Go to your menu, choose New and Project. A wizard will be prompted  to you.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image003.png"><img class="size-full wp-image-55 aligncenter" title="image003" src="http://www.javabout.com/wp-content/uploads/2009/02/image003.png" alt="image003" width="490" height="520" /></a></p>
<p style="text-align: left;">Go to the Web and choose Dynamic Web Project and press Next. If you do not have  Dynamic Web Project available, it means that Web Tools Platform (WTP) plugin is  not correctly installed to Eclipse. Please re-download it or look at the  documentation of Web Tools Platform (WTP) plugin for more details.</p>
<p style="text-align: center;">
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image005.png"><img class="size-full wp-image-56 aligncenter" title="image005" src="http://www.javabout.com/wp-content/uploads/2009/02/image005.png" alt="image005" width="500" height="500" /></a></p>
<p>A wizard will be displayed to you. The wizard is mainly used to configure our Dynamic Web Project. You are allowed to name your Dynamic Web Project with any name that you wish but please consider to choose the name that is self-explanatory.</p>
<p>For our tutorial’s sample, I am going to name my Dynamic Web Project as MyFirstServlet. There is an interesting point worth to be looked at in this wizard. You need to provide the Target Runtime. What does it mean? Well, it basically requests you to identify the server that you would like to use for this Dynamic Web Project; in this case, Apache Tomcat. You may try to click on the combo box to observe whether you have any existing server(s) or not. If not, you are required to create one server for your project. If this is the first time you run Eclipse, you should not have any server yet. So, click on the New button on the right side of the Target Runtime combo box. Remember, after defining one server, you can re-use it in other projects so you do not need to define new server in every project unless if you wish to use other servers than Tomcat such as Bea WebLogic and etc.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image007.png"></a><a href="http://www.javabout.com/wp-content/uploads/2009/02/image007.png"><img class="size-full wp-image-59 aligncenter" title="image007" src="http://www.javabout.com/wp-content/uploads/2009/02/image007.png" alt="image007" width="550" height="445" /></a></p>
<p><strong>Creating Server Definition in Eclipse 3.1.2</strong></p>
<p>If you have configured the server, you may skip this step.</p>
<p>After you click on New button to choose a new server, another wizard should be displayed on top of the existing wizard. In this wizard, you are asked what server you wish to use. For our tutorial, we should use Apache Tomcat 5.5 so you need to select Apache Tomcat v5.5 from the list and press Next button.</p>
<p>From the illustration below, it is obvious that there are other servers available for you to select such as Bea WebLogic, JBoss and etc.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image009.png"><img class="size-full wp-image-65 aligncenter" title="image009" src="http://www.javabout.com/wp-content/uploads/2009/02/image009.png" alt="image009" width="438" height="453" /></a></p>
<p>In the next wizard, we are required to provide the information on where our Apache Tomcat is located. In my machine, I extracted it on C:\tomcat-5.5.16 so I got to click on the Browse button on right side of the Tomcat installation directory textbox and we need to select the Tomcat folder for it. Additionally, you also need to provide on what Java Run Time (JRE) you want to use. If you have installed Java Development Kit (JDK), it should also install JRE automatically. Eclipse should be able to auto-detect and should provide you few options in the list. Choose the JRE that is installed together with the installation of your Java Development Kit (JDK). You can press Finish button and our server’s configuration is completed.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image011.png"><img class="size-full wp-image-66 aligncenter" title="image011" src="http://www.javabout.com/wp-content/uploads/2009/02/image011.png" alt="image011" width="438" height="453" /></a></p>
<p>Back to our Dynamic Web Project configuration, after configuring the server, we should have Tomcat 5.5 as one of the options in the combo box for Target Runtime. Please select the server that you just recently added or have configured and press Next button.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image013.png"><img class="size-full wp-image-68 aligncenter" title="image013" src="http://www.javabout.com/wp-content/uploads/2009/02/image013.png" alt="image013" width="550" height="445" /></a></p>
<p>Next wizard is shown. We can ignore them and keep them as default. Press Next button.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image015.png"><img class="size-full wp-image-67 aligncenter" title="image015" src="http://www.javabout.com/wp-content/uploads/2009/02/image015.png" alt="image015" width="550" height="445" /></a></p>
<p>Now, we are in the last step of our Dynamic Web Project configuration. For this wizard, you should not modify the Context Root albeit you are allowed to do so as to understand the concept of context root may be puzzling. You can leave them as default and when you are more familiar with Java programming, you may try to play around with it.</p>
<p>You can amend the Content Directory with any name that you wish. Normally, J2EE Developers will name it the same as the Project Name to increase the maintainability of the project. For this tutorial, we are not going to change it and we are going to keep it as default &#8211; WebContent. For your information, this directory is the place where all the deployment descriptor, JSP files, other configuration files are located. Press Finish button and we have completed our Dynamic Web Project configuration.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image017.png"><img class="size-full wp-image-69 aligncenter" title="image017" src="http://www.javabout.com/wp-content/uploads/2009/02/image017.png" alt="image017" width="550" height="445" /></a></p>
<p>You should be able to see something like below illustration.</p>
<p style="text-align: center;"><a href="http://www.javabout.com/wp-content/uploads/2009/02/image019.png"><img class="size-full wp-image-70 aligncenter" title="image019" src="http://www.javabout.com/wp-content/uploads/2009/02/image019.png" alt="image019" width="360" height="394" /></a></p>
<p><strong>Related posts<br />
</strong><a href="http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part1" target="_self"></a><a href="http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part2" target="_self">- Introduction to Java Servlets with Eclipse (Part2)<br />
</a><a href="http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part3" target="_self">- Introduction to Java Servlets with Eclipse (Part3)<br />
</a><a href="http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part4" target="_self">- Introduction to Java Servlets with Eclipse (Part4)</a><a href="http://www.javabout.com/2009/02/10/introduction-to-java-servlets-with-eclipse-part2" target="_self"><br />
</a></p>

<!-- google_ad_section_end -->
]]></content:encoded>
			<wfw:commentRss>http://www.javabout.com/2009/02/09/introduction-to-java-servlets-with-eclipse-part1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

