zl程序教程

您现在的位置是:首页 >  工具

当前栏目

JavaWeb 后端 <二> 之 Servlet 学习笔记

笔记学习Servlet gt lt javaweb
2023-09-14 08:58:20 时间


//转发:源

public class ServletContextDemo4 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        ServletContext sc = getServletContext();

        RequestDispatcher rd = sc.getRequestDispatcher("/servlet/ServletContextDemo5");//转发的地址。ServletContext得到的,地址必须以"/"开头,该"/"就代表着当前应用的访问路径/day07_01_servlet

        rd.forward(request, response);//转发

    }


//转发:目标

public class ServletContextDemo5 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        response.getWriter().write("I am five");

    }

 实现中文文件的下载


//实现中文文件的下载

public class ServletContextDemo6 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        //文件在哪儿?以不变应万变

        ServletContext sc = getServletContext();

        String realPath = sc.getRealPath("/WEB-INF/classes/霉女.jpg");//  文件存放的真实绝对路径

//      System.out.println(realPath);

        //构建文件的输入流

        InputStream in = new FileInputStream(realPath);

        //告知客户端以下载的方式打开:Content-Disposition=attachment;filename=27.jpg

         

        //获取要下载的文件名

         

        String filename  = realPath.substring(realPath.lastIndexOf(File.separator)+1);

         

        response.setHeader("Content-Type", "application/octet-stream");

        response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(filename,"UTF-8"));//中文属于不安全的字符,需要进行URL编码

         

        //用response的字节流进行输出

        OutputStream out = response.getOutputStream();

         

        int len = -1;

        byte b[] = new byte[1024];

        while((len=in.read(b))!=-1){

            out.write(b, 0, len);

        }

        in.close();

        out.close();

    }


 encode 编码

实例: 使用utf-8编码


import java.io.UnsupportedEncodingException;

import java.net.URLDecoder;

import java.net.URLEncoder;

import org.junit.Test;

public class UrlEncodeDemo {

    @Test

    public void test1() throws UnsupportedEncodingException{

        String s = "胡轩";

        System.out.println(URLEncoder.encode(s, "UTF-8"));

    }

    @Test

    public void test2() throws UnsupportedEncodingException{

        String s = "%E8%83%A1%E8%BD%A9";

        String v = URLDecoder.decode(s, "UTF-8");

        System.out.println(v);

    }

}

 读取配置文件的各种方式


//演示:读取配置文件的各种方式

public class ServletContextDemo7 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        test31(request, response);

    }

    //请不要把Tomcat等服务器装在有空格的目录中

    //类加载器读取:只能读取classes或者类路径中的任意资源。但是不适合读取特别大的资源。b c 

    private void test31(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        ClassLoader cl = ServletContextDemo7.class.getClassLoader();//得到类加载器

        URL url = cl.getResource("com/itheima/resources/c.properties");

        String path = url.getPath();

        InputStream in = new FileInputStream(path);

        Properties props = new Properties();

        props.load(in);

        System.out.println(props.getProperty("hello"));

    }

    //类加载器读取:只能读取classes或者类路径中的任意资源。但是不适合读取特别大的资源。b c 

    private void test30(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        ClassLoader cl = ServletContextDemo7.class.getClassLoader();//得到类加载器

//      InputStream in = cl.getResourceAsStream("b.properties");

        InputStream in = cl.getResourceAsStream("com/itheima/resources/c.properties");

        Properties props = new Properties();

        props.load(in);

        System.out.println(props.getProperty("hello"));

    }

     

    //利用ResourceBundle读取:b  c ,不能读a,只能读取properties的文件

    private void test20(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

//      ResourceBundle rb = ResourceBundle.getBundle("b");

        ResourceBundle rb = ResourceBundle.getBundle("com.itheima.resources.c");

        System.out.println(rb.getString("hello"));

    }

    //利用ServletContext读取:a b c

    //可以读取应用中任何位置上的资源。使用限制:只能在web应用中用

    private void test10(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

//      String path = getServletContext().getRealPath("/a.properties");

//      String path = getServletContext().getRealPath("/WEB-INF/classes/b.properties");

        String path = getServletContext().getRealPath("/WEB-INF/classes/com/itheima/resources/c.properties");

         

        InputStream in = new FileInputStream(path);

        Properties props = new Properties();

        props.load(in);

        System.out.println(props.getProperty("hello"));

    }