Eclipse+Tomcat开发servlet

近日在学习用Eclipse开发Servlet的时候在网上看了N多篇教程,搞得我一头雾水,很多文章都是一个抄一个,抄得都一模一样,都没有自己用心去写,所以搞得一篇早就过时N年的文章仍然在网上广为流传!实在看不过去了,所以写下这篇开发心得,希望能帮到刚学Servlet的朋友。

以Hello World为例, 其基本步骤是:


1.  选择File->New->Project

2.  在对话框中选择Tomcat Project,然后next

3.  输入Project Name,这里可以写HelloWorld,继续next

4.  勾上can update server.xml file选项,finish,好了,一个Poject已经创建好了。

5.  打开new class 向导,在Name中填入HelloWorld,在Superclass中填入javax.servlet.http.HttpServlet,按下finish,好了,一个servlet就创建好了

6.  在HelloWorld.java中重载doGet方法,代码如下:
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;

public class HelloWorld extends HttpServlet {
       public void doGet(HttpServletRequest request,HttpServletResponse response)
      throws IOException, ServletException {
       response.setContentType("text/html");
       PrintWriter writer = response.getWriter();
       writer.println("Hello World!");
      }
}

7.在WEB-INF目录下新建web.xml文件如下(注意这个xml文件要建在工程下的WEB-INF里面):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<web-app>
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>servlet/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>

8.Ctrl+S保存,右键HelloWorld project,选择Tomcat Project->update context to server.xml
  注意:这里很重要,此步骤eclipse会自动修改了server.xml文件,添加HelloWorld项目。

9.在Eclipse中启动Tomcat

10.在用浏览器访问http://localhost:8080/HelloWorld/servlet/HelloWorld
 

eclipse+tomcat开发servlet的问题

07-25

在eclipse下建立了一个Tomcat Project,然后在这个Project下写了一个servlet程序,并在Project的WEB-INF目录下建立了一个web.xml配置我的这个servlet,程序完成后运行很正常,当时也就没有再理会,过了几天以后再运行的时候就出现问题了,首先用eclipse启动Tomcat的时候console会提示下面这样的信息:rn- Initializing Coyote HTTP/1.1 on http-8080rn- Initialization processed in 3595 msrn- Starting service Catalinarn- Starting Servlet Engine: Apache Tomcat/5.5.7rn- XML validation disabledrn- Parse Fatal Error at line 1 column 3: The processing instruction must begin with the name of the target.rnorg.xml.sax.SAXParseException: The processing instruction must begin with the name of the target.rn at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)rn at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source)rn at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)rn at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)rn at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(Unknown Source)rn at com.sun.org.apache.xerces.internal.impl.XMLScanner.scanPI(Unknown Source)rn at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDispatcher.dispatch(Unknown Source)rn at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)rn at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)rn at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)rn at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)rn at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)rn at org.apache.tomcat.util.digester.Digester.parse(Digester.java:1561)rn at org.apache.catalina.startup.ContextConfig.applicationWebConfig(ContextConfig.java:302)rn at org.apache.catalina.startup.ContextConfig.start(ContextConfig.java:959)rn at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:249)rn at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)rn at org.apache.catalina.core.StandardContext.start(StandardContext.java:4020)rn at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1011)rn at org.apache.catalina.core.StandardHost.start(StandardHost.java:718)rn at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1011)rn at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:440)rn at org.apache.catalina.core.StandardService.start(StandardService.java:450)rn at org.apache.catalina.core.StandardServer.start(StandardServer.java:683)rn at org.apache.catalina.startup.Catalina.start(Catalina.java:537)rn at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)rn at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)rn at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)rn at java.lang.reflect.Method.invoke(Unknown Source)rn at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:271)rn at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:409)rn- Parse error in application web.xmlrnorg.xml.sax.SAXParseException: The processing instruction must begin with the name of the target.rn at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)rn at org.apache.tomcat.util.digester.Digester.parse(Digester.java:1561)rn at org.apache.catalina.startup.ContextConfig.applicationWebConfig(ContextConfig.java:302)rn at org.apache.catalina.startup.ContextConfig.start(ContextConfig.java:959)rn at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:249)rn at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)rn at org.apache.catalina.core.StandardContext.start(StandardContext.java:4020)rn at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1011)rn at org.apache.catalina.core.StandardHost.start(StandardHost.java:718)rn at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1011)rn at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:440)rn at org.apache.catalina.core.StandardService.start(StandardService.java:450)rn at org.apache.catalina.core.StandardServer.start(StandardServer.java:683)rn at org.apache.catalina.startup.Catalina.start(Catalina.java:537)rn at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)rn at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)rn at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)rn at java.lang.reflect.Method.invoke(Unknown Source)rn at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:271)rn at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:409)rn- Occurred at line 1 column 3rn- Marking this application unavailable due to previous error(s)rn- Error getConfiguredrn- Context startup failed due to previous errorsrn- Unable to find config file. Creating new servlet engine config file: /WEB-INF/server-config.wsddrn- org.apache.webapp.balancer.BalancerFilter: init(): ruleChain: [org.apache.webapp.balancer.RuleChain: [org.apache.webapp.balancer.rules.URLStringMatchRule: Target string: News / Redirect URL: http://www.cnn.com], [org.apache.webapp.balancer.rules.RequestParameterRule: Target param name: paramName / Target param value: paramValue / Redirect URL: http://www.yahoo.com], [org.apache.webapp.balancer.rules.AcceptEverythingRule: Redirect URL: http://jakarta.apache.org]]rn- ContextListener: contextInitialized()rn- SessionListener: contextInitialized()rn- ContextListener: contextInitialized()rn- SessionListener: contextInitialized()rn- Starting Coyote HTTP/1.1 on http-8080rn- JK2: ajp13 listening on /0.0.0.0:8009rn- Jk running ID=0 time=0/290 config=nullrn- Find registry server-registry.xml at classpath resourcern- Server startup in 13209 msrnrn然后访问我的servlet则不能正常运行了,控制台提示这样的信息:rn- HTMLManager: init: Associated with Deployer 'Catalina:type=Deployer,host=localhost'rn- HTMLManager: init: Global resources are availablern- HTMLManager: list: Listing contexts for virtual host 'localhost'rn- HTMLManager: list: Listing contexts for virtual host 'localhost'rnrn还请高手指教。谢谢先

没有更多推荐了,返回首页