YuFeng

tomcat服务器创建以及servlet小程序
tomcat文件下载:本地下载:tomcat.zipservlet小程序如果要在tomcat上运行需要实现一个接口...
扫描右侧二维码阅读全文
26
2018/12

tomcat服务器创建以及servlet小程序

tomcat文件下载:
本地下载:tomcat.zip
servlet小程序如果要在tomcat上运行需要实现一个接口Servlet.例:

package cn.feng.program;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class Test implements Servlet{

    @Override 
    public void destroy() {
        // 销毁方法,当服务器关闭此方法执行
        
    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        //初始化方法当Servlet创建时此方法执行,用于程序的初始化
        
    }

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        // 用于处理Servlet方法的接受资源处理之后返回相应资源
        //Request 相应用于接收
        //Response 用于返回相应的资源
        
    }

}

Servlet小程序的生命周期:
(1)Servlet接口中的方法
1)init(ServletConfig config)
何时执行:servlet对象创建的时候执行
ServletConfig : 代表的是该servlet对象的配置信息

2)service(ServletRequest request,ServletResponse response)
何时执行:每次请求都会执行
ServletRequest :代表请求 认为ServletRequest 内部封装的是 http请求的信息
ServletResponse :代表响应 认为要封装的是响应的信息

3)destroy()
何时执行:servlet销毁的时候执行

(2)HttpServlet类的方法
1)init()
2)doGet(HttpServletRequest request,HttpServletResponse response)
3)doPost(HttpServletRequest request,HttpServletResponse response)
4)destroy()

(3)Servlet的生命周期(面试题)
1)Servlet何时创建
默认第一次访问servlet时创建该对象

2)Servlet何时销毁
服务器关闭servlet就销毁了

3)每次访问必然执行的方法
service(ServletRequest req, ServletResponse res)方法

在开发中我们一般使用Servlet的实现类httpServlet作为父类来创建Servlet小程序.例用于来查找数据库相应的代码:

package cn.feng.program;

import java.io.IOException;
import java.sql.SQLException;

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

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import cn.yufeng.domain.User;

public class ServletLogin extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String name = request.getParameter("name");
            String password =  request.getParameter("password");
            QueryRunner qr = new QueryRunner(MyDatasource.getDataSource());
            String sql = "select * from users where name=?";
            User user = null;
            user = qr.query(sql, new BeanHandler<User>(User.class), name);
            if(user != null) {
                ServletContext servletContext = getServletContext();
                Integer num = (Integer) servletContext.getAttribute("num");
                num++;
                servletContext.setAttribute("num", num);
                response.getWriter().write(user.toString() + num);
            }else {
                response.getWriter().write("sorry this name or password is wrong");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
    
    @Override
    public void init() throws ServletException {
        ServletContext servletContext = getServletContext();
        servletContext.setAttribute("num", 0);
        System.out.println("servletContext 初始化成功");
    }
}

在http访问时需要配置相对项目的WEB-INF文件夹下的web.xml配置文件来执行相应的Servlet小程序x`xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>webuser</display-name>
  <servlet>
      <!-- 自定义类名称 -->
    <servlet-name>ServletLogin</servlet-name>    
    <!-- 类的限定名 -->
    <servlet-class>cn.yufeng.program.ServletLogin</servlet-class>    
  </servlet>
  <servlet-mapping>
      <!-- 自定义类名称需要和servlet-name相对应 -->    
    <servlet-name>ServletLogin</servlet-name>
    <!-- 访问此Servlet程序所需要的路径 -->    
    <url-pattern>/login</url-pattern>            
  </servlet-mapping>
</web-app>

相应简单的的html源码用于访问Servlet程序

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body>
    <form action="/webuser/login" method="get">
        用户名:<input type="text" name="name">
        密码:<input type="password" name="password">
        <input type="submit" value="提交"> 
    </form>
</body>
</html>
Last modification:June 11th, 2019 at 12:29 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment