/** * Constructor of the object. */ public LoginServlet() { 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 { System.out.println("执行 doGet 方法..."); // //1.接收前台传递过来的参数 // Enumeration enums = request.getParameterNames(); // while(enums.hasMoreElements()){ // System.out.println(enums.nextElement()); // // } //转换编码的第2种方式,配合doPost()方法使用 request.setCharacterEncoding("GBK"); //提交的name可以在后台使用request.getParameter("loginname")获取值 String loginname = request.getParameter("loginname"); System.out.println("转换前loginname:" + loginname); //String password = request.getParameter("password"); //把loginname这个字符串转成GBK,前提你要确定编码 loginname = new String(loginname.getBytes("iso-8859-1"),"GBK"); System.out.println("转换后loginname:" + loginname); String password = request.getParameter("password"); //properties文件是java的默认配置文件,以key-value的形式存储数据 //增加了一个user.properties文件存储用户名密码 Properties pro = new Properties(); //load方法从输入流中读取属性列表(键和元素对) pro.load(this.getClass().getResourceAsStream("/user.properties")); //System.out.print(pro); response.setContentType("text/html;charset=GBK"); 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.print(" loginname: " + loginname); //out.print(" password: " + password); if(loginname.equals(pro.getProperty("loginname")) && password.equals(pro.getProperty("password"))){ out.println(" 欢迎["+pro.getProperty("username")+"]登陆"); }else{ out.println("用户名密码错误"); } 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 {
this.doGet(request, response); }
/** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here }