[java web 入门](一)MyEclipse & HelloWorld 记录
第一部,下载安装MyEclipse for mac.
http://downloads.myeclipseide.com/downloads/products/eworkbench/2014/installers/myeclipse-spring-2014-GA-offline-installer-macosx.dmg
http://downloads.myeclipseide.com/downloads/products/eworkbench/2014/installers/myeclipse-pro-2014-GA-offline-installer-macosx.dmg
第二部,helloWorld
1,新建项目
2, 新建MyServlet类,继承字HttpServlet,重写doGet和doPost方法,调用process方法响应请求。
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; @SuppressWarnings("serial") public class MyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { process(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { process(req,resp); } /** * @param req * @param resp * @throws ServletException * @throws IOException */ protected void process(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html"); PrintWriter out = resp.getWriter(); out.println("<HTML><HEAD><TITLE>My First Servlet</TITLE></HEAD>"); out.println("<BODY>"); out.println("<H1>Hello , Jason Hu</H1>"); out.println("</BODY></HTML>"); } }
3,在web.xml中配置servlet及servlet-mapping
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>web01</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>MyFirstServlet</servlet-name> <servlet-class>com.jason.web.helloworld.MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyFirstServlet</servlet-name> <url-pattern>/MyServlet</url-pattern> </servlet-mapping> </web-app>
4,执行项目,http://192.168.1.117:8080/HelloWorld/MyServlet
5,项目代码包: 点击打开链接