Servlet3.0注解init-param

Servlet3.0    默认不在有web.xml    可以在创建Servlet是第三步把勾打上生成web.xml,            


3.0中采用@webServlet注解的方式来配置一些相应的参数,如下代码将如何使用3.0的新特性来访问init-param


package com.study.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet(value="/LoginServlet",initParams={
		@WebInitParam(name = "default_market", value = "NASDAQ"),	
		@WebInitParam(name = "user_market", value = "userNASDAQ")	
})
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	     String default_market=	getInitParameter("default_market");
	     String user_market=	getInitParameter("user_market");
		response.getWriter().print("doPost:user_market:"+user_market+" or default_market:"+default_market);
		
	}

}


你可能感兴趣的:(JAVA学习笔记)