posts - 0,comments - 0,trackbacks - 0
          不專業(yè),主要是自己以后忘記了可以拿出來(lái)看看。
          步驟:
          1.創(chuàng)建web工程。
          2.導(dǎo)入Servlet-api.jar包(主要:HttpServlet接口)
          3.創(chuàng)建請(qǐng)求頁(yè)面 Login.jsp
          Login.jsp

          4.創(chuàng)建LoginServlet。處理頁(yè)面發(fā)送過(guò)來(lái)的請(qǐng)求。(只輸出頁(yè)面請(qǐng)求參數(shù),兩種跳轉(zhuǎn)方式)

          LoginServlet.java
          package com.ay.login;

          import java.io.IOException;

          import javax.servlet.ServletException;
          import javax.servlet.http.HttpServlet;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;

          public class LoginServlet extends HttpServlet {
              
          /**
               * 
               
          */

              
          private static final long serialVersionUID = 1L;

              
          protected void doGet(HttpServletRequest request, HttpServletResponse response)
                      
          throws ServletException, IOException {
                      System.out.println(
          "登陸");
                      String name
          =request.getParameter("username");
                      String password
          =request.getParameter("password");
                      System.out.println(
          "用戶名:"+name);
                      System.out.println(
          "密碼:"+password);
                      
                      
          /**
                       * forward到頁(yè)面方法
                       
          */

                      
          //getServletConfig().(ServletConfig對(duì)象)獲取web.xml對(duì)于servlert的配置文件內(nèi)容
                      
          //getServletContext() 獲取servlet上下文
                      
          //getRequestDispatcher()請(qǐng)求分發(fā)器
                      getServletConfig().getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
                      
          /**
                       * response.sendRedirect重定向
                       
          */

                      
          //response.sendRedirect("/index.jsp");
              }


              
          protected void doPost(HttpServletRequest request, HttpServletResponse response)
                      
          throws ServletException, IOException {
                  
          this.doGet(request, response);
              }

              
          }


          5.在web.xml文件中添加servlet配置信息
            <servlet>
             <servlet-name>Login</servlet-name>
             <servlet-class>com.ay.login.LoginServlet</servlet-class>
            </servlet>
           
            <servlet-mapping>
             <servlet-name>Login</servlet-name>
             <url-pattern>/login</url-pattern>
            </servlet-mapping>

          一個(gè)簡(jiǎn)單JSP+Servlet例子就這樣可以了。

          表面的流程
          jsp發(fā)送請(qǐng)求-->servlet攔截-->回復(fù)請(qǐng)求
          posted on 2011-09-22 11:43 嘿傻妞▂↗ 閱讀(436) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 屏东县| 剑阁县| 申扎县| 盘锦市| 辽中县| 宜阳县| 皋兰县| 祥云县| 肇东市| 霍林郭勒市| 崇阳县| 涿鹿县| 崇信县| 桃园县| 乌兰浩特市| 宁乡县| 西畴县| 金沙县| 南木林县| 五峰| 贺州市| 麟游县| 沅江市| 搜索| 双牌县| 蒙城县| 乌鲁木齐县| 辉县市| 台北县| 三门峡市| 大港区| 阳山县| 甘谷县| 抚州市| 甘孜县| 武强县| 正蓝旗| 瑞昌市| 彝良县| 洛扎县| 留坝县|