刚装的myeclipse2013 all-in-one,建立web project工程和程序servlet文档结构如图
程序内容:import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class HelloServlet extends HttpServlet { /**
 * Constructor of the object.
 */
public HelloServlet() {
super();
} /**
 * Destruction of the servlet. <br>
 */
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
 * The doGet method of the servlet. <br>
 *
 * This method is called when a form has its tag value method equals to get.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println("  <BODY>");String requestURI=request.getRequestURI();
out.println("<form action='"+requestURI+"' method='post'>");
out.println("Input your name:<input type='text' name='name'/>");
out.println("<input type='submit'/>");
out.println("</form>");
out.println("");

String name=request.getParameter("name");
if(name!=null && name.trim().length()>0){
out.println("Hello,<b>"+name+"</b>.");
out.println("  </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
} /**
 * The doPost method of the servlet. <br>
 *
 * This method is called when a form has its tag value method equals to post.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println("  <BODY>");
out.print("    This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println("  </BODY>");
out.println("</HTML>");
out.flush();
out.close();
} /**
 * Initialization of the servlet. <br>
 *
 * @throws ServletException if an error occurs
 */
public void init() throws ServletException {
// Put your code here
}}web.xml为自动生成,内容:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>HelloServlet</servlet-name>
    <servlet-class>com.mikedehill.firstweb.HelloServlet</servlet-class>
  </servlet>  <servlet-mapping>
    <servlet-name>HelloServlet</servlet-name>
    <url-pattern>/servlet/HelloServlet</url-pattern>
  </servlet-mapping></web-app>tomcat可以启动
2013-7-11 16:53:57 org.apache.tomcat.util.digester.SetPropertiesRule begin
警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:FirstWeb' did not find a matching property.
2013-7-11 16:53:58 org.apache.catalina.core.AprLifecycleListener initializeSSL
信息: OpenSSL successfully initialized (OpenSSL 1.0.1d 5 Feb 2013)
2013-7-11 16:53:59 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-apr-8080"]
2013-7-11 16:53:59 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["ajp-apr-8009"]
2013-7-11 16:53:59 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 3969 ms
2013-7-11 16:53:59 org.apache.catalina.core.StandardService startInternal
信息: Starting service Catalina
2013-7-11 16:53:59 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet Engine: Apache Tomcat/7.0.40
2013-7-11 16:54:01 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\Program Files\apache-tomcat-7.0.40\webapps\DeployCopy
2013-7-11 16:54:02 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\Program Files\apache-tomcat-7.0.40\webapps\docs
2013-7-11 16:54:02 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\Program Files\apache-tomcat-7.0.40\webapps\examples
2013-7-11 16:54:02 org.apache.catalina.core.ApplicationContext log
信息: ContextListener: contextInitialized()
2013-7-11 16:54:02 org.apache.catalina.core.ApplicationContext log
信息: SessionListener: contextInitialized()
2013-7-11 16:54:02 org.apache.catalina.core.ApplicationContext log
信息: ContextListener: attributeAdded('org.apache.jasper.compiler.TldLocationsCache', 'org.apache.jasper.compiler.TldLocationsCache@1a05bdb')
2013-7-11 16:54:02 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\Program Files\apache-tomcat-7.0.40\webapps\host-manager
2013-7-11 16:54:03 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\Program Files\apache-tomcat-7.0.40\webapps\manager
2013-7-11 16:54:03 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\Program Files\apache-tomcat-7.0.40\webapps\ROOT
2013-7-11 16:54:03 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-apr-8080"]
2013-7-11 16:54:03 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-apr-8009"]
2013-7-11 16:54:03 org.apache.catalina.startup.Catalina start
信息: Server startup in 3952 ms1、按小三角运行按钮显示:the serve is not specified2、用浏览器键入:http://localhost:8080/FirstWeb/servlet/HelloServlet后回车,显示:
HTTP Status 404 - /FirstWeb/servlet/HelloServlet--------------------------------------------------------------------------------type Status reportmessage /FirstWeb/servlet/HelloServletdescription The requested resource is not available.
--------------------------------------------------------------------------------Apache Tomcat/7.0.40请教各位,这个程序应该如何才能编译运行?

解决方案 »

  1.   

    高科技啊。。我都还在用MyEclipse6.0   
      

  2.   

    是你项目没部署成功到的原因,你看你tomcat控制台输出信息,自带的root,manage等都都有输出信息,没有你的firstWeb,你重新部署一下,然后点击run as server application
      

  3.   

    404是路径问题,tomcat没有找到
      

  4.   

    http://localhost:8080/FirstWeb/servlet/HelloServlet   
    对类进行访问?  没用过。
      

  5.   

    从tomcat控制台的信息来看,你的项目没有deploy成功~~所以浏览器访问肯定是404。
      

  6.   

    没什么问题啊..是不是你的web容器用的是myeclipse中的默认的啊..而且那个三角下面不是都能直接点击的..里面会记录你最近运行的项目名称..说不定你运行的是其他项目而不是你现在这个项目呢..你应该在servlet那个类里面直接点右键..然后run as server就ok了..
      

  7.   

    首先,web项目是需要部署到tomcat下的,怎么配置服务器环境怎么部署,百度。
    其次,servlet不是java可运行文件,是不能点三角图标来运行的,要部署到tomcat下用url来访问。
      

  8.   

    首先要配置好tomcat,其次要将项目部署到tomcat中,然后开启tomcat,最后在浏览器访问!总共四个步骤!至于每一个步骤如何做!我相信只要学了servlet开发的就应该会配置!
      

  9.   


    现在知道是tomcat的问题,但具体在哪不知道。myeclipse自带tomcat7(原来不知道),我将程序部署在自带tomcat7上即可访问了。那么问什么我引入的外部tomcat7.x可以在mycelipse中运行,却不能成功部署程序?谁知道?我希望程序在两个tomcat中都能运行。
      

  10.   


    现在知道是tomcat的问题,但具体在哪不知道。myeclipse自带tomcat7(原来不知道),我将程序部署在自带tomcat7上即可访问了。那么为什么我引入的外部tomcat7.x可以在mycelipse中运行,却不能成功部署程序?谁知道?我希望程序在两个tomcat中都能运行。打错了,是为什么
      

  11.   

    1、工具栏windows->showview->servers
    2、servers视图,右键选择Add and Remove
    3、选择你的项目添加到server
    4、点击三角运行