eclipse的经验
方法/步骤
1、新建javaProject工程。导入servlet-api.jar包。
2、新建class文件,粘贴代码。im圬桦孰礅portjava.io.IOException;importjava.util.Date;i罪焐芡拂mportjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;publicclassHelloServletextendsHttpServlet{ @SuppressWarnings("deprecation") publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse){ try{ response.getWriter().println("<h1>helloservlet!</h1>"); response.getWriter().println(newDate().toLocaleString()); }catch(IOExceptione){ //TODO:handleexception e.printStackTrace(); } }}
3、新建web/WEB-INF/web饱终柯肢.xml文件,粘贴代码。<?xmlversion="1.0"encoding="UTF-8"?>&l墉掠载牿t;web-app> <servlet> <servlet-name>HelloServlet</servlet-name> <servlet-class>HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping></web-app>
4、修改java目录,与tomcat匹配。修改tomcat/conf/sever.xml文件,链接到java,粘贴代码。<Contextpath="/"docBase="d:\\project\\j2ee\\web"debug="0"reloadable="false"/>
5、删除tomcat\webapps\ROOT文件夹。重启tomcat,打开浏览器,执行代码,刷新会变。http://127.0.0.1:8080/hello