Jsp-Servlet 那一大堆事儿--1

为毛全局变量声明时初始化在try内不能用?

import javax.servlet.http .*;
import java.io.*;
import javax.servlet.*;
import java.sql.*;
public class LoginServlet extends HttpServlet{
    public void service(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
        String errMsg = "";
        RequestDispatcher rd;
        String username = "";
        username = request.getParameter("username");
        String pass = "";
        pass = request.getParameter("pass");
        try{
            DbDao dd = new DbDao("com.mysql.jdbc.Driver","jdbc:mysql://localhost:3306/poetry","root","root");
            ResultSet rs = dd.query("select password from t_account where account = ?",username);
            if(rs.next()){
                if(rs.getString("password").equals(pass)){
                    HttpSession session = request.getSession(true);
                    session.setAttribute("name",username);
                    rd = request.getRequestDispatcher("/welcome.jsp");
                    rd.forward(request,response);
                }else{
                    errMsg += "您的用户名密码不符合,请重新输入";
                }
            }else{
                errMsg += "您的用户名不存在,请先注册";
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        if(errMsg != null &&  !errMsg.equals("")){
            rd = request.getRequestDispatcher("/login.jsp");
            request.setAttribute("err",errMsg);
            rd.forward(request,response);
        }
    }
}

 

posted @ 2016-11-02 21:27  limeOracle  阅读(282)  评论(0编辑  收藏  举报