servlet-Mylogin(老规矩,会一直修改的)

版权声明:转载请指明出处 https://blog.csdn.net/weixin_42321963/article/details/82628476
package com.servlet;

import com.xing.MyDb;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/game/Login")
public class Login extends HttpServlet {
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        //获取账号,密码
        String uaccount = req.getParameter("uaccount");
        String upass = req.getParameter("upass");
        //从数据库进行验证
        MyDb db=new MyDb("dt_game","root","");
        String[][]accs=db.selectInfo("accs","select * from accs","id","account","pass","regdate");
        boolean b=true;
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out=resp.getWriter();
        for(int i=0;i<accs.length;i++){
            for(int j=0;j<accs[i].length;j++){
                if(accs[i][1].equals(uaccount)&&accs[i][2].equals(upass)){
                    b=false;
                    out.print("<h3>欢迎"+uaccount+"<h3>");
                    out.print("<a href=\"show.jsp\">请进入游戏界面</a>");
                    break;
                }
            }
        }
        if(b){
            out.print("<h3>该账户不存在,请核对您的账号,若不存在,请先注册<h3>");
            out.print("<a href=\"/game/index.jsp\">返回主页面</a>");
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_42321963/article/details/82628476