分類  >  Web前端 >

Servlet:透過初始參數實現許可權訪問某個文件、頁面

tags:    時間:2013-12-10 00:25:02
Servlet:通過初始參數實現許可權訪問某個文件、頁面

目錄結構


src 目錄下com.xieyuan包MyServlet.java文件(Servlet文件)

package com.xieyuan;  import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.PrintWriter; import java.net.URLEncoder; import java.util.Enumeration; import java.util.Random;  import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;  import com.sun.corba.se.impl.javax.rmi.CORBA.Util; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder;  public class MyServlet extends HttpServlet {  	/** 	 * Constructor of the object. 	 */ 	public MyServlet() { 		super(); 	}  	/** 	 * Destruction of the servlet. <br> 	 */ 	public void destroy() { 		super.destroy(); // Just puts "destroy" string in log 		// Put your code here 	}   	/** 	 * The doGet method of the servlet. <br> 	 * 	 * This method is called when a form has its tag value method equals to get. 	 *  	 * @param request the request send by the client to the server 	 * @param response the response send by the server to the client 	 * @throws ServletException if an error occurred 	 * @throws IOException if an error occurred 	 */ 	public void doGet(HttpServletRequest request, HttpServletResponse response) 			throws ServletException, IOException {          request.setCharacterEncoding("UTF-8");          response.setCharacterEncoding("UTF-8");                    response.setContentType("text/html");          PrintWriter out=response.getWriter();          out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");          out.println("<HTML>");          out.println("<HEAD><TITLE>請登錄查看NOTICE文件</TITLE></HEAD>");          out.println("<style>body,td,div{font-size:12px;}</style>");          out.println("<BODY>");                    String referer=request.getHeader("referer");          //通過獲取用戶上次進入的頁面判斷用戶是否輸入過密碼,然後跳轉到本頁來的          if(referer!=null&&referer.equals(request.getRequestURL().toString()))               out.println("<font color='red'>賬號密碼錯誤!</font>");          out.println("<form action='"+request.getRequestURI()+"' method='post'>");          out.println("賬號:<input type='text' name='username' autocomplete='off' style='width:200px' /><br/>");          out.println("密碼:<input type='password' name='password' style='width:200px' /><br/>");          out.println("<input type='submit' value='登錄' />");          out.println("</form>");                    //獲取上下文參數          out.println("<BR/>"+ this.getServletContext().getInitParameter("test"));          out.println("</BODY");          out.println("</HTML>");          out.flush();          out.close(); 	}  	/** 	 * The doPost method of the servlet. <br> 	 * 	 * This method is called when a form has its tag value method equals to post. 	 *  	 * @param request the request send by the client to the server 	 * @param response the response send by the server to the client 	 * @throws ServletException if an error occurred 	 * @throws IOException if an error occurred 	 */ 	public void doPost(HttpServletRequest request, HttpServletResponse response) 			throws ServletException, IOException { 		//獲得提交的數據 		String userName=request.getParameter("username"); 		String password=request.getParameter("password"); 		//遍歷所有初始參數參數 		Enumeration<?> params=this.getInitParameterNames(); 		while(params.hasMoreElements()) 		{ 			String paramName=(String)params.nextElement(); 			String paramValue=this.getInitParameter(paramName); 			if(paramName.equals(userName)&¶mValue.equals(password)) 			{ 				/* 				 * 一個RequestDispatcher對象作為資源在指定的路徑,或者為null的包裝, 				 * 如果servlet容器不能返回的RequestDispatcher 				 *                          將請求轉發到另一個資源(servlet,JSP和文件或HTML文件)從一個servlet在伺服器上。                         此方法允許一個servlet做初步處理的請求和其他資源來生成響應。  				 */ 				request.getRequestDispatcher("/WEB-INF/notice.txt").forward(request, response); 				return; 			} 			 		} 		//不匹配,返回 		doGet(request, response); 	} 	 	/** 	 * Initialization of the servlet. <br> 	 * 	 * @throws ServletException if an error occurs 	 */ 	public void init() throws ServletException { 	}  } 

web.xml

<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4"  	xmlns="http://java.sun.com/xml/ns/j2ee"  	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee  	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">   <servlet>     <description>This is the description of my J2EE component</description>     <display-name>This is the display name of my J2EE component</display-name>     <servlet-name>MyServlet</servlet-name>     <servlet-class>com.xieyuan.MyServlet</servlet-class>     <init-param>        <param-name>user123</param-name>        <param-value>pass123</param-value>     </init-param>          </servlet>      <context-param>       <param-name>test</param-name>       <param-value>這是測試數據!</param-value>   </context-param>     <servlet-mapping>     <servlet-name>MyServlet</servlet-name>     <url-pattern>/servlet/MyServlet</url-pattern>   </servlet-mapping>   <welcome-file-list>     <welcome-file>index.jsp</welcome-file>   </welcome-file-list> </web-app> 

部署好WEB后,訪問:http://127.0.0.1:8080/Test/servlet/MyServlet 進入登錄頁面

輸入正確的賬號:

user123
密碼

pass123
訪問到預先定義的WEB-INF/notice.txt頁面



推薦閱讀文章

Bookmark the permalink ,來源:互聯網