| Page 2 of 3 
  
    
| 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 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 - GreetingServlet ->
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. |  |  |  |  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 <input type=”text” value=”50”> and so on.
Please remember that every textbox or components whose values would
like to be passed into Java Servlet must be within
<form></form> tag. Thus, please add below lines of codes
into your index.jsp after the <h1></h1> tag.</p>
 
   
  
   | 
    <form action="GreetingServlet" method="POST">First Name: <input type="text" name="firstName" size="20"><br />
 Surname: <input type="text" name="surname" size="20">
 <br /><br />
 <input type="submit" value="Submit">
 </form>
 |  Now, we have <form> and </form> HTML tag. Inside
<form></form> 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. We also have two textboxes now i.e <input type="text"
name="firstName" size="20"> and <input type="text" name="surname"
size="20">. 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. Okay, you must be wondering how the index.jsp looks like now when it
has been executed. Let’s deploy our JSP into the bundled Tomcat in
NetBeans. Right-click your Web Application Project and choose Run
Project. This menu first compiles and deploys the application to the
Server and subsequently runs the application by executing its
index.jsp. Yes, that is correct; it is just one click to run your Web
Application within the NetBeans. Later on, if you have modified your
JSP or your Java Servlet, you can merely re-deploy the application and
all the changes will be reflected. You also need to refresh your
Internet Browser. 
 
   
 
 While running your Web Application Project, you may be prompted with
a dialog box showing the progress of your Web Application project. If
you carefully pay attention on the dialog box, they once display the
message like “Starting Tomcat 5.5.7” and etc. Wait for a few seconds
and your default Internet Browser will be launched and it should
display your new index.jsp. Below screenshot shows how the index.jsp should look like. 
 
   
 
 We have completed our index.jsp. What we need to do now is to create
our Java Servlet which is called GreetingServlet. Actually, the name of
the Java Servlet can be anything as you want. The most important thing
is how you map this Java Servlet in your web.xml to be accessible from
your JSP. Well, web.xml is actually a deployment descriptor and it must
always be present for each Java J2EE Web Application. For now, what you
need to know is that every Java Servlet that you create must be
registered in the web.xml file. Thus, this file may be getting bigger
and bigger if the project is getting more complex. This will be
described in the next phase. To create your Java Servlet, you need to right-click your Source
Packages section within your Web Application and choose New >
Servlet as shown on below figures. 
 
   
 
   
 
 Again, a wizard should pop up for you to configure your Java
Servlet. Your Java Servlet name can be anything but for this tutorial
sample, let’s name it GreetingServlet. If you want the Java Servlet to
be on different name, you need to modify the mapping of your web.xml as
well. This web.xml will be discussed in later phase. For now, we can
keep the location be in the Source Packages. This Source Packages
should only contain Java files. The reason we have this Source Packages
is to assist us in maintaining our Web Application. Consider that if
you have all the JSP, Java and other files in one folder, it would be
hard for you or the new developers to track the flow of the
applications.  For the Package, you can define any package that you want. 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”. Then
press Next button. 
 
 | 
				
Excellent topic included with Netbean 5.0 editor