刚装的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 ms
1、按小三角运行按钮显示:the serve is not specified
2、用浏览器键入:http://localhost:8080/FirstWeb/servlet/HelloServlet后回车,显示:
HTTP Status 404 - /FirstWeb/servlet/HelloServlet
--------------------------------------------------------------------------------
type Status report
message /FirstWeb/servlet/HelloServlet
description The requested resource is not available.
--------------------------------------------------------------------------------
Apache Tomcat/7.0.40
请教各位,这个程序应该如何才能编译运行?