Home > Article > Backend Development > Develop Servlet programs using Tomcat and Eclipse_PHP tutorial
1. Install eclipse
1). Download Eclipse IDE for Java EE Developers directly from the official website and unzip it;
2. Install tomcat plug-in in eclipse:
1). Download tomcatPluginV33.zip at http://www.eclipsetotale.com/tomcatPlugin.html
2). Unzip it into the plugins directory under the eclipse directory
3). Restart eclipse
4). If you can’t find the Server selection in Window -> Prefences, it means you downloaded the wrong eclipse version. You need to download Eclipse IDE for Java EE Developers
5). Select Runtime Environment in the Server column, select Add on the right, select the installation path and download. After the download is completed, click finished and you will see that the selected version of tomcatServer has been established
6) Configure tomcat in Window -> Prefences -> Tomcat. Tomcat home select the directory you just downloaded
7). Click the start tomcat button and you will find a large amount of printing information. Enter http://localhost:8080 in the browser and the tomcat web page will appear, proving that it has been installed.
3 Create a new Servlet test program
1). Select the menu File->New->Project..., select Web->Dynamic Web Project in the new project wizard, the project name is MyFirstDynamicWebProject, go to next, and finally choose to generate web.xml;
2). New a class, package com.johnny.test, name is helloworld, super class is HttpServlet;
3), code:
[java] package com.johnny.test;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorld extends HttpServlet implements javax.servlet.Servlet{
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
Public HelloWorld() {
super();
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().write("Hello, world 1112!");
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
}
package com.johnny.test;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class HelloWorld extends HttpServlet implements javax.servlet.Servlet{
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public HelloWorld() {
super();
}
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().write("Hello, world 1112!");
}
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
web.xml
[html]
4)、 右键点击HelloWorld.java,选择rus as :run on server,在浏览器上会显示:Hello, world 1112!
5)、 或者右键点击工程,export MyFirstDynamicWebProject.war,然后放到ubuntu的tomcat的webapps目录下