⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 login.jsp

📁 1
💻 JSP
字号:
<%@ page contentType="text/plain; charset=UTF-8"%>
<%@ page language="java"%>
<%@ page import="java.sql.*,javax.naming.*,javax.sql.*"%>
<%
    out.clear();                                        //清空当前的输出内容(空格和换行符)
    request.setCharacterEncoding("UTF-8");              //设置请求字符集为UTF-8

    String username = request.getParameter("username");       //获取
	String password = request.getParameter("password"); 
    String sql = "select count(*),username from user where username=? and password=? Group by username";   //定义查询数据库的SQL语句
    Connection conn = null;                 //声明Connection对象
    PreparedStatement pstmt = null;         //声明PreparedStatement对象
    ResultSet rs = null;                    //声明ResultSet对象
	String jndi="jdbc/news";
	Context initContext = null;
    try {
        initContext = new InitialContext();
        Context envContext = (Context) initContext.lookup("java:/comp/env");
        DataSource ds = (DataSource) envContext.lookup(jndi);
        conn = ds.getConnection();   					 //获取数据库连接
        pstmt = conn.prepareStatement(sql); 			//根据sql创建PreparedStatement
        pstmt.setString(1, username);          			
		pstmt.setString(2, password); 
        rs=pstmt.executeQuery();  
		int i=0;          		  //执行insert操作
		while(rs.next()){
			i=rs.getInt(1);
			session.setAttribute("username",rs.getString(2));
		}
		out.print(i);
    } catch (SQLException e) {
        System.out.println(e.toString());
    } finally {
	if(rs!=null)
        rs.close();  
	if(pstmt!=null)	                //关闭结果集
        pstmt.close();               //关闭PreparedStatement
	if(conn!=null)
        conn.close();                //关闭连接
    }
%>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -