• servlet从xml提取数据登陆


    在xml文档下可以设置参数的初始值,在这里把他当成了数据库在用

      <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>InitParamServlet</servlet-name>
        <servlet-class>servlet.InitParamServlet</servlet-class>
        <init-param>
            <param-name>helloween</param-name>
            <param-value>password</param-value>
        </init-param>
            <init-param>
            <param-name>admin</param-name>
            <param-value>admin</param-value>
        </init-param>
            <init-param>
            <param-name>babyface</param-name>
            <param-value>babyface</param-value>
        </init-param>
      </servlet>
    View Code

    变量名为账号,值为密码

    在doGet中的<form>提供的<input type='text'  name='username'...>的值

    可以传递到doPost

    然后用String username=request.getParameter("username");来获取他

    再和xml里面的变量比较

     原码

    package 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 {
    
        /**
         * Constructor of the object.
         */
        public InitParamServlet() {
            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 {
            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.getRequestURI()+"' method='post'>");
            out.println("账号:<input type='text' name='username'<br><br/>");
            out.println("密码:<input type='password' name='password'<br><br/>");
            out.println("<input type='submit' value='  登陆    '>");
            out.println("</form>");
            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 {
            response.setCharacterEncoding("UTF-8");
            request.setCharacterEncoding("UTF-8");
    
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            String username=request.getParameter("username");
            String password=request.getParameter("password");
            Enumeration params=this.getInitParameterNames();
            for(;params.hasMoreElements();){
                String usernameParam=params.nextElement().toString();
                String passnameParam=getInitParameter(usernameParam);
                if(usernameParam.equalsIgnoreCase(username)&&passnameParam.equals(password)){
                    request.getRequestDispatcher("/WEB-INF/notice.html").forward(request,response);
                    return;
                }
            }
            doGet(request,response);//不匹配的话重新加载
        }
    
        /**
         * Initialization of the servlet. <br>
         *
         * @throws ServletException if an error occurs
         */
        public void init() throws ServletException {
            // Put your code here
        }
    
    }
    View Code
  • 相关阅读:
    SAP的PI日志查看工具
    微信小程序调用SAP发布的REST显示数据列表
    SAP发布REST/HTTP接口
    SAP的JSON没有双引号问题
    SAP扩展库位
    函数使用十一:BAPI_BANK_CREATE
    竟然有人在群里谈交钱培训PI。。。。等哥哥有时间,断了你们的财路
    FPM十一:点击POPUP显示明细
    WDA基础十八:Select option配置
    SAP常见查询组合
  • 原文地址:https://www.cnblogs.com/vhyc/p/6221028.html
Copyright © 2020-2023  润新知