JAVA基础之ServletContext应用
创建一个登陆的界面,并且统计次数!
导入jar包;
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 { // 获取ServletContext对象 ServletContext context = getServletContext(); // 定义计数器 int count = 0; // 将计数器存入ServletContext对象中 context.setAttribute("count", count); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取请求参数 String uname = request.getParameter("username"); // 获取密码 String pwd = request.getParameter("pwd"); // 调用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); //赋值 pst.setString(1, uname); pst.setString(2, pwd); //执行sql ResultSet rs=pst.executeQuery(); //处理结果集 int row =0; 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>
(自动)
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; //创建properties集合 Properties pro=new Properties(); try { // //明确数据源 // FileInputStream fis=new FileInputStream("src/db.properties"); // //将properties中的减值对读取到properties集合中 // pro.load(fis); //1.注册驱动(静态方法)(包名+类名) Class.forName("com.mysql.jdbc.Driver"); //2.获取连接对象(导包都导sql里面的,不导jdbc里的;多态!报异常是因为用户输入的串可能写错)后面设置下数据格式 String url="jdbc:mysql://localhost:3306/java0603?useUnicode=true&characterEncoding=UTF-8"; String user="root"; String password="123456"; conn=DriverManager.getConnection(url,user,password); } catch (ClassNotFoundException | SQLException e) { // TODO Auto-generated catch block 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>