winfwu

导航

Sevlet局部变量初始化

//java 代码部分
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>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.println("<form action='"+request.getRequestURL()+"' method='post'>"); out.println("帐号:<input type='text' name='username' style='width:200px'><br/>"); out.println("密码:<input type='password' name='password' style'width:200px'>"); out.println("<input type='submit' value='登录'> </form>"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); }
//验证正确后跳转notice.jsp 登录失败后执行this.doGet()方法 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 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>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); String username=request.getParameter("username"); String password=request.getParameter("password"); Enumeration params=this.getInitParameterNames(); while(params.hasMoreElements()){ String usernameParam=(String)params.nextElement(); String passwordParam=getInitParameter(usernameParam); if(usernameParam.equalsIgnoreCase(usernameParam) && passwordParam.equalsIgnoreCase(password)){ request.getRequestDispatcher("/WEB-INF/notice.jsp").forward(request, response);//前往 return; } } this.doGet(request, response); } }

web.xml部分

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>
  <servlet>
    <servlet-name>InitParamServlet</servlet-name>
    <servlet-class>com.servlet.InitParamServlet</servlet-class>
     <init-param>
        <param-name>user1</param-name>
        <param-value>123</param-value>
    </init-param>
      
       <init-param>
        <param-name>user2</param-name>
        <param-value>123</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>InitParamServlet</servlet-name>
    <url-pattern>/servlet/InitParamServlet</url-pattern>
  </servlet-mapping>    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

核心代码讲解:

Enumeration params=this.getInitParameterNames();//获取web.xml中设置的全部初始值
params.hasMoreElements()//遍历params

posted on 2013-12-16 21:25  winfwu  阅读(432)  评论(0编辑  收藏  举报