zoukankan      html  css  js  c++  java
  • JavaWeb核心之Servlet

    JavaWeb核心之Servlet

    (1)Servlet接口中的方法

    1)init(ServletConfig config)

    何时执行:servlet对象创建的时候执行

    ServletConfig : 代表的是该servlet对象的配置信息

     

    2)service(ServletRequest request,ServletResponse response)

    何时执行:每次请求都会执行

    ServletRequest :代表请求 认为ServletRequest 内部封装的是                                                         http请求的信息

    ServletResponse :代表响应 认为要封装的是响应的信息

     

    3)destroy()

    何时执行:servlet销毁的时候执行

    (1)Servlet的生命周期(面试题)

    1)Servlet何时创建

    默认(服务器启动时)第一次访问servlet时创建该对象

     

    2)Servlet何时销毁

    服务器关闭servlet就销毁了

     

    3)每次访问必然执行的方法

    service(ServletRequest req, ServletResponse res)方法

     

    问题:对XXXServlet进行了10次访问,init(),destory(),service(),doGet(),doPost() 一共执行力多少次?request对象创建几个?response创建几个?

    init():一次 (创建时)        destory():一次(销毁时)          service():10次(运行一次创建一次)   doGet(),doPost()属于 service()

    request对象创建10个(运行一次创建一次)           response创建10个(运行一次创建一次)

    (1)HttpServlet类的方法

    1)init()

    2)doGet(HttpServletRequest request,HttpServletResponse response)

    3)doPost(HttpServletRequest request,HttpServletResponse response)

    4)destroy()

    Servlet的配置


    其中url-pattern的配置方式:

    1)完全匹配 访问的资源与配置的资源完全相同才能访问到、

    2)目录匹配 格式:/虚拟的目录../*   *代表任意



    3)扩展名匹配 格式:*.扩展名



    注意:第二种与第三种不要混用 /aaa/bbb/*.abcd(错误的)

    登录页面

    创建Dynamic Web项目

     在Webcontebt里新建jsp文件

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
      <form action="/WW/LoginServlet" method="post">
                 用户名:<input type="text" name="uname"><br>
                   密码:<input type="password" name="pwd"><br>
            <input type="submit" value="注册"><br>
        </form>
    </body>
    </html>
    action="/WW/LoginServlet"需要运行的地址
    LoginServlet页面
    public class LoginServlet extends HttpServlet {
    private UserService userService=new UserService();
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String uname=request.getParameter("uname");
            String pwd=request.getParameter("pwd");
            int row=userService.login(uname, pwd);
            if(row>0){
                response.getWriter().write("chenggong");;
            }else{
                response.getWriter().write("shibai");
            }
            
        }
    
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }
    }

    UserService页面

    public class UserService {
    private    UserDao userDao=new UserDao();
    public int login(String uname,String pwd){
        int row=0;
        try {
        row=userDao.login(uname, pwd);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return row;
    }
    }

    UserDao页面

    public class UserDao {
    public int login(String uname,String pwd) throws SQLException{
        Connection conn=JDBCUtils.getConn();
        String sql="INSERT INTO USER (uname,pwd) VALUES(?,?) ";
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setString(1, uname);
        pst.setString(2, pwd);
        int row=pst.executeUpdate();
        JDBCUtils.close(conn, pst);
        return row;
    }
    }

    JDBCUtils类

    public class JDBCUtils {
     //获取连接对象
        public static Connection getConn(){
            Connection conn=null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                String url="jdbc:mysql://localhost:3306/shop?characterEncoding=utf8";
                String uname="root";
                String ped="123456"; 
                conn=DriverManager.getConnection(url,uname,ped);
                
                
            } catch (ClassNotFoundException e) {
                
                e.printStackTrace();
            } catch (SQLException e) {
        
                e.printStackTrace();
            }
            return conn;
            
    }
        //释放资源方法
        public static void close(Connection conn,PreparedStatement pst){
            if(pst!=null){
                try {
                    pst.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                }
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
        }
        //查询
        public static void close(Connection conn,PreparedStatement pst,ResultSet rs){
            if(pst!=null){
                try {
                    pst.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(rs!=null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.getWriter().write("hello dandan...");//往客户端打印
    }

     

    public void init(ServletConfig arg0) throws ServletException {
    //获取ServletName
    System.out.println(arg0.getServletName());
    //获取初始化参数
    System.out.println(arg0.getInitParameter("driver"));
    //ServerContext con=arg0.getServletContext();
    System.out.println("Servlet创建了");

    }

     

  • 相关阅读:
    前端开发拥有属于自己的云服务器能做什么?
    C语言编译、链接和运行详解
    C程序运行机制概述
    Java之顺序查找
    C语言注释
    C转义字符
    Java之二维数组基本使用
    Java之类与对象基本使用
    分享些发表技术类文章的平台
    解决蓝奏云链接无法访问问题
  • 原文地址:https://www.cnblogs.com/111wdh/p/13473603.html
Copyright © 2011-2022 走看看