關於servlet的一個簡單應用

自己開發一個servlet,要求在網頁中輸出自己的名字

思路:

1.     在webapps中創建一個文件夾huangxuanheng,並新建一個web.xml(可以從root中複製過來)

2.     在WEB-INF下創建classes創建自己的servlet--àMyServlet該類實現Servlet接口和lib文件夾

3.     開發MyServlet

packagecom.servlet;

 

importjavax.servlet.*;

importjavax.servlet.http.*;

import java.io.*;

 

public classMyServlet implements Servlet

{

       //初始化servlet,該函數只被調用一次

       public void init(ServletConfigconfig)throws ServletException

       {

      

       }

       //獲取ServletConfig對象

       public ServletConfig getServletConfig()

       {

              return null;

       }

       //該函數是服務函數,用來寫業務邏輯

       public void service(ServletRequestreq,ServletResponse res)throws ServletException,IOException

       {

              System.out.println(MyServlet.class.getName()+"====黃賢亨");

              res.getWriter().println(MyServlet.class.getName()+"====黃賢亨");

       }

 

       public String getServletInfo()

       {

              return null;

       }

 //銷燬servlet

       public void destroy()

       {

      

       }

}

 

4.     根據servlet部署web.xml

<?xmlversion="1.0" encoding="ISO-8859-1"?>

<!--

 Licensed to the Apache Software Foundation(ASF) under one or more

  contributor license agreements.  See the NOTICE file distributed with

  this work for additional informationregarding copyright ownership.

  The ASF licenses this file to You under theApache License, Version 2.0

  (the "License"); you may not usethis file except in compliance with

  the License. You may obtain a copy of the License at

 

     http://www.apache.org/licenses/LICENSE-2.0

 

  Unless required by applicable law or agreedto in writing, software

  distributed under the License is distributedon an "AS IS" BASIS,

  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied.

  See the License for the specific languagegoverning permissions and

  limitations under the License.

-->

 

<web-appxmlns="http://java.sun.com/xml/ns/javaee"

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

   version="2.5">

 <servlet>

     <servlet-name>MyServlet</servlet-name>

      <servlet-class>com.servlet.MyServlet</servlet-class>

    </servlet>

 

  <servlet-mapping>

       <servlet-name>MyServlet</servlet-name>

       <url-pattern>/huangxuanheng</url-pattern>

    </servlet-mapping>

 

</web-app>

 

5.     編譯MyServlet,啓動tomcat

6.     測試:

在網址欄中輸入:

http://localhost:8080/huangxuanheng/huangxuanheng

 

測試結果:       

                      

至於爲什麼出現:???,我想應該是編碼問題,可能我用的是UFT-8編碼,那如何解決這問題呢?也就是該怎麼操作,才能顯示中文字樣在網頁上呢?

問題是這樣解決的,把迴應對象設置爲GBK 編碼就OK了

即修改方法

public void service(ServletRequest req,ServletResponse res)throws ServletException,IOException
 {
  System.out.println(MyServlet.class.getName()+"====黃賢亨");

//設置編碼爲GBK編碼
  res.setCharacterEncoding("gbk");
  res.getWriter().println(MyServlet.class.getName()+"====黃賢亨");
 }

重新啓動tomcat,刷新後的結果爲:

 

 

 

 

 

發佈了66 篇原創文章 · 獲贊 13 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章