java left logo
java middle logo
java right logo
 


 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4092
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Introduction to Java Servlets with Eclipse E-mail
User Rating: / 1363
PoorBest 

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.

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.



Image

Choose JSP and press Next button.



Image

You can name your JSP with any name that you wish but for our tutorial sample, I am going to name it index.jsp.



Image

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.



Image

If you double click on your JSP, your screen should look similar as below.



Image

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.

    <form action=" " 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.

However, if you press the Submit button, it should not go anywhere as we do not specify the action attribute of the <form></form> 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.

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.

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.

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.

Right click on your Dynamic Web Project, choose Export.



Image

Another wizard will be prompted to you. Choose WAR file and press Next button.



Image

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.

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.



Image

So the wizard should look like below.

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.

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.



Image



 
< Prev   Next >
Posted by Prabhu, prabhurangasamy@gmail.com on Tuesday, 27 June 2006 at 9:17

Hai guys. This is the best, best, best website which gives me an idea for using the webtools platform of eclipse.


 1 
Page 1 of 1 ( 1 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.