毕设准备---转发,重定向

毕设准备---转发,重定向_第1张图片


转发

地址栏的信息不会发生改变

转发代码

package com.wyh.servlet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author WuYongheng
 * @date 2022/8/29
 * @description
 */
public class ServletDemo3 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("进入了sd3");
        ServletContext context = this.getServletContext();
        RequestDispatcher requestDispatcher = context.getRequestDispatcher("/getc");//转发的请求路径
        requestDispatcher.forward(req, resp);//调用forward实现请求转发
    }

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

注册

  
    sd3
    com.wyh.servlet.ServletDemo3
  
  
    sd3
    /sd3
  

效果:

请求的是/sd3,转发到了/getc

毕设准备---转发,重定向_第2张图片

重定向

地址栏会发生改变

RedirectServlet

package com.wyh.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author WuYongheng
 * @date 2022/8/30
 * @description
 */
public class RedirectServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 重定向,所在项目的名称response_war
        resp.sendRedirect("/response_war/index.jsp");
    }

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

注册

    
        RedirectServlet
        com.wyh.servlet.RedirectServlet
    
    
        RedirectServlet
        /reb
    

效果

状态码302,重定向到index.jsp

毕设准备---转发,重定向_第3张图片


面试题

重定向和转发的区别?

相同点 不同点
页面都能实现跳转 地址栏的信息不会发生改变

地址栏会发生改变

状态码:请求转发307,重定向302

读取资源文件

Properties

  • 在Java目录下新建 properties
  • 在resources目录下新建 properties

发现都被打包到了同一路径下:classex,我们俗称这个路径为classpath

思路:需要一个文件流,把文件造出来。

Servlet代码

package com.wyh.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author WuYongheng
 * @date 2022/8/29
 * @description
 */
public class ServletDemo4 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/com/wyh/servlet/wyh.properties");

        Properties prop = new Properties();
        prop.load(in);
        String user = prop.getProperty("username");
        String pwd = prop.getProperty("password");
        resp.getWriter().print(user + ":" + pwd);
    }

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

去web.xml注册。。。

在当前项目的pom.xml中配置,保证 *.properties文件在java目录下也能导出


     
        
            
                src/main/resources
                
                    **/*.properties
                    **/*.xml
                
            
            
                
                src/main/java
                
                    **/*.properties
                    **/*.xml
                
                
                false
            
        
    

效果:

毕设准备---转发,重定向_第4张图片

你可能感兴趣的:(毕设,java,servlet,javascript)