달력

4

« 2024/4 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
출처 : http://www.java-tips.org/java-tutorials/tutorials/introduction-to-java-servlets-with-eclipse.html


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 Java programming languages.

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.

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.

The tutorial consists of four main steps.

  1. Introduction to Eclipse 3.1.2
  2. Creating New Web Application Project in Eclipse 3.1.2 with Web Tools Platform (WTP) Plug-in.
  3. Creating Server Definition in Eclipse 3.1.2
  4. Implementation of Tutorial’s Example
  5. How to Start Tomcat Manually
  6. How to Shutdown Tomcat Manually
  7. Conclusion



Introduction to Eclipse 3.1.2

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.

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.

Creating New Web Application Project in Eclipse 3.1.2 with Web Tools Platform (WTP) Plug-in

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 visithttp://www.eclipseplugincentral.com/ for the complete list of available plugins for Eclipse.

To download Web Tools Platform (WTP), you can go to http://www.eclipse.org/webtools/ 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.

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.

Before going further to our tutorial, it would be good if you understand the concept of Java Servlet as explained in the next paragraphs.

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.

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.

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.



Image 

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.



Image 

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.



Image 

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.

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.



Image 
:
Posted by 라면스프