repuest轉發學習筆記一

dapan發表於2021-09-09

Java程式碼

import java.io.IOException;import java.io.InputStream;import java.util.Properties;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class DownServlet extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {        //獲取表單引數        String username = request.getParameter("username");        String password = request.getParameter("password");        //使用者名稱和密碼是否填了        if(username!=null && password!=null){            //載入檔案            InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/cn/itcast/web/request/config.properties");            Properties props = new Properties();            props.load(is);            //請求引數與檔案內容是否相同            if(props.getProperty(username).equals(password)){                //有許可權                response.setHeader("content-disposition","attachment;filename=a.JPG");            }else{                //轉發到message.html頁面                request                    .getRequestDispatcher("/message.html")                    .forward(request,response);            }        }else{            //轉發到login.html頁面            request                .getRequestDispatcher("/login.html")                .forward(request,response);        }    }    public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {        request.setCharacterEncoding("UTF-8");        this.doGet(request,response);    }}

config.properties
uername=liwen
password=123456

html程式碼

index.html

HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">      index.html              
HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">      使用者登入            
                                                                                                                                                                                       
使用者登入
使用者名稱
密碼
                                                                                    
    
  

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/758/viewspace-2813352/,如需轉載,請註明出處,否則將追究法律責任。

相關文章