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>
JAVA基础之ServletContext应用的更多相关文章
- JAVA基础之ServletContext对象
个人理解: ServletContext类似字节码文件对象,在web创建的时候就自动生成了,并且是唯一的,跟随着项目和服务器共存亡了.通过这个对象,我们可以向里面存数据(键值对),也可以通过别的Se ...
- Java基础部分 2
一. Java基础部分 2 1.一个".java"源文件中是否可以包括多个类(不是内部类)?有什么限制? 2 2.Java有没有goto? 2 3.说说&和&&am ...
- 精心收集java基础106条
Java基础 1.一个".java"源文件中是否可以包括多个类(不是内部类)?有什么限制? 一个Java源文件中可以定义多个类,但最多只能定义一个public的类,并且public ...
- (Java基础--Spring阶段)常见面试题题目及解析整理(2021.03.12)
题目整理 Java基础进阶阶段 基础概念类 1.JDK1.8新特性? 2.面向对象和面向过程的区别? 3.什么是值传递和引用传递? 4.什么是不可变对象? 5.讲讲类的实例化顺序? 6.java 创建 ...
- Java基础知识(壹)
写在前面的话 这篇博客,是很早之前自己的学习Java基础知识的,所记录的内容,仅仅是当时学习的一个总结随笔.现在分享出来,希望能帮助大家,如有不足的,希望大家支出. 后续会继续分享基础知识手记.希望能 ...
- [Java面经]干货整理, Java面试题(覆盖Java基础,Java高级,JavaEE,数据库,设计模式等)
如若转载请注明出处: http://www.cnblogs.com/wang-meng/p/5898837.html 谢谢.上一篇发了一个找工作的面经, 找工作不宜, 希望这一篇的内容能够帮助到大 ...
- 【JAVA面试题系列一】面试题总汇--JAVA基础部分
JAVA基础 基础部分的顺序: 基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法 线程的语法,集合的语法,io 的语法,虚拟机方面的语法 每天几道,持续更新!! 1.一个". ...
- 最适合作为Java基础面试题之Singleton模式
看似只是最简单的一种设计模式,可细细挖掘,static.synchronized.volatile关键字.内部类.对象克隆.序列化.枚举类型.反射和类加载机制等基础却又不易理解透彻的Java知识纷纷呼 ...
- java基础练习 字符串,控制流,日历,日期等
1,对基本控制流程的一些练习 package org.base.practice3; import org.junit.Test; /** * Created with IntelliJ IDEA. ...
随机推荐
- 大数据应用期末总评(hadoop综合大作业)
作业要求源于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/3363 一.将爬虫大作业产生的csv文件上传到HDFS (1)在/usr ...
- winrunner 测试工具
WinRunner在项目中的作用 (winrunner测试设计:http://blog.chinaunix.net/uid/301743/year-2013-list-81.html?/178 ...
- 微信小程序开发:背景图片设置
本文链接:https://blog.csdn.net/michael_f2008/article/details/86543134开发微信小程序时,不能直接在wxss文件里引用本地图片,运行时会报错: ...
- 分布式事务之最终一致性BASE理论
一.事务 事务提供一种机制将一个活动涉及的所有操作纳入到一个不可分割的执行单元,组成事务的所有操作只有在所有操作均能正常执行的情况下方能提交,只要其中任一操作执行失败,都将导致整个事务的回滚.简单地说 ...
- egg.js 相关
egg sequelize 建表规范 CREATE TABLE `wx_member` ( `id` ) NOT NULL AUTO_INCREMENT COMMENT 'primary key' ...
- [转]windows 下 gcc/g++ 的安装
链接地址:https://www.jianshu.com/p/ff24a81f3637 不过下载地址直接进这里就可以了:https://sourceforge.net/projects/mingw/
- 使用 Fiddler 代理调试本地手机页面
文件下载:http://files.cnblogs.com/files/dtdxrk/fiddler4_4.6.2.0_setup.rar 从事前端开发的同学一定对 Fiddler 不陌生,它是一个非 ...
- python:pytest优秀博客
上海悠悠:https://www.cnblogs.com/yoyoketang/tag/pytest/
- 最新 大众书网java校招面经 (含整理过的面试题大全)
从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.大众书网等10家互联网公司的校招Offer,因为某些自身原因最终选择了大众书网.6.7月主要是做系统复习.项目复盘.Leet ...
- filter和map内置函数
filter filter()函数接收一个函数 f 和一个list,这个函数 f 的作用是对每个元素进行判断,返回 True或 False,filter()根据判断结果自动过滤掉不符合条件的元素,返回 ...