Step1.
- File > New > Maven Project
Step2 .
- Group Id : tw.com.ocean //輸入你想要的 "名稱空間"Artifact Id: MyWebProject //輸入你想要的 "專案名稱"Packaging: war
- Finish
Step4. 設定運行環境
Step7. 修改web.xml 檔
- 在專案上,
滑鼠右鍵 > Properties > Project Facets
Dynamic Web Module 2.5 (3.0 是 tomcat7)
java 1.7 (JDK 版本)
javaScript 1.0
Step5. 建立 Servlet
Note:執行此步驟後,Eclipse會在 /ProjectD/src/main/webapp中建立
● WEB-INF
● web.xml
- 在java Resources上,滑鼠右鍵 > New > Servlet
- 輸入Servlet 名稱,這裡用Controller作為範例。
Note:執行此步驟後,Eclipse會在 /ProjectD/src/main/webapp中建立
● WEB-INF
● web.xml
<?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_2_5.xsd" version="2.5">
<display-name>ProjectD</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>
</web-app>
Step6 . 建立 index.jsp 檔
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
Hello !!!!
</body>
</html>
Step7. 修改web.xml 檔
<?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_2_5.xsd" version="2.5">
<display-name>ProjectD</display-name>
<servlet>
<description></description>
<display-name>Controller</display-name>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>Controller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>homeJSP</servlet-name>
<jsp-file>/index.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>homeJSP</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
Step8. 修改Controller.java 檔
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Controller
*/
public class Controller extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Controller() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Post");
RequestDispatcher dispatcher = request.getRequestDispatcher("/hello");
}
}
Step 9. Run on Server
- 執行專案後,將導向Controller.java ,在導向index.jsp
下一篇將進入Maven Spring 3
沒有留言:
張貼留言