–提交表单的方法 
• get 
• post
 
–Servlet 生命周期 
–使用Servlet 输出HTML页面 
–获得Servlet初始化参数 
–页面导航 
• 请求重定向 
–response.sendRedirect(“url”);
 
• 请求转发 
–request.getRequestDispatcher(“url”).forward(request,response); 
• 请求包含 
–request.getRequestDispatcher(“url”).include(request,response);
############Michael分割线################
全局参数对全局生效,我们来测试一下
ServletBasic2.java
package com.michael.servlet;    

import java.io.IOException;    
import java.io.PrintWriter;    
import java.util.Enumeration;    

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

public  class ServletBasic2  extends HttpServlet {    

         /**    
         * Constructor of the object.    
         */
    
         public ServletBasic2() {    
                 super();    
        }    

         /**    
         * Destruction of the servlet. <br>    
         */
    
         public  void destroy() {    
                 super.destroy();  // Just puts "destroy" string in log    
                 // Put your code here    
        }    

         /**    
         * 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 {    

                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 GET method");    
                out.println("    </BODY>");    
                out.println("</HTML>");    
                out.flush();    
                out.close();    
        }    

        /**    
         * 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();    
        }    

        /**    
         * Initialization of the servlet. <br>    
         *    
         * @throws ServletException if an error occure    
         */
    
        public void init() throws ServletException {    
                Enumeration enu = this.getServletContext().getInitParameterNames();    
                while(enu.hasMoreElements()){    
                        String name = (String) enu.nextElement();    
                        String value = this.getServletContext().getInitParameter(name);    
                        System.out.println(name+":"+value);    
                }    
        }    


测试一下
image
image

–局部参数 
• web.xml 配置
image 
• 获得
image
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" >    
         < context-param >    
                 < param-name >driver </ param-name >    
                 < param-value >com.mysql.jdbc.Driver </ param-value >    
         </ context-param >    
         < context-param >    
                 < param-name >url </ param-name >    
                 < param-value >jdbc:mysql://localhost:3306/jdbc_db </ param-value >    
         </ context-param >    
     < servlet >    
         < servlet-name >ServletBasic </ servlet-name >    
         < servlet-class >com.michael.servlet.ServletBasic </ servlet-class >    
         < init-param >    
                 < param-name >username </ param-name >    
                 < param-value >michael </ param-value >    
         </ init-param >    
         < init-param >    
                 < param-name >password </ param-name >    
                 < param-value >111222 </ param-value >    
         </ init-param >    
     </ servlet >    
     < servlet >    
         < servlet-name >ServletBasic2 </ servlet-name >    
         < servlet-class >com.michael.servlet.ServletBasic2 </ servlet-class >    
     </ servlet >    

     < servlet-mapping >    
         < servlet-name >ServletBasic </ servlet-name >    
         < url-pattern >/servlet/ServletBasic </ url-pattern >    
     </ servlet-mapping >    
     < servlet-mapping >    
         < servlet-name >ServletBasic2 </ servlet-name >    
         < url-pattern >/servlet/ServletBasic2 </ url-pattern >    
     </ servlet-mapping >    

</ web-app > 
image
ServletBasic.java
package com.michael.servlet;    

import java.io.IOException;    
import java.io.PrintWriter;    
import java.util.Enumeration;    

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

public  class ServletBasic  extends HttpServlet {    

         /**    
         * Constructor of the object.    
         */
    
         public ServletBasic() {    
                 super();    
                System.out.println( "-----ServletBasic-----");    
        }    

         /**    
         * Destruction of the servlet. <br>    
         */
    
         public  void destroy() {    
                 super.destroy();    
                System.out.println( "-----destroy-----");    
        }    

         /**    
         * 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 {    
                System.out.println( "-----doGet-----");    

                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 GET method");    
                out.println("    </BODY>");    
                out.println("</HTML>");    
                out.flush();    
                out.close();    
        }    

        /**    
         * 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 {    
                System.out.println("-----doPost-----");    
                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();    
        }    

        /**    
         * Initialization of the servlet. <br>    
         *    
         * @throws ServletException if an error occure    
         */
    
        public void init() throws ServletException {    
                /*    
                //第一种方法    
                String driver = this.getServletContext().getInitParameter("driver");    
                String url = this.getServletContext().getInitParameter("url");    
                System.out.println(driver);    
                System.out.println(url);    
                //第二种方法    
                Enumeration enu = this.getServletContext().getInitParameterNames();    
                while(enu.hasMoreElements()){    
                        String name = (String) enu.nextElement();    
                        String value = this.getServletContext().getInitParameter(name);    
                        System.out.println(name+":"+value);    
                }    
                */
    
                String username = this.getInitParameter("username");    
                String password = this.getInitParameter("password");    
                System.out.println(username);    
                System.out.println(password);    
                System.out.println("-----init-----");    
        }    

        @Override    
        protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {    
                // TODO Auto-generated method stub    
                System.out.println("-----service-----");    
        }    

image
ServletBasic2.java
package com.michael.servlet;    

import java.io.IOException;    
import java.io.PrintWriter;    
import java.util.Enumeration;    

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

public  class ServletBasic2  extends HttpServlet {    

         /**    
         * Constructor of the object.    
         */
    
         public ServletBasic2() {    
                 super();    
        }    

         /**    
         * Destruction of the servlet. <br>    
         */
    
         public  void destroy() {    
                 super.destroy();  // Just puts "destroy" string in log    
                 // Put your code here    
        }    

         /**    
         * 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 {    

                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 GET method");    
                out.println("    </BODY>");    
                out.println("</HTML>");    
                out.flush();    
                out.close();    
        }    

        /**    
         * 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();    
        }    

        /**    
         * Initialization of the servlet. <br>    
         *    
         * @throws ServletException if an error occure    
         */
    
        public void init() throws ServletException {    
                /*    
                Enumeration enu = this.getServletContext().getInitParameterNames();    
                while(enu.hasMoreElements()){    
                        String name = (String) enu.nextElement();    
                        String value = this.getServletContext().getInitParameter(name);    
                        System.out.println(name+":"+value);    
                }    
                */
    
                String username = this.getInitParameter("username");    
                String password = this.getInitParameter("password");    
                System.out.println(username);    
                System.out.println(password);    
        }    


image
测试
image
image
image
现在调用第二个
image
image
• 页面导航 
– 请求重定向 
• response.sendRedirect(“url”); 
– 请求转发 
• request.getRequestDispatcher(“url”).forward(request,response); 
– 请求包含
 
• request.getRequestDispatcher(“url”).forward(request,response);
ServletBasic.java
package com.michael.servlet;    

import java.io.IOException;    
import java.io.PrintWriter;    
import java.util.Enumeration;    

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

public  class ServletBasic  extends HttpServlet {    

         /**    
         * Constructor of the object.    
         */
    
         public ServletBasic() {    
                 super();    
                System.out.println( "-----ServletBasic-----");    
        }    

         /**    
         * Destruction of the servlet. <br>    
         */
    
         public  void destroy() {    
                 super.destroy();    
                System.out.println( "-----destroy-----");    
        }    

         /**    
         * 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 {    
                String username = request.getParameter( "username");    
                String password = request.getParameter( "password");    
                System.out.println( "username="+username);    
                 if(username!= null&&username.equals( "redking")){    
                        request.getRequestDispatcher( "/successfull.html").forward(request,response);    
                } else{    
                        request.getRequestDispatcher( "/failure.html").forward(request,response);    
                }    
                 /*    
                System.out.println("-----doGet-----");    

                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 GET method");    
                out.println("    </BODY>");    
                out.println("</HTML>");    
                out.flush();    
                out.close();    
        */
         
        } 

         /**    
         * 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 {    
                System.out.println( "-----doPost-----");    
                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();    
        }    

        /**    
         * Initialization of the servlet. <br>    
         *    
         * @throws ServletException if an error occure    
         */
    
        public void init() throws ServletException {    
                /*    
                //第一种方法    
                String driver = this.getServletContext().getInitParameter("driver");    
                String url = this.getServletContext().getInitParameter("url");    
                System.out.println(driver);    
                System.out.println(url);    
                //第二种方法    
                Enumeration enu = this.getServletContext().getInitParameterNames();    
                while(enu.hasMoreElements()){    
                        String name = (String) enu.nextElement();    
                        String value = this.getServletContext().getInitParameter(name);    
                        System.out.println(name+":"+value);    
                }    
                */
    
                String username = this.getInitParameter("username");    
                String password = this.getInitParameter("password");    
                System.out.println(username);    
                System.out.println(password);    
                System.out.println("-----init-----");    
        }    
        /*    
        @Override    
        protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {    
                // TODO Auto-generated method stub    
                System.out.println("-----service-----");    
        }    
        */
    

image
successfull.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >    
< html >    
     < head >    
         < title >successfull.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 >    
         < h1 >successfull!!! </h1>    
     </body>    
</html> 
image
failure.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >    
< html >    
     < head >    
         < title >failure.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 >    
         < h1 >failure!!! </h1>    
     </body>    
</html> 
image
输入用户名为redking进行测试
image
显示successfull!!!
image
输入其他用户名测试
image
显示failure!!!
image
看下显示信息
image
############Michael分割线################