标签:
1初始化参数init-param
init-param是配置在web.xml的<servlet>标签里的,也就是说,是归该servlet单独所有的。
实例
<servlet> <servlet-name>ContextParamServlet</servlet-name> <servlet-class>com.servlet.ContextParamServlet</servlet-class> </servlet> <servlet> <servlet-name>InitParamServlet</servlet-name> <servlet-class>com.servlet.InitParamServlet</servlet-class> <init-param> <param-name>harry</param-name> <param-value>3324</param-value> </init-param> <init-param> <param-name>kitty</param-name> <param-value>3124</param-value> </init-param> <init-param> <param-name>rixiang</param-name> <param-value>1992</param-value> </init-param> </servlet>
servlet程序中读取:
package com.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 InitParamServlet extends HttpServlet { private static final long serialVersionUID = 1L; 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("<!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>"); out.println("<form action=‘" + request.getRequestURI() + "‘ method=‘post‘>"); out.println("帐号:<input type=‘text‘ name=‘username‘ style=‘width:200px; ‘> <br/>"); out.println("密码:<input type=‘password‘ name=‘password‘ style=‘width:200px; ‘> <br/><br/>"); out.println("<input type=‘submit‘ value=‘ 登录 ‘>"); out.println("</form>"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{ String username = request.getParameter("username"); String password = request.getParameter("password"); @SuppressWarnings("rawtypes") Enumeration params = this.getInitParameterNames(); while(params.hasMoreElements()){ String usernameParam = (String)params.nextElement(); String passnameParam = this.getInitParameter(usernameParam); if(usernameParam.equalsIgnoreCase(username) && passnameParam.equals(password)){ request.getRequestDispatcher("/WEB-INF/notice.html").forward(request, response); return; } } this.doGet(request, response); } }
效果:
2上下文参数,context-param
context-param是所有servlet公有的
<context-param> <param-name>upload folder</param-name> <param-value>attachment</param-value> </context-param> <context-param> <param-name>allowed file type</param-name> <param-value>.gif,.jpg,.bmp</param-value> </context-param>
servlet程序读取:
package com.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ContextParamServlet extends HttpServlet{ private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{ 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>读取文档参数</TITLE></HEAD>"); out.println(" <link rel=‘stylesheet‘ type=‘text/css‘ href=‘../css/style.css‘>"); out.println(" <BODY>"); out.println("<div align=center><br/>"); out.println("<fieldset style=‘width:90%‘><legend>所有的文档参数</legend><br/>"); ServletContext servletContext = this.getServletConfig().getServletContext(); String uploadFolder = servletContext.getInitParameter("upload folder"); String allowedFileType = servletContext.getInitParameter("allowed file type"); out.println("<div class=‘line‘>"); out.println(" <div align=‘left‘ class=‘leftDiv‘>上传文件夹</div>"); out.println(" <div align=‘left‘ class=‘rightDiv‘>" + uploadFolder + "</div>"); out.println("</div>"); out.println("<div class=‘line‘>"); out.println(" <div align=‘left‘ class=‘leftDiv‘>实际磁盘路径</div>"); out.println(" <div align=‘left‘ class=‘rightDiv‘>" + servletContext.getRealPath(uploadFolder) + "</div>"); out.println("</div>"); out.println("<div class=‘line‘>"); out.println(" <div align=‘left‘ class=‘leftDiv‘>允许上传的类型</div>"); out.println(" <div align=‘left‘ class=‘rightDiv‘>" + allowedFileType + "</div>"); out.println("</div>"); out.println("</fieldset></div>"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } }
效果
3资源注射@Resource
不需要servlet主动去读取,tomcat启动时会把web.xml里配置的信息主动注射到servlet里。
只需要@Resource注解。
<env-entry> <env-entry-name>hello</env-entry-name> <env-entry-type>java.lang.String</env-entry-type> <env-entry-value> Hello, Welcome to the JavaEE Resource Injection. </env-entry-value> </env-entry> <env-entry> <env-entry-name>i</env-entry-name> <env-entry-type>java.lang.Integer</env-entry-type> <env-entry-value>30</env-entry-value> </env-entry> <env-entry> <env-entry-name>persons</env-entry-name> <env-entry-type>java.lang.String</env-entry-type> <env-entry-value> Helloween, Cobain, Roses, Axl, </env-entry-value> </env-entry>
servlet程序读取
package com.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.annotation.Resource; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class InjectionServlet extends HttpServlet{ private static final long serialVersionUID = -8526907492073769090L; // 注入的 字符串 private @Resource(name="hello") String hello; // 注入的 整数 private @Resource(name="i") int i; // 注入更常见的写法 @Resource(name="persons") private String persons; 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("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>资源注入</TITLE></HEAD>"); out.println("<style>body {font-size:12px; }</style>"); out.println("<b>注入的字符串</b>:<br/> - " + hello + "<br/>"); out.println("<b>注入的整数</b>:<br/> - " + i + "<br/>"); out.println("<b>注入的字符串数组</b>:<br/>"); for(String person : persons.split(",")){ out.println(" - " + person + "<br/>"); } out.println(" <BODY>"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } }
效果
标签:
原文地址:http://www.cnblogs.com/rixiang/p/5150569.html