https://www.yiibai.com/servlet/creating-servlet-in-myeclipse-ide.html
如何在myeclipse IDE中創建Servlet?
要在myeclipse IDE中創建Servlet,可參考以下幾個步驟 -
- 創建一個Web項目
- 創建一個html文件
- 創建一個servlet
- 啟動MyEclipse tomcat服務器並部署項目
請依次按照以下步驟在MyEclipse IDE中創建servlet。步驟如下:
1.創建Web項目:
要創建一個Web項目,首先打開MyEclipse,單擊文件菜單 -> 新建 - > Web項目 ->填寫項目名稱,例如,要創建一個項目的名稱為:MyeclipseServlet 。
在打開的對話框中,填寫相關項目的信息,如下圖所示 -
點擊完成(Finish),完整的項目結構如下圖所示 -
2.創建html文件
可以看到有一個名稱為:MyeclipseServlet項目被創建成功了,現在來瀏覽這個項目。
要創建一個html文件,請右鍵單擊WebRoot -> New - > HTML(Advanceed Templates) -> 填寫html文件名,例如:index.html -> 完成。
在彈出對話框中,填寫HTML文件的名稱:index.html,如下所示 -
下面對這個index.html
文件中的代碼進行簡單的修改,修改的結果如下所示 -
<!DOCTYPE html> <html> <head> <title>index.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <div style="text-align:center;"> <form action="/MyeclipseServlet/SayHello" method="POST"> 輸入名字:<input type="text" name="name"/> <input type="submit" value="提交"> </form> </div> </body> </html>
3.創建servlet
要創建Servlet,請單擊菜單文件(File) -> 新建(New) -> Servlet -> 填寫servlet名稱,例如: SayHello
-> 選中doGet()
方法復選框 -> 下一步(Next>) -> 完成。
填寫要創建Servlet的信息,這里要在com.yiibai
包中,創建一個名稱為SayHello
的Servlet,如下所示 -
Servlet配置的相關信息 -
可以看到一個名為SayHello.java
的servlet文件被創建。接下來將在這個文件里編寫servlet代碼。刪除SayHello.java
類中其它的方法,只保留doPost()
並重寫此方法的功能,如下所示 -
package com.yiibai; 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; public class SayHello extends HttpServlet { /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String name = null; // 獲取表單Post過來的數據 name = request.getParameter("name"); if(name==null){ name = ""; } out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>SayHello Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "+this.getClass()+", using the POST method"); out.println("<hr/>"); out.println("Hello, "+name); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } }
現在刪除項目中的index.jsp
文件,並將index.html
文件設置為項目的默認頁面。打開web.xml
文件,並將歡迎文件(<welcome-file>
標簽)名稱更改為index.html
。同時修改SayHello
的映射url為/SayHello
,現在完整的web.xml配置代碼如下 -
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name>SayHello Servlet</display-name> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>SayHello</servlet-name> <servlet-class>com.yiibai.SayHello</servlet-class> </servlet> <servlet-mapping> <servlet-name>SayHello</servlet-name> <url-pattern>/SayHello</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
到這里,項目的創建和配置就完成了,接下來來看看如何部署和運行項目。
4.啟動服務器並部署項目
要啟動服務器並部署項目。右鍵單擊項目 -> 運行方式(Run As…) - > MyEclipse服務器應用程序。
MyEclipse Tomcat的默認端口是8080
,如果您在系統上安裝了Oracle,則端口號可能會沖突而無法正常啟動,可以先改變MyEclipse Tomcat服務器的端口號。要更改端口號,請單擊瀏覽器圖標左側的啟動服務器圖標 -> myeclipse tomcat -> 配置服務器連接器 -> 將端口號更改為您想要的端口號,如:8088
-> 應用(Apply) -> 完成。
在彈出的對話框中,填寫新的端口號,然后點擊應用(Apply)-> OK , 如下圖所示 -
現在端口號已更改了。啟動服務器右鍵單擊項目 -> Run As -> MyEclipse Server Application。
可以看到項目的默認頁面是打開的,填寫一個名字,然后提交。如果程序沒有問題,打開瀏覽器,訪問這個項目的URL:http://localhost:8088/MyeclipseServlet/ ,應該會看到下面界面 -
在上面輸入框中,輸入一個字符串(名字):Maxsu
,然后提交,則應該會看到以下結果 -
到此,使用MyEclipse創建Servlet的介紹就結束了。
原文出自【易百教程】,商業轉載請聯系作者獲得授權,非商業轉載請保留原文鏈接:https://www.yiibai.com/servlet/creating-servlet-in-myeclipse-ide.html