package com.oracle.web;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.orecla.tools.JDBCUtils;
public class loginServlet extends HttpServlet {
public void init() throws ServletException {
//定义计数器
int countt = 0;
//获取ServletContext对象
ServletContext context = getServletContext();
context.setAttribute("countt", countt);
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1、获取前台传来的值,用户名和密码
String username = request.getParameter("username");
String password = request.getParameter("password");
//UserDao层开始
//2、连接数据库连接
Connection conn = JDBCUtils.get();
String sql = "select count(*) from user where uname = ? and pwd = ?";
int count = 0;
PreparedStatement pst = null;
ResultSet rs = null;
try {
pst = conn.prepareStatement(sql);
pst.setString(1, username);
pst.setString(2, password);
rs = pst.executeQuery();
while(rs.next()){
count = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
JDBCUtils.close(conn, pst, rs);
}
//UserDao层结束
//判断count 值给予给前台提示
if(count >0){
ServletContext context = getServletContext();
int countt = (int)context.getAttribute("countt");
countt++;
response.getWriter().write("success");//前台打印
response.getWriter().write("nishidi"+countt+"gerenfangwen");
context.setAttribute("countt", countt);
}else{
response.getWriter().write("false");//前台打印
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/WEB02/loginServlet" method="post">
用户名:<input type = "text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="提交">
</form>
</body>
</html>

