溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

使用IDEA怎么模擬一個Servlet 網(wǎng)絡請求

發(fā)布時間:2021-04-09 17:09:32 來源:億速云 閱讀:214 作者:Leah 欄目:編程語言

這篇文章給大家介紹使用IDEA怎么模擬一個Servlet 網(wǎng)絡請求,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

首先下載 IntelliJ IDEA 集成工具

接下來配置 Tomcat 服務器,以 Mac 電腦為例,參考:Mac上tomcat服務器安裝配置 。

然后打開 IntelliJ IDEA ,選擇右邊的 Java Enterprise 項目類型,選擇剛裝的 Tomcat 服務器,勾選 Web Application 選項。

使用IDEA怎么模擬一個Servlet 網(wǎng)絡請求

新建工程

點選 next,輸入自定義工程名稱 demo:

使用IDEA怎么模擬一個Servlet 網(wǎng)絡請求

工程

然后我們就能看到新建工程的全貌:

使用IDEA怎么模擬一個Servlet 網(wǎng)絡請求

工程

至此,一個 Web 應用工程的框架已經(jīng)做好。但是要順利部署到 Tomcat 服務器,還需要我們添加處理服務的對象 Servlet。點擊 src 文件夾,添加 Servlet:

使用IDEA怎么模擬一個Servlet 網(wǎng)絡請求

servlet

Servlet 類中能看到默認生成的 doGet 和 doPost 方法:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    response.setCharacterEncoding("utf-8");
    response.setContentType("text/html");
    response.getWriter().print("收到 post 請求");

    String username = request.getParameter("username");
    String pwd = request.getParameter("password");

    if("admin".equals(username) && "abc123".equals(pwd)) {
      response.sendRedirect("/2.html");
    }
  }

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setCharacterEncoding("utf-8");//設置
    response.setContentType("text/html");

    String username = request.getParameter("username");
    String pwd = request.getParameter("password");
    if("admin".equals(username) && "abc123".equals(pwd)) {
      response.sendRedirect("/2.html");
    }
  }

要想使用新建的 Servlet 類,還需要在 web.xml 中進行配置:

<web-app ...>
  <servlet>
    <servlet-name>Servlet</servlet-name>
    <servlet-class>demo.Servlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Servlet</servlet-name>
    <url-pattern>/demo</url-pattern>
  </servlet-mapping>
</web-app>

其中 servlet-mapping 標簽設置對外訪問的路徑。

然后在 web 目錄下添加前端頁面文件,比如命名 1.html 作為起始頁面,2.html 作為跳轉的結果頁面。

使用IDEA怎么模擬一個Servlet 網(wǎng)絡請求

頁面

在 1.html 中編輯頁面布局,設置 head 標簽,在 body 標簽中添加 form表單。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" >
  <title>MyFirst</title>
  <script type="text/javascript">
  </script>
</head>
<body>

<h2>登錄頁面(get)</h2>
<form action="/demo" method="get">

  <table>
    <tr>
      <td>
        用戶名:
      </td>
      <td>
        <input type="text" name="username">
      </td>
    </tr>
    <tr>
      <td>
        密碼:
      </td>
      <td>
        <input type="text" name="password" type="hidden">
      </td>
    </tr>
    <tr>
      <td colspan="2" >
        <input type="submit" value="登錄">
      </td>
    </tr>
  </table>
</form>

<h2>登錄頁面(post)</h2>
<form action="/demo" method="post">
  <table>
    <tr>
      <td>
        用戶名:
      </td>
      <td>
        <input type="text" name="username">
      </td>
    </tr>
    <tr>
      <td>
        密碼:
      </td>
      <td>
        <input type="text" name="password" type="hidden">
      </td>
    </tr>
    <tr>
      <td colspan="2">
        <input type="submit" value="登錄">
      </td>
    </tr>
  </table>
</form>
</body>
</html>

2.html中編輯頁面:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
  <h2 >
    登錄成功?。。?
  </h2>
</body>
</html>

最后點擊 Debug 進行運行,部署到自己的 Tomcat 服務器上:

使用IDEA怎么模擬一個Servlet 網(wǎng)絡請求

Debug

最后在瀏覽器輸入網(wǎng)址: http://localhost:8080/1.html ,就能訪問我們部署的網(wǎng)站了。

使用IDEA怎么模擬一個Servlet 網(wǎng)絡請求

網(wǎng)站

打開 Chrome 的開發(fā)者工具,能夠看到發(fā)送請求的詳細情況:

使用IDEA怎么模擬一個Servlet 網(wǎng)絡請求

關于使用IDEA怎么模擬一個Servlet 網(wǎng)絡請求就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI