如何學好java

          如何學好java,其實很簡單,只要用心體會,慢慢積累!
          posts - 106, comments - 7, trackbacks - 0, articles - 3
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          寫一個最簡單的Servlet

          Posted on 2011-04-29 15:11 哈希 閱讀(264) 評論(0)  編輯  收藏 所屬分類: Servlet 總結
          Web.xml
          二話不說看代碼:
            
           
          <?xml version="1.0" encoding="UTF-8"?>
          <web-app version="2.4" 
              xmlns
          ="http://java.sun.com/xml/ns/j2ee" 
              xmlns:xsi
          ="http://www.w3.org/2001/XMLSchema-instance" 
              xsi:schemaLocation
          ="http://java.sun.com/xml/ns/j2ee 
              http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
            <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>ValidateView</servlet-name>
              
          <servlet-class>org.man.prj.ValidateView</servlet-class>
            
          </servlet>

            
          <servlet-mapping>
              
          <servlet-name>ValidateView</servlet-name>
              
          <url-pattern>/ValidateView</url-pattern>
            
          </servlet-mapping>
            
          <welcome-file-list>
              
          <welcome-file>index.jsp</welcome-file>
            
          </welcome-file-list>
          </web-app>

          package org.man.prj;

          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 ValidateView extends HttpServlet {

              
          /**
               * The doGet method of the servlet. <br>
               *
               * This method is called when a form has its tag value method equals to get.
               * 
               * 
          @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 doGet(HttpServletRequest request, HttpServletResponse response)
                      
          throws ServletException, IOException {

                  doPost(request, response);
              }

              
          /**
               * 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();
                  out.println(
          "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
                  out.println("<HTML>");
                  out.println(
          "  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
                  out.println(
          "  <BODY>");
                  out.print(
          "    This is ");
                  out.print(
          this.getClass());
                  out.println(
          ", using the POST method");
                  out.println(
          "  </BODY>");
                  out.println(
          "</HTML>");
                  out.flush();
                  out.close();
              }

          }
          輸入tomcat驗證一下:
          http://localhost:8888/Validate/ValidateView



          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 平乐县| 雷波县| 新密市| 上虞市| 绥滨县| 五家渠市| 临泉县| 应用必备| 怀仁县| 醴陵市| 楚雄市| 津南区| 普兰店市| 珲春市| 江安县| 贵州省| 舞钢市| 通城县| 政和县| 广宗县| 阿拉善左旗| 延吉市| 香港 | 罗甸县| 德安县| 桐城市| 兴安盟| 怀化市| 延庆县| 江陵县| 天祝| 揭阳市| 渭源县| 桐庐县| 甘洛县| 平安县| 巴林左旗| 蕲春县| 邛崃市| 蒙山县| 吉木乃县|