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

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

         /**    
         * Constructor of the object.    
         */
    
         public ServletBasic() {    
                 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 {    
                // Put your code here    
        }    


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 >ServletBasic </ servlet-name >    
         < servlet-class >com.michael.servlet.ServletBasic </ servlet-class >    
     </ servlet >    

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

</ web-app > 
测试页面
image
############Michael分割线################
• 提交表单的方法 
–提交表单的常用方法有两种 
• get 
–将请求参数显示在URL中 
–调用Servlet的doGet方法 
• post 
–不在URL终显示请求参数 
–调用Servlet的doPost方法
login.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >    
< html >    
     < head >    
         < title >login.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 >    
         < form  name ="f1"  id ="f1"  action ="/Servlet_Basic/servlet/ServletBasic"  method ="post" >    
             < table  border ="0" >    
                 < tr >    
                     < td >帐号: </td>    
                     < td > < input  type ="text"  name ="username"  id ="login" > </td>    
                 </tr>    
                 < tr >    
                     < td >密码: </td>    
                     < td > < input  type ="password"  name ="password"  id ="password" > </td>    
                 </tr>    
                 < tr >    
                     < td  colspan ="2"  align ="center" > < input  type ="submit"  value ="登录" > </td>    
                 </tr>    
             </table>    
         </form>    
     </body>    
</html> 
测试页面
image
现在将method设置为get
image
看下测试页面
image
get会将用户密码显示在url中,而post不会哈~
• Servlet 生命周期 
–Servlet生命周期的四个步骤 
• 加载实例化Servlet 
–创建一个Servlet实例 
• 调用init方法 
–仅被调用一次 
• 调用service方法(doGet或者doPost) 
–被调用多次(每次请求调用) 
• 调用destory方法 
–调用一次(销毁Servlet)
image
ServletBasic.java
package com.michael.servlet;    

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


输入信息进行登录
image
页面调用Servlet
image
image
现在我们再重新登录下,Servlet重新执行了一次doGet或者doPost方法,而不会重新构造及初始化Servlet
image
doGet或者doPost方法最终都会调用service方法,我们测试下,先覆盖service方法
ServletBasic.java
image
重新部署tomcat后执行登录操作
image
image
现在Servlet不再调用doGet或者doPost方法而直接调用service方法
• 使用Servlet 输出HTML页面 
–获得输出流 
• PrintWriter out = response.getWriter(); 
–输出HTML页面
image
• 获得Servlet初始化参数 
–获得全局参数 
• 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 >    
         < 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 >ServletBasic </ servlet-name >    
         < servlet-class >com.michael.servlet.ServletBasic </ servlet-class >    
     </ servlet >    

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

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

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 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);    
                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
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);    
                }    
                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
image 
###################Michael分割线######################