一.servers配置
eclipse(neon)-File-New-Other-Server-Server-Apache-Tomcat v8.0 Server
二、新建项目
1.File-New-Dynamic Web Project
2.取Project name(例:ido10171)
3.建servlet类
ido10171-Java Resources-src--->建包(com.servlet)--->建class(FirstServlet)
例:
FirstServlet--->继承(extends)HttpServlet
覆写其中的(doGet doPost)去掉super();
doPost里面写
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// super.doPost(req, resp);
PrintWriter pw=response.getWriter();
response.setContentType("text/html;");
pw.println("<html>");
pw.println("<head>");
pw.println("</head>");
pw.println("<body>");
pw.println(" <h1>hello. this is our fisrt Servlet . </h1>");
pw.println(" <h2>hello. this is our fisrt Servlet . </h2>");
pw.println("</body>");
pw.println("</html>");
pw.close();
}
覆写(右键Source-Override/Implement Methods)
4.插入web.xml (ido10171右键 Java EE Tools第二个(Generate Deployment Descriptor Stub))
向web.xml里面写内容
web.xml-Source------>
</welcome-file-list> 开始
<servlet>
<servlet-name>a</servlet-name>
<servlet-class>com.servlet.FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>a</servlet-name>
<url-pattern>/abc/def/eh</url-pattern>
</servlet-mapping>
结尾 </web-app>
说明:1.上下两个名字一致
2.<servlet-class>包名.类名</servlet-class>
3.<url-pattern>路径名</url-pattern>
5.写html(ido10171-Webcontent-右键New-HTML File-hello.html)
html内容
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
地址与web.xml相同
<form action="abc/def/eh" method="post">
用户名:<input name="username" /> <br/>
密码:<input name="password" type="password" /> <br/>
<input type="submit" />
</form>
</body>
</html>
三、其他操作
1.添加Servers(Window-Show View-Servers)
2.添加运行项目 点开Servers-Tomcat v8.0 Server at localhost右键-Add and Remove(左边加到右边)
3.运行Tomcat v8.0 Server at localhost右键-Start(红灯) 停止时(右键Stop)
4.网页访问时
4.1先访问hello.html 地址为:http://localhost:8080/ido-10171/hello.html.
4.2在hello.html中提交后,显示在doPost中写的html内容 地址为:http://localhost:8080/ido-10171/abc/def/eh