Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之12.Servlet基础(1)

本文详细介绍了Servlet的基本概念,包括提交表单的方法(get与post的区别),Servlet的生命周期,如何使用Servlet输出HTML页面,以及如何获取Servlet的初始化参数等内容。
–提交表单的方法
• 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分割线######################
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值