Home > Backend Development > PHP Tutorial > Develop Servlet programs using Tomcat and Eclipse_PHP tutorial

Develop Servlet programs using Tomcat and Eclipse_PHP tutorial

WBOY
Release: 2016-07-14 10:11:55
Original
1051 people have browsed it

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]   
 
   
    HelloWorld 
    com.johnny.test.HelloWorld 
 
 
   
    HelloWorld 
    /helloworld 
 
 
   
 



 
   HelloWorld
   com.johnny.test.HelloWorld
 

 
   HelloWorld
   /helloworld
 

 

4)、 右键点击HelloWorld.java,选择rus as :run on server,在浏览器上会显示:Hello, world 1112!
5)、 或者右键点击工程,export MyFirstDynamicWebProject.war,然后放到ubuntu的tomcat的webapps目录下

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/477266.htmlTechArticle1、 安装eclipse 1)、 在官网上直接下载Eclipse IDE for Java EE Developers,解压即可; 2、 eclipse安装tomcat插件: 1)、 在http://www.eclipsetotale.com/tomcat...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template