Javaweb——IDEA案例之servlet

Javaweb——IDEA案例之servlet

第三次上机:
1、新建一个类名为DemoServletConfig01的Servlet,该Servlet路径映射为/config01
2、新建一个类名为DemoServletConfig02的Servlet,该Servlet路径映射为/config02
3、分别在这两个Servlet中获取Servlet配置对象ServletCnfig并打印出来看它们是否是同一个对象
4、给Servlet类DemoServletConfig01配置初始化参数:{“name”:“张三”,“sex”:“男”},并分别在上面两个Servlet中获取它们
5、新建一个类名为DemoServletContext01的Servlet,该Servlet路径映射为/context01
6、新建一个类名为DemoServletContext02的Servlet,该Servlet路径映射为/context02
7、分别在这两个Servlet中获取web应用上下文对象ServletContext并打印出来看它们是否是同一个对象
8、给web应用配置初始化参数:{“loginid”:“tom”,“loginpwd”:“123”},并分别在上面两个Servlet中获取它们
9、在DemoServletContext01中将键值对{“msg”:“登录成功!”}存入ServletContext域对象中,在DemoServletContext02去获取上面这个键值对并输出
10、分别在src、web及WEB-INF目录下新建三个配置文件jdbc.properties,文件内容分别为:
jdbc01.driver=com.mysql.jdbc.Driver
jdbc01.url=jdbc:mysql:///localhost/jdbc
jdbc01.username=root
jdbc01.password=123
jdbc02.driver=com.mysql.jdbc.Driver
jdbc02.url=jdbc:mysql:///localhost/jdbc
jdbc02.username=root
jdbc02.password=123
jdbc03.driver=com.mysql.jdbc.Driver
jdbc03.url=jdbc:mysql:///localhost/jdbc
jdbc03.username=root
jdbc03.password=123
10、分别获取上面三个文件夹中的文件并输出其内容

ServletDemo01

package xyc.sjxy.servlet;
//代码全了
import javax.servlet.*;
import java.io.IOException;

public abstract class ServletDemo01 implements Servlet{
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        //servlet初始化时执行的方法,这个方法在整个servlet生命周期中只会执行一次
        //即当这个servlet创建时执行
    }

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

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("hello" );
        //核心方法,处理servlet http请求,业务处理代码写在这,e.g登陆验证方法
        //方法执行次数与请求次数相关
    }

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

    @Override
    public void destroy() {
        //当servlet生命结束时执行,也只会执行一次
    }
}

ServletDemo02

package xyc.sjxy.servlet;
//代码齐全
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;

@WebServlet(name="ServletDemo02",value = "/ServletDemo02")
public abstract class ServletDemo02 extends GenericServlet {
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println(" hello1"  );
    }
}

ServletContext01

package xyc.sjxy.servlet;
//代码齐全
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(value = "/Context01",initParams = {@WebInitParam(name="loginid",value = "tom"),@WebInitParam(name = "loginid",value = "123")})
public class ServletContext01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //打印servletContext这个实例对象
        //servletContext这个接口有一个实现是tomcat提供的一个web应用
        //一个web应用只有一个servletContext对象
        ServletContext servletContext01=this.getServletContext();
        System.out.println("servletContext01 = " + servletContext01);

        //读取web应用的全局初始化参数
        String loginid=servletContext01.getInitParameter("loginid");
        System.out.println("loginid = " + loginid);

        String loginpwd=this.getServletConfig().getInitParameter("loginpwd");
        servletContext01.getInitParameter("loginpwd");
        System.out.println("loginpwd = " + loginpwd);

        //设置一个键值对,由另一个servlet来读取,这里用了域对象
        servletContext01.setAttribute("msg","登陆成功");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

ServletContext02

在这里插入代码片

ServletConfig01

package xyc.sjxy.servlet;
//代码齐全
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ServletConfig01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletConfig ServletConfig=this.getServletConfig();
        //这个ServletConfig对象是由servlet容器tomcat的类的实例,由容器给封装了配置文件一个对象
        //servletconfigorg.apache.catlina.core.StandardWrapperFacade@4545ccdf

        System.out.println("ServletConfig=" + ServletConfig);
        System.out.println("ServletConfig.getServletName " + ServletConfig.getServletName());

        String name=ServletConfig.getInitParameter("name");
        System.out.println("name = " + name);

        String sex=ServletConfig.getInitParameter("sex");
        System.out.println("sex = " + sex);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
    }


ServletConfig02

package xyc.sjxy.servlet;
//代码齐全
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ServletConfig02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletConfig ServletConfig02=this.getServletConfig();

        //这个ServletConfig对象是由servlet容器tomcat的类的实例,由容器给封装了配置文件一个对象
        //servletconfigorg.apache.catlina.core.StandardWrapperFacade@4545ccdf

        System.out.println("ServletConfig02=" + ServletConfig02);
        System.out.println("ServletConfig02.getServletName " + ServletConfig02.getServletName());
        String ending02 = ServletConfig02.getInitParameter("encoding02");
        System.out.println("ending = " + ending02);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
    }


JdbcServlet

package xyc.sjxy.servlet;
//代码齐全
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

@WebServlet("/jdbc")
public class JdbcServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //读取web应用霞的资源文件,jdbc.properties

        ServletContext servletContext = this.getServletContext();
        Properties jdbcProp=new Properties();

        String root=servletContext.getRealPath("/");
        System.out.println("root="+root);
        String realPath=servletContext.getRealPath("/config/jdbc.properties");
        InputStream in=new FileInputStream(realPath);

        jdbcProp.load(in);

        //lamda表达式
        jdbcProp.forEach((k,v)->System.out.println(k+"="+v));

        //动态获取web的部署路径:也是web应用的根路径如/02或/03
        System.out.println("servletContext.getContextPath() = " + servletContext.getContextPath());
    }
}

HttpServletDemo03

package xyc.sjxy.servlet;
//代码齐全
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class HttpServletDemo03 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理get请求
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理post请求
        //this.doPost();
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理delete请求
        super.doDelete(req, resp);
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPut(req, resp);
    }
}

你可能感兴趣的:(Java,intellij-idea,java,intellij,idea)