Create a login interface, and count the number!
Introducing jar package;
1、
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/java0603?useUnicode=true&characterEncoding=UTF-8 username=root password=123456
2、
package com.oracle.web; import java.io.IOException; 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.oracle.service.UserService; public class LoginServlet extends HttpServlet { private UserService userService = new UserService(); public void init() throws ServletException { //Get ServletContext object ServletContext context = GetServletContext (); // custom counter int COUNT = 0 ; // counter ServletContext object stored in context.setAttribute ( "COUNT" , COUNT); } protected void the doGet (the HttpServletRequest Request, the HttpServletResponse Response) throws ServletException, IOException { // get the request parameter String uname = request.getParameter ( "username" ); // get the password String pwd = request.getParameter ( "pwd" ); // call the login method Service int row = userService.loginUser(uname, pwd); if (row > 0) { // 登录成功 // 获取ServletContext对象 ServletContext context = getServletContext(); int count = (int) context.getAttribute("count"); count++; context.setAttribute("count", count); response.getWriter().write("you are the " + count + " person success"); } else { // 登录失败 response.getWriter().write("fail"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
3、
package com.oracle.service; import java.sql.SQLException; import com.oracle.dao.UserDao; public class UserService { private UserDao userDao = new UserDao(); // 用户登录 public int loginUser(String uname, String pwd) { int row = 0; try { row = userDao.loginUser(uname, pwd); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return row; } }
4、
package com.oracle.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.oracle.tools.JDBCUtils; public class UserDao { //用户登录 public int loginUser(String uname,String pwd) throws SQLException{ Connection conn=JDBCUtils.getConn(); String sql="select count(*) from user where uname=? and pwd=?"; PreparedStatement pst=conn.prepareStatement(sql); // Assignment pst.setString (. 1 , the uname); pst.setString ( 2 , pwd); // execute SQL the ResultSet RS = pst.executeQuery (); // result set int Row = 0 ; the while (rs.next ( )) { Row = rs.getInt (. 1 ); } return Row; } }
5、
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>WEB04</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <description></description> <display-name>MyServlet</display-name> <servlet-name>MyServlet</servlet-name> <servlet-class>com.oracle.demo01.MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/MyServlet</url-pattern> </servlet-mapping> <servlet> <description></description> <display-name>LoginServlet</display-name> <servlet-name>LoginServlet</servlet-name> <servlet-class>com.oracle.web.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/LoginServlet</url-pattern> </servlet-mapping> <servlet> <description></description> <display-name>Servlet01</display-name> <servlet-name>Servlet01</servlet-name> <servlet-class>com.oracle.demo01.Servlet01</servlet-class> </servlet> <servlet-mapping> <servlet-name>Servlet01</servlet-name> <url-pattern>/Servlet01</url-pattern> </servlet-mapping> <servlet> <description></description> <display-name>Servlet02</display-name> <servlet-name>Servlet02</servlet-name> <servlet-class>com.oracle.demo01.Servlet02</servlet-class> </servlet> <servlet-mapping> <servlet-name>Servlet02</servlet-name> <url-pattern>/Servlet02</url-pattern> </servlet-mapping> <servlet> <description></description> <display-name>Serlvlet03</display-name> <servlet-name>Serlvlet03</servlet-name> <servlet-class>com.oracle.demo01.Serlvlet03</servlet-class> </servlet> <servlet-mapping> <servlet-name>Serlvlet03</servlet-name> <url-pattern>/Serlvlet03</url-pattern> </servlet-mapping> </web-app>
(automatic)
6、
package com.oracle.tools; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; public class JDBCUtils { //获取连接对象的方法(静态的) // public static void main(String[] args) { // Connection conn=getConn(); // System.out.println (Conn); // } public static Connection getConn () { Connection Conn = null ; // Create a set of properties the Properties Pro = new new the Properties (); the try { // // clear the data source // the FileInputStream FIS the FileInputStream new new = ( "the src / the db.properties"); // // the properties of the impairment reading to the properties set // pro.load (FIS); // 1. Register drive (static method) ( package name + class name) the Class.forName ( "com.mysql.jdbc.Driver"); // 2. Get the connection object (sql guide package are turned inside, in the non-conducting jdbc; polymorphism reported abnormal because the string entered by the user may be wrong) is provided behind the data format! String URL = "jdbc: MySQL: // localhost: 3306 / java0603 useUnicode = & characterEncoding = UTF-to true. 8 "? ; String User =" the root " ; String password =" 123456 " ; Conn = the DriverManager.getConnection (URL, User, password); } the catch (a ClassNotFoundException | SQLException E) { // the TODO Auto-Generated Block the catch e.printStackTrace (); } return conn; } //释放资源 public static void close(Connection conn,Statement sta){ if(sta!=null){ try { sta.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(conn!=null){ try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //释放资源2 public static void close(Connection conn,Statement sta,ResultSet rs){ if(rs!=null){ try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(sta!=null){ try { sta.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(conn!=null){ try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
7、
<%@ 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="/WEB04/LoginServlet" method="post"> 用户名:<input type="text" name="username"><br> 密码;<input type="password" name="pwd"><br> <input type="submit" value="登录"> </form> </body> </html>