java的servlet配置
一.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.網頁訪問時