读取web.xml的配置参数

1.新建Servlet:

package com.wch.sp.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RequestServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public RequestServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. 
*/ public void destroy() { super.destroy(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("utf-8"); request.setCharacterEncoding("utf-8"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(""); out.println(""); out.println(" 请登录查看秘密文件"); out.println(" "); out.println(request.getRequestURI()); String requestURI=request.getRequestURI(); out.print("
"); out.print("帐号:
"); out.print("密码:
"); out.println(""); out.println("
"); out.println(" "); out.println(""); out.flush(); out.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); String username = request.getParameter("username"); String password= request.getParameter("password"); Enumeration params=this.getInitParameterNames(); while(params.hasMoreElements()) { String usernameParame = params.nextElement().toString(); String passwordParame =this.getInitParameter(usernameParame); if(username.equals(usernameParame)&&password.equals(passwordParame)) { request.getRequestDispatcher("/WEB-INF/noticde.html").forward(request, response); return; } } this.doGet(request, response); } /** * Initialization of the servlet.
* * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } }

2.配置web.xml文件:



  
    index.jsp
  
  
    
    FirstServlet
    com.wch.sp.servlet.FirstServlet
    
    
        message
        utf-8
    
    
    1

  
    RequestServlet
    com.wch.sp.servlet.RequestServlet
    
 		admin
 		admin
 	
 	
 		babyface
 		123
 	
  



    FirstServlet

    /servlet/firstServlet


  RequestServlet
  /servlet/RequestServlet  



3.在WEB-INF下面建立noticde.html



  
    noticde.html
	
    
    
    
    
    

  
  
  
    HELLO
  


4.发布项目至服务器,启动服务器,访问: http://localhost:8080/Servlet/servlet/RequestServlet

结果为:


登录后:


总结:

         1.在web.xml配置文件中配置Servlet的时候,可以给Servlet配置初始化参数,使用标签类配置。配置完毕之后,Servlet提供方法:getInitParameterNames()或者getInitParameterName(String name)方法获取初始化参数值。这些初始化参数也可以使用ServletConfig对象获取,Servlet提供getServletConfig()方法获取ServletConfig对象。由ServletConfig取初始化参数与Servlet直接取方式一样。

        2.初始化参数的好处就是可以把某些变量拿到web.xml中配置,需要修改时只需要修改web.xml文件中重启服务器即可,而不是需要修改Servlet类

        3.由此扩展到上下文参数():由于是配置在标签里的,只能由这个Servlet来读取,因此它不是全局参数,不能被其它的Servlet读取,这时可以配置上下文参数。获取context-param可以使用ServletContext对象,Servlet中可以通过getServletConfig().getServletContext()来获取一个ServletContext对象,使用ServletContext的getInitParameter()方法来获取指定名称的参数,通过getInitParameters()获取所有的context-param参数名称

你可能感兴趣的:(Servlet)