在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Linux/ 使用MyEclipse創(chuàng)建Servlet
Servlet web.xml welcome-file-list
Servlet從數(shù)據(jù)庫讀取記錄性能優(yōu)化
Servlet URL重寫帶參數(shù)
War文件
Web技術(shù)基礎(chǔ)
Servlet GenericServlet類
Servlet API
Servlet ServletInputStream類
使用MyEclipse創(chuàng)建Servlet
Servlet增刪改查
Servlet ServletConfig配置信息
Servlet Cookies
Servlet重定向
Servlet生命周期
Servlet HttpSession會話
Servlet HttpServlet類
Servlet注冊表單示例
Servlet表單隱藏字段
Servlet下載文件
Servlet教程
Servlet身份驗(yàn)證過濾器
Servlet ServletOutputStream類
Servlet HttpSession登錄注銷實(shí)例
Servlet啟動時(shí)加載
Servlet事件和監(jiān)聽器
使用Eclipse創(chuàng)建Servlet
Servlet ServletContextEvent事件
Servlet HttpSessionEvent統(tǒng)計(jì)在線用戶數(shù)實(shí)例
Servlet RequestDispatcher請求轉(zhuǎn)發(fā)
Servlet使用注釋
Servlet過濾器示例
Servlet過慮器
Servlet ServletContext配置信息
Servlet登錄注銷Cookies實(shí)例
Servlet工作流程
Servlet會話跟蹤
Servlet登錄實(shí)例
Servlet ServletRequest接口
Servlet ServletRequestEvent類和接口
Servlet入門程序
Servlet查詢搜索數(shù)據(jù)示例
Servlet FilterConfig應(yīng)用示例
Servlet顯示所有頭信息
Servlet屬性設(shè)置
使用NetBeans創(chuàng)建Servlet
Servlet接口實(shí)現(xiàn)
Servlet上傳文件

使用MyEclipse創(chuàng)建Servlet

如何在myeclipse IDE中創(chuàng)建Servlet?

要在myeclipse IDE中創(chuàng)建Servlet,可參考以下幾個(gè)步驟 -

  • 創(chuàng)建一個(gè)Web項(xiàng)目
  • 創(chuàng)建一個(gè)html文件
  • 創(chuàng)建一個(gè)servlet
  • 啟動MyEclipse tomcat服務(wù)器并部署項(xiàng)目

請依次按照以下步驟在MyEclipse IDE中創(chuàng)建servlet。步驟如下:

1.創(chuàng)建Web項(xiàng)目:

要創(chuàng)建一個(gè)Web項(xiàng)目,首先打開MyEclipse,單擊文件菜單 -> 新建 - > Web項(xiàng)目 ->填寫項(xiàng)目名稱,例如,要創(chuàng)建一個(gè)項(xiàng)目的名稱為:MyeclipseServlet 。

在打開的對話框中,填寫相關(guān)項(xiàng)目的信息,如下圖所示 -

點(diǎn)擊完成(Finish),完整的項(xiàng)目結(jié)構(gòu)如下圖所示 -

2.創(chuàng)建html文件

可以看到有一個(gè)名稱為:MyeclipseServlet項(xiàng)目被創(chuàng)建成功了,現(xiàn)在來瀏覽這個(gè)項(xiàng)目。

要創(chuàng)建一個(gè)html文件,請右鍵單擊WebRoot -> New - > HTML(Advanceed Templates) -> 填寫html文件名,例如:index.html -> 完成。

在彈出對話框中,填寫HTML文件的名稱:index.html,如下所示 -

下面對這個(gè)index.html文件中的代碼進(jìn)行簡單的修改,修改的結(jié)果如下所示 -

<!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.創(chuàng)建servlet

要創(chuàng)建Servlet,請單擊菜單文件(File) -> 新建(New) -> Servlet -> 填寫servlet名稱,例如: SayHello -> 選中doGet()方法復(fù)選框 -> 下一步(Next>) -> 完成。

填寫要創(chuàng)建Servlet的信息,這里要在com.yiibai包中,創(chuàng)建一個(gè)名稱為SayHello的Servlet,如下所示 -

Servlet配置的相關(guān)信息 -

可以看到一個(gè)名為SayHello.java的servlet文件被創(chuàng)建。接下來將在這個(gè)文件里編寫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過來的數(shù)據(jù)
        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();
    }
}

現(xiàn)在刪除項(xiàng)目中的index.jsp文件,并將index.html文件設(shè)置為項(xiàng)目的默認(rèn)頁面。打開web.xml文件,并將歡迎文件(<welcome-file>標(biāo)簽)名稱更改為index.html。同時(shí)修改SayHello的映射url為/SayHello,現(xiàn)在完整的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>

到這里,項(xiàng)目的創(chuàng)建和配置就完成了,接下來來看看如何部署和運(yùn)行項(xiàng)目。

4.啟動服務(wù)器并部署項(xiàng)目

要啟動服務(wù)器并部署項(xiàng)目。右鍵單擊項(xiàng)目 -> 運(yùn)行方式(Run As…) - > MyEclipse服務(wù)器應(yīng)用程序。

MyEclipse Tomcat的默認(rèn)端口是8080,如果您在系統(tǒng)上安裝了Oracle,則端口號可能會沖突而無法正常啟動,可以先改變MyEclipse Tomcat服務(wù)器的端口號。要更改端口號,請單擊瀏覽器圖標(biāo)左側(cè)的啟動服務(wù)器圖標(biāo) -> myeclipse tomcat -> 配置服務(wù)器連接器 -> 將端口號更改為您想要的端口號,如:8088 -> 應(yīng)用(Apply) -> 完成。

在彈出的對話框中,填寫新的端口號,然后點(diǎn)擊應(yīng)用(Apply)-> OK , 如下圖所示 -

現(xiàn)在端口號已更改了。啟動服務(wù)器右鍵單擊項(xiàng)目 -> Run As -> MyEclipse Server Application。

可以看到項(xiàng)目的默認(rèn)頁面是打開的,填寫一個(gè)名字,然后提交。如果程序沒有問題,打開瀏覽器,訪問這個(gè)項(xiàng)目的URL:http://localhost:8088/MyeclipseServlet/ ,應(yīng)該會看到下面界面 -

在上面輸入框中,輸入一個(gè)字符串(名字):Maxsu ,然后提交,則應(yīng)該會看到以下結(jié)果 -

到此,使用MyEclipse創(chuàng)建Servlet的介紹就結(jié)束了。