1. 程式人生 > >servlet-Myloin(老規矩,會一直修改的)

servlet-Myloin(老規矩,會一直修改的)

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>");
        }
    }
}